Example 对象可以设置的查询条件

1、等于条件

andEqualTo(String property, Object value):设置属性等于指定值的条件。

andNotEqualTo(String property, Object value):设置属性不等于指定值的条件。

Example example = new Example(User.class);
example.createCriteria().andEqualTo("age", 25);

2、大于、小于条件

andGreaterThan(String property, Object value):设置属性大于指定值的条件。

andGreaterThanOrEqualTo(String property, Object value):设置属性大于等于指定值的条件。

andLessThan(String property, Object value):设置属性小于指定值的条件。

andLessThanOrEqualTo(String property, Object value):设置属性小于等于指定值的条件。

Example example = new Example(User.class);
example.createCriteria().andGreaterThan("age", 25)
                        .andLessThan("salary", 50000);

3、区间条件

andBetween(String property, Object value1, Object value2):设置属性在指定区间内的条件。

andNotBetween(String property, Object value1, Object value2):设置属性不在指定区间内的条件。

Example example = new Example(User.class);
example.createCriteria().andBetween("age", 20, 30)
                        .andNotBetween("salary", 10000, 30000);

4、包含、不包含条件

andIn(String property, Iterable<?> values):设置属性在给定集合中的条件。

andNotIn(String property, Iterable<?> values):设置属性不在给定集合中的条件。

Example example = new Example(User.class);
example.createCriteria().andIn("department", Arrays.asList("IT", "Finance"))
                        .andNotIn("role", Arrays.asList("Admin", "Manager"));

5、为空、不为空条件

andIsNull(String property):设置属性为空的条件。

andIsNotNull(String property):设置属性不为空的条件。

Example example = new Example(User.class);
example.createCriteria().andIsNull("email")
                        .andIsNotNull("phone");

6、模糊匹配条件

andLike(String property, String value):设置属性模糊匹配指定值的条件。

andNotLike(String property, String value):设置属性不模糊匹配指定值的条件。

Example example = new Example(User.class);
example.createCriteria().andLike("name", "Joh%")
                        .andNotLike("address", "%Apartment%");

7、自定义条件

andCondition(String condition):根据自定义的条件表达式添加条件。

Example example = new Example(User.class);
example.createCriteria().andCondition("age > 20 and salary < 60000");

8、逻辑运算符条件

and():使用 AND 连接当前条件。

or():使用 OR 连接当前条件。

Example example = new Example(User.class);
example.createCriteria()
       .andGreaterThan("age", 20)
       .andLessThan("salary", 60000)
       .or()
       .andEqualTo("name", "Alice");

9、多条件组合

createCriteria():创建一个条件对象,可以在该对象上设置多个条件并进行组合。

Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("department", "HR");
criteria.andGreaterThan("age", 30);
criteria.andLessThan("salary", 80000);

10、去重查询

setDistinct(boolean distinct):设置是否进行去重查询。

Example example = new Example(User.class);
example.setDistinct(true);

11、设置排序规则

setOrderByClause(String orderByClause):设置排序规则。

Example example = new Example(User.class);
example.setOrderByClause("age DESC, salary ASC");

12、选择要查询的字段

selectProperties(String… properties):设置要查询的字段。

Example example = new Example(User.class);
example.selectProperties("name", "age", "email");

总结

这些示例涵盖了 tk.mybatis.mapper.entity.Example 对象的常见查询条件设置。你可以根据具体需求,选择适合的方法和组合来构建查询条件,从而更轻松地执行数据库查询操作。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值