前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Example 对象可以设置的查询条件

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

原创
作者头像
用户4396583
发布2024-07-17 00:10:46
330
发布2024-07-17 00:10:46
举报
文章被收录于专栏:spring

1、等于条件

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

代码语言:java
复制
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):设置属性小于等于指定值的条件。

代码语言:java
复制
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):设置属性不在指定区间内的条件。

代码语言:java
复制
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):设置属性不在给定集合中的条件。

代码语言:java
复制
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):设置属性不为空的条件。

代码语言:java
复制
Example example = new Example(User.class);
example.createCriteria().andIsNull("email")
                        .andIsNotNull("phone");

6、模糊匹配条件

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


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

代码语言:java
复制
Example example = new Example(User.class);
example.createCriteria().andLike("name", "Joh%")
                        .andNotLike("address", "%Apartment%");

7、自定义条件

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

代码语言:java
复制
Example example = new Example(User.class);
example.createCriteria().andCondition("age > 20 and salary < 60000");

8、逻辑运算符条件

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


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

代码语言:java
复制
Example example = new Example(User.class);
example.createCriteria()
       .andGreaterThan("age", 20)
       .andLessThan("salary", 60000)
       .or()
       .andEqualTo("name", "Alice");

9、多条件组合

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

代码语言:java
复制
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):设置是否进行去重查询。

代码语言:java
复制
Example example = new Example(User.class);
example.setDistinct(true);

11、设置排序规则

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

代码语言:java
复制
Example example = new Example(User.class);
example.setOrderByClause("age DESC, salary ASC");

12、选择要查询的字段

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

代码语言:java
复制
Example example = new Example(User.class);
example.selectProperties("name", "age", "email");

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、等于条件
  • 2、大于、小于条件
  • 3、区间条件
  • 4、包含、不包含条件
  • 5、为空、不为空条件
  • 6、模糊匹配条件
  • 7、自定义条件
  • 8、逻辑运算符条件
  • 10、去重查询
  • 11、设置排序规则
  • 12、选择要查询的字段
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档