1. 程式人生 > >Mybatis之Mapper介面及Example例項函式使用詳解

Mybatis之Mapper介面及Example例項函式使用詳解

宣告:本文章部分內容源自於CSDN博主biandous的部落格文章,在其基礎上進行了部分修正和程式碼修改。

一、Mapper介面方法

方法 功能說明
int countByExample(UserExample example) throws SQLException 按條件計數
int deleteByPrimaryKey(Integer id) throws SQLException 按主鍵刪除
int deleteByExample(UserExample example) throws SQLException 按條件刪除
String/Integer insert(User record) throws SQLException 插入資料(返回值為ID)
User selectByPrimaryKey(Integer id) throws SQLException 按主鍵查詢
List selectByExample(UserExample example) throws SQLException 按條件查詢,返回型別為List
List selectByExampleWithBLOGs(UserExample example) throws SQLException 按條件查詢(包括BLOB欄位)。只有當資料表中的欄位型別包含二進位制的才會產生。
int updateByPrimaryKey(User record) throws SQLException 按主鍵更新
int updateByPrimaryKeySelective(User record) throws SQLException 按主鍵更新值不為Null的欄位
int updateByExample(User record, UserExample example) throws SQLException 按條件更新
int updateByExampleSelective(User record, UserExample example) throws SQLException

按條件更新值不為Null的欄位

二、Example例項使用詳情

Mybatis的逆向工程中會生成例項及例項對應的Example,Example用於新增條件,等同於SQL語句WHERE關鍵字後的篩選條件部分。

xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();

示例程式碼如下(實際專案應用):

/**
 * 根據Appkey查詢渠道資訊
 * @param appkey
 * @return
 */
 public ChannelInfo getChannelInfoByAppkey(String appkey) throws SCFException {
     log.debug("Enter getChannelInfoByAppkey appkey:{}", appkey);
     ChannelInfoExample example = new ChannelInfoExample();
     // 這裡直接通過createCriteria()方法建立Criteria例項後新增條件
     example.createCriteria().andAppKeyEqualTo(appkey);
     List<ChannelInfo> list = mapper.selectByExample(example);
     if (!CollectionUtils.isEmpty(list) && list.size() == 1) {
         log.debug("Exit getChannelInfoByAppkey ChannelInfo:{}", list.get(0));
         return list.get(0);
     } else {
         log.error("ERROR getChannelInfoByAppkey not find ChannelInfo. appkey is {}", appkey);
         throw new SCFException("渠道資訊不存在");
     }
 }
方法 功能說明
example.setOrderByClause("欄位名 ASC"); 添加升序排列條件,DESC為降序
example.setDistinct(false); 去除重複,引數為boolean型,true為選擇不重複的記錄
criteria.andXxxIsNull 新增欄位xxx為Null的條件
criteria.andXxxIsNotNull 新增欄位xxx不為Null的條件
criteria.andXxxEqualTo(value) 新增xxx欄位等於value條件
criteria.andXxxNotEqualTo(value) 新增xxx欄位不等於value條件
criteria.andXxxGreaterThan(value) 新增xxx欄位大於value條件
criteria.andXxxGreaterThanOrEqualTo(value) 新增xxx欄位大於等於value條件
criteria.andXxxLessThan(value) 新增xxx欄位小於value條件
criteria.andXxxLessThanOrEqualTo(value) 新增xxx欄位小於等於value條件
criteria.andXxxIn(List<?>) 新增xxx欄位值在List<?>條件
criteria.andXxxNotIn(List<?>) 新增xxx欄位值不在List<?>條件
criteria.andXxxLike(“%”+value+”%”) 新增xxx欄位值為value的模糊查詢條件
criteria.andXxxNotLike(“%”+value+”%”) 新增xxx欄位值不為value的模糊查詢條件
criteria.andXxxBetween(value1,value2) 新增xxx欄位值在value1和value2之間條件
criteria.andXxxNotBetween(value1,value2) 新增xxx欄位值不在value1和value2之間條件

三、專案應用場景程式碼

1、查詢

① selectByPrimaryKey()

ChannelInfo channelInfo = getChannelInfoByAppkey(appKey);
ChannelInfoExample example = new ChannelInfoExample();
// 等同於查詢語句select * from ChannelInfo where id = xxx
example.createCriteria().selectByPrimaryKey(channelInfo.getId);

② selectByExample() 和 selectByExampleWithBLOGs()

ChannelInfoExample example = new ChannelInfoExample();
example.createCriteria().andAppKeyEqualTo(appKey).andIsDeleteEqualTo("n");
// 根據AppKey值和刪除狀態來查詢渠道資訊
// 等同於select * from channel_info where app_key = 'xxx' and is_delete = 'n'; 
List<ChannelInfo> list = mapper.selectByExample(example);

注:在Mybatis逆向工程生成的文章XxxExample.java 中包含一個static的內部類Criteria,Criteria中的方法是定義SQL語句where後的查詢條件。

2、插入

①insert()

public ResourceInfo insertResource(ResourceInfo resourceInfo) {
    resourceInfoMapper.insert(resourceInfo);
    return resourceInfo;
}

3、更新

①updateByPrimaryKey()

ChannelInfo channelInfo = new ChannelInfo();
channelInfo.setId(channelId);
channelInfo.setAppKey(appKey);
channelInfo.setAppSecret(appSecret);
channelInfo.setCreateTime(DateUtil.getNowDate());
channelInfo.setModifiyTime(DateUtil.getNowDate());
channelInfo.setIsDelete(isDelete);
mapper.updateByPrimaryKey(channelInfo);

②updateByPrimaryKeySelective()

ChannelInfo channelInfo = new ChannelInfo();
channelInfo.setId(channelId);
channelInfo.setAppKey(appKey);
channelInfo.setAppSecret(appSecret);
mapper.updateByPrimaryKeySelective(channelInfo);

③ updateByExample() 和 updateByExampleSelective() 

ChannelInfoExample channelInfoExample = new ChannelInfoExample();
Criteria criteria = example.createCriteria();
criteria.andIdEqualTo(channelId);
ChannelInfo channelInfo = new ChannelInfo();
channelInfo.setId(channelId);
channelInfo.setAppKey(appKey);
channelInfo.setAppSecret(appSecret);
mapper.updateByExampleSelective(channelInfo, channelInfoExample);

updateByExample()更新所有的欄位,包括欄位為null的也更新,建議使用 updateByExampleSelective()更新想更新的欄位。