Mybatis Example的高级用法

近几个项目一直使用的mybatis来对数据库做查询,期间用到了很多高效简洁的查询方法,特此记录和分享。

一. mapper接口中的函数及方法

方法名功能
int countByExample(UserExample example)按条件计数
int deleteByPrimaryKey(Integer id)按主键删除
int deleteByExample(UserExample example)按条件查询
String/Integer insert(User record)插入数据(返回值为ID)
User selectByPrimaryKey(Integer id)按主键查询
ListselectByExample(UserExample example)按条件查询
ListselectByExampleWithBLOGs(UserExample example)按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
int updateByPrimaryKey(User record)按主键更新
int updateByPrimaryKeySelective(User record)按主键更新值不为null的字段
int updateByExample(User record, UserExample example)按条件更新
int updateByExampleSelective(User record, UserExample example)按条件更新值不为null的字段

二. example实例方法

example 用于添加条件,相当于where后面的部分,理论上单表的任何复杂条件查询都可以使用example来完成。

方法说明
example.setOrderByClause(“字段名 ASC”);添加升序排列条件,DESC为降序
example.setDistinct(false)去除重复,boolean型,true为选择不重复的记录。
example.and(Criteria criteria)为example添加criteria查询条件,关系为与
example.or(Criteria criteria)为example添加criteria查询条件,关系为或
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.基本字段查询**

   			// 1.使用criteria
   			Example example = new Example(User.class);
           	Criteria criteria = example.createCriteria();
            criteria.andEqualTo("name", name);
            criteria.andNotEqualTo("id", id);
            criteria.andEqualTo("userId", uid);
            List<User> list = userMapper.selectByExample(example);
            
            // 不使用criteria,实则example.and()本质底层还是返回的criteria,倒是可以简便写法。
            Example example = new Example(User.class);
			example.and()
                .andEqualTo("name", name)
                .andEqualTo("id", id)
                .andEqualTo("userId", uid);
				List<User> list = userMapper.selectByExample(example);
		等效于:select * from user where name = #{name} and id = #{id} and uid = #{uid}

2. and or 查询

		Example example = new Example(User.getClass());
        // where 条件
        Criteria criteria = example.createCriteria();
        Criteria criteria1 = example.createCriteria();
        
        criteria.andIn("id", ids);
        criteria1.orLike("des", "%" + des + "%");
        criteria1.orLike("name", "%" + name + "%");
        example.and(criteria1);
        example.and().andEqualTo("status", staus)
 等效于:where id in ( #{ids} ) and ( name like concat(%, #{name} ,%) or des like concat(%, #{des} ,%) ) and status = #{status} 

注意:如果不加 example.and(criteria1);,则默认example只添加生成的第一个criteria,criteria1 将不会加到此条件中

2. 数组参数的条件查询

public Example test(List<String> names, String sex) {
		Example example = new Example(User.getClass());
		example.and().andEqualTo("sex", sex)
        Example.Criteria criteria = example.createCriteria();
        for (String str : names) {
             criteria.orLike("name", str);
         }
         example.and(criteria);
         List<User> list = userMapper.selectByExample(example);
            
 等效于:where sex = #{sex} and ( name like concat(%, #{name1} ,%) or name like concat(%, #{name2} ,%) )
}
  • 6
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值