3.2 Example - xphsc/EasyJdbc GitHub Wiki
通用 easyjdbc 中的 Example 方法有两大类定义,一个参数,例如下面:
// EasyJdbcTemplate
List<T> findByExample(Example example);
public Example example(Class<?> persistentClass);
//继承接口和实现类(EasyJdbcDao<泛型>,SimpleJdbcDao<泛型>)
注意: 从1.0.6及以上版本只需继承接口EasyJdbcDao<泛型>
Example example();
所有 Example 方法中的 example 类型都是 Object 类型,这是因为通用 easyjdbc 支持所有符合 Example 结构的参数,还有通用 easyjdbc 中提供的通用 Example
示例:
Example example = new Example(User.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.or().andLessThan("id", 41);
List<User> users = easyJdbcTemplate.findByExample(example);
// 使用链式
Example example =easyJdbcTemplate.example(User.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.andLessThan("id", 41);
example.list();
// 使用函数表达式(easyjdbc2.x)
Example example =easyJdbcTemplate.example(User.class);
example.createCriteria().andGreaterThan(User::getId, 100).andLessThan(User::getId,151);
example.andLessThan(User::getId, 41);
example.list();
示例:
Example example = new Example(User.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.or().andLessThan("id", 41);
User user= easyJdbcTemplate.getByExample(example);
// 使用链式
Example example =easyJdbcTemplate.example(User.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.andLessThan("id", 41);
example.get();
// 使用函数表达式(easyjdbc2.x)
Example example =easyJdbcTemplate.example(User.class);
example.createCriteria().andGreaterThan(User::getId, 100).andLessThan(User::getId,151);
example.andLessThan(User::getId, 41);
example.get();
示例:
Example example = new Example(User.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.or().andLessThan("id", 41);
int total = easyJdbcTemplate.countByExample(example);
// 使用链式
Example example =easyJdbcTemplate.example(User.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.andLessThan("id", 41);
example.count();
// 使用函数表达式(easyjdbc2.x)
Example example =easyJdbcTemplate.example(User.class);
example.createCriteria().andGreaterThan(User::getId, 100).andLessThan(User::getId,151);
example.andLessThan(User::getId, 41);
example.count();
3.2.4 动态 查询
示例:
Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
if(user.getId() != null){
criteria.andGreaterThan("id", user.getId());
}
List<User> users =easyJdbcTemplate.findByExample(example);
// 使用函数表达式(easyjdbc2.x)
if(user.getUserName()!= null){
criteria.andLike("User::getUserName, user.getUserName() + "%");
}
if(user.getId() != null){
criteria.andGreaterThan(User::getId, user.getId());
};
example.list();
3.2.5 排序
示例:
Example example = new Example(User.class);
//单字段排序
Sorts sort=new Sorts(Sorts.Direction.DESC, "id");
//多字段排序
Sorts.Order order=new Sorts.Order(Sorts.Direction.DESC, "id");;
Sorts.Order order1=new Sorts.Order(Sorts.Direction.DESC, "createTime");;
Sorts sort=new Sorts(order,order1);
example.orderByClause(sort);
List<User> users = easyJdbcTemplate.findByExample(example);
3.2.6 去重
示例:
//设置 distinct
Example example = new Example(User.class)
example.isDistinct(true);
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
List<User> users = easyJdbcTemplate.findByExample(example);
3.2.7 动态映射对象
示例:
Example example=new Example(User.class);
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
example.entityClass(UserDTO.class);
List<UserDTO> users = easyJdbcTemplate.findByExample(example);
3.2.8 mapping动态组装对象
示例:
Example example=new Example(User.class);
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
example.mapping("id","userId"); //初始化映射数据库对象User(DO)字段id 动态映射组装UserDTO的字段为userId
example.entityClass(UserDTO.class); //组装UserDTO(DTO)
List<UserDTO> users = easyJdbcTemplate.findByExample(example);
3.2.9 分组查询
示例:
Example example=new Example(User.class);
example.groupByClause("id");
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
List<User> users = easyJdbcTemplate.findByExample(example);
3.2.10 动态定义查询项
示例:
Example example=new Example(User.class);
example.selectPropertys("id","userName");
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
List<User> users = easyJdbcTemplate.findByExample(example);
3.2.11 动态聚合函数查询
示例:
Example example=new Example(User.class);
//定义一个函数
Aggregation aggregation=new Aggregation(AggregateType.MAX.toString(), "age", "maxAge");
//定义多函数
List list=new ArrayList();
Aggregation.Aggregate aggregate=new Aggregation.Aggregate(AggregateType.MAX.toString(), "age", "maxAge");
Aggregation.Aggregate aggregate1=new Aggregation.Aggregate(AggregateType.SUM.toString(), "id", "sumId");
list.add(aggregate,aggregate1)
Aggregation aggregation=new Aggregation(list);
example.selectPropertys(Aggregation,"id");
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
example.entityClass(UserDTO.class); //组装UserDTO(DTO)
List<UserDTO> users = easyJdbcTemplate.findByExample(example);
3.2.12 分页
示例:
Example example=new Example(User.class);
example.pageInfo(1,2);
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
example.entityClass(UserDTO);
PageInfo<UserDTO> pageInfo=easyJdbcTemplate.findByPage(example);
//使用链式
Example example =easyJdbcTemplate.example(User.class);
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
example.entityClass(UserDTO .class);
PageInfo<UserDTO> pageInfo=example.page();
//offset limit 分页
Example example =userDao.example();
example.offsetPage(0,10);
Example.Criteria criteria = example.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
example.entityClass(UserDTO .class);
PageInfo<UserDTO> pageInfo=example.page();