Mybatis之Mapper接口及Example实例函数使用详解

 

声明:本文章部分内容源自于优快云博主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()更新想更新的字段。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值