二、Example 用法

  1. 通用 easyjdbc 中的 Example 方法有两大类定义,一个参数,例如下面:
  2. // EasyJdbcTemplate
  3. List<T> findByExample(Example example);
  4. public Example example(Class<?> persistentClass);
  5. //继承接口和实现类(EasyJdbcDao<泛型>,SimpleJdbcDao<泛型>)
  6. 注意: 1.0.6及以上版本只需继承接口EasyJdbcDao<泛型>
  7. Example example();

所有 Example 方法中的 example 类型都是 Object 类型,这是因为通用 easyjdbc 支持所有符合 Example 结构的参数,还有通用 easyjdbc 中提供的通用 Example

3.2 通用 Example

3.2.1 查询

  1. 示例:
  2. Example example = new Example(User.class);
  3. example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
  4. example.or().andLessThan("id", 41);
  5. List<User> users = easyJdbcTemplate.findByExample(example);
  6. // 使用链式
  7. Example example =easyJdbcTemplate.example(User.class);
  8. example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
  9. example.andLessThan("id", 41);
  10. example.list();

3.2.2 获取单列数据get

  1. 示例:
  2. Example example = new Example(User.class);
  3. example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
  4. example.or().andLessThan("id", 41);
  5. User user= easyJdbcTemplate.getByExample(example);
  6. // 使用链式
  7. Example example =easyJdbcTemplate.example(User.class);
  8. example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
  9. example.andLessThan("id", 41);
  10. example.get();

3.2.3 获取总数 count

  1. 示例:
  2. Example example = new Example(User.class);
  3. example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
  4. example.or().andLessThan("id", 41);
  5. int total = easyJdbcTemplate.countByExample(example);
  6. // 使用链式
  7. Example example =easyJdbcTemplate.example(User.class);
  8. example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
  9. example.andLessThan("id", 41);
  10. example.count();

3.2.4 动态 查询

示例:

  1. Example example = new Example(User.class);
  2. Example.Criteria criteria = example.createCriteria();
  3. if(user.getUserName()!= null){
  4. criteria.andLike("userName", user.getUserName() + "%");
  5. }
  6. if(user.getId() != null){
  7. criteria.andGreaterThan("id", user.getId());
  8. }
  9. List<User> users =easyJdbcTemplate.findByExample(example);

3.2.5 排序

示例:

  1. Example example = new Example(User.class);
  2. //单字段排序
  3. Sorts sort=new Sorts(Sorts.Direction.DESC, "id");
  4. //多字段排序
  5. Sorts.Order order=new Sorts.Order(Sorts.Direction.DESC, "id");;
  6. Sorts.Order order1=new Sorts.Order(Sorts.Direction.DESC, "createTime");;
  7. Sorts sort=new Sorts(orderorder1);
  8. example.orderByClause(sort);
  9. List<User> users = easyJdbcTemplate.findByExample(example);

3.2.6 去重

示例:

  1. //设置 distinct
  2. Example example = new Example(User.class)
  3. example.isDistinct(true);
  4. Example.Criteria criteria = example.createCriteria();
  5. if(user.getUserName()!= null){
  6. criteria.andLike("userName", user.getUserName() + "%");
  7. }
  8. List<User> users = easyJdbcTemplate.findByExample(example);

3.2.7 动态映射对象

示例:

  1. Example example=new Example(User.class);
  2. Example.Criteria criteria = example.createCriteria();
  3. if(user.getUserName()!= null){
  4. criteria.andLike("userName", user.getUserName() + "%");
  5. }
  6. example.entityClass(UserDTO.class);
  7. List<UserDTO> users = easyJdbcTemplate.findByExample(example);

3.2.8 mapping动态组装对象

示例:

  1. Example example=new Example(User.class);
  2. Example.Criteria criteria = example.createCriteria();
  3. if(user.getUserName()!= null){
  4. criteria.andLike("userName", user.getUserName() + "%");
  5. }
  6. example.mapping("id","userId"); //初始化映射数据库对象User(DO)字段id 动态映射组装UserDTO的字段为userId
  7. example.entityClass(UserDTO.class); //组装UserDTO(DTO)
  8. List<UserDTO> users = easyJdbcTemplate.findByExample(example);

3.2.9 分组查询

示例:

  1. Example example=new Example(User.class);
  2. example.groupByClause("id");
  3. Example.Criteria criteria = example.createCriteria();
  4. if(user.getUserName()!= null){
  5. criteria.andLike("userName", user.getUserName() + "%");
  6. }
  7. List<User> users = easyJdbcTemplate.findByExample(example);

3.2.10 动态定义查询项

示例:

  1. Example example=new Example(User.class);
  2. example.selectPropertys"id","userName");
  3. Example.Criteria criteria = example.createCriteria();
  4. if(user.getUserName()!= null){
  5. criteria.andLike("userName", user.getUserName() + "%");
  6. }
  7. List<User> users = easyJdbcTemplate.findByExample(example);

3.2.11 动态聚合函数查询

示例:

  1. Example example=new Example(User.class);
  2. //定义一个函数
  3. Aggregation aggregation=new Aggregation(AggregateType.MAX.toString(), "age", "maxAge");
  4. //定义多函数
  5. List list=new ArrayList();
  6. Aggregation.Aggregate aggregate=new Aggregation.Aggregate(AggregateType.MAX.toString(), "age", "maxAge");
  7. Aggregation.Aggregate aggregate1=new Aggregation.Aggregate(AggregateType.SUM.toString(), "id", "sumId");
  8. list.add(aggregate,aggregate1)
  9. Aggregation aggregation=new Aggregation(list);
  10. example.selectPropertysAggregation,"id");
  11. Example.Criteria criteria = example.createCriteria();
  12. if(user.getUserName()!= null){
  13. criteria.andLike("userName", user.getUserName() + "%");
  14. }
  15. example.entityClass(UserDTO.class); //组装UserDTO(DTO)
  16. List<UserDTO> users = easyJdbcTemplate.findByExample(example);

3.2.12 分页

示例:

  1. Example example=new Example(User.class);
  2. example.pageInfo(1,2);
  3. Example.Criteria criteria = example.createCriteria();
  4. if(user.getUserName()!= null){
  5. criteria.andLike("userName", user.getUserName() + "%");
  6. }
  7. example.entityClass(UserDTO);
  8. PageInfo<UserDTO> pageInfo=easyJdbcTemplate.findByPage(example);
  9. //使用链式
  10. Example example =easyJdbcTemplate.example(User.class);
  11. Example.Criteria criteria = example.createCriteria();
  12. if(user.getUserName()!= null){
  13. criteria.andLike("userName", user.getUserName() + "%");
  14. }
  15. example.entityClass(UserDTO .class);
  16. PageInfo<UserDTO> pageInfo=example.page();

原文: https://github.com/xphsc/easyjdbc/wiki/3.2-Example