1、非原生语句排序,在Dao接口Repositorie中编写非原生SQL语句
/**
* 用户管理的Dao接口
*/
public interface UserRepositories extends JpaRepository<User, Integer>,JpaSpecificationExecutor<User>{
/**
* 查询全部,倒序
* @return
*/
@Query(value = "select u from User u order by u.user_id desc")
List<User> findAllUser();
}
2、SQL语句排序,在Dao接口Repositorie中编写原生SQL语句
/**
* 用户管理的Dao接口
*/
public interface UserRepositories extends JpaRepository<User, Integer>,JpaSpecificationExecutor<User>{
/**
* 查询全部,顺序
* @return
*/
@Query(value = "select * from user order by user_id asc",nativeQuery = true)
List<User> findAllUser();
}
3、使用JPA接口进行分页排序PageRequest(如果需要排序的字段有_下划线,使用第4种方案)
controller层
/**
* 查询全部,分页+多字段查询+倒序
*/
@RequestMapping(value = "/findAll", produces = "application/json;charset=UTF-8")
public Result findAll(Integer pageNumber,String condition){
//默认第一页
if (pageNumber == null || pageNumber == 0) pageNumber = 1;
Page<User> users = userService.getAll(condition,pageNumber,10, Sort.Direction.DESC,"id");
Map<String, Object> map = new HashMap<>();
map.put("totalCount", users.getTotalElements());//总数量
map.put("users", users.getContent()); //数据
return new Result(0, "success", map);
}
service层
/**
* 查询全部用户数据,分页+关键字查询+排序
* @param condition 关键字
* @param pageNo 当前页码
* @param pageSize 每页条数
* @param dir 排列顺序
* @param str 排列字段
* @return
*/
public Page<User> getAll(String condition,int pageNo, int pageSize, Sort.Direction dir, String str) {
PageRequest request = buildPageRequest(pageNo, pageSize, dir, str);
//分页查询
return userRepositories.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
// 关键字查询 --模糊查询
if (condition != null && !"".equals(condition)) {
Expression<String> concat1 = criteriaBuilder.concat(root.get("name"),root.get("phone"));
Expression<String> concat2 = criteriaBuilder.concat(root.get("user_name"),root.get("station"));
predicates.add(criteriaBuilder.like(criteriaBuilder.concat(concat1,concat2), "%"+condition+"%"));
// predicates.add(criteriaBuilder.like(criteriaBuilder.concat(root.get("name"),root.get("phone")), "%"+condition+"%"));
}
return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
},request);
}
/**
* 构建PageRequest对象
* @param num 当前页码
* @param size 每页条数
* @param asc 排列顺序
* @param string 排列字段
* @return PageRequest 分页对象
*/
private PageRequest buildPageRequest(int num, int size, Sort.Direction asc,
String string) {
return new PageRequest(num-1, size,asc,string);
}
dao层,注意继承JpaSpecificationExecutor接口
/**
* 用户管理的Dao接口
*/
public interface UserRepositories extends JpaRepository<User, Integer>,JpaSpecificationExecutor<User>{
}
4、使用JPA接口进行分页PageRequest,使用criteriaQuery.orderBy方法进行排序(可解决PageRequest中无法使用_下划线问题)
controller层
/**
* 查询全部,分页+多字段查询+倒序
*/
@RequestMapping(value = "/findAll", produces = "application/json;charset=UTF-8")
public Result findAll(Integer pageNumber,String condition){
//默认第一页
if (pageNumber == null || pageNumber == 0) pageNumber = 1;
Page<User> users = userService.getAll(condition,pageNumber,10);
Map<String, Object> map = new HashMap<>();
map.put("totalCount", users.getTotalElements());//总数量
map.put("users", users.getContent()); //数据
return new Result(0, "success", map);
}
service层
/**
* 查询全部用户数据,分页+关键字查询+排序
* @param condition 关键字
* @param pageNo 当前页码
* @param pageSize 每页条数
* @param dir 排列顺序
* @param str 排列字段
* @return
*/
public Page<User> getAll(String condition,int pageNo, int pageSize) {
PageRequest request = buildPageRequest(pageNo, pageSize);
//分页查询
return userRepositories.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
// 关键字查询 --模糊查询
if (condition != null && !"".equals(condition)) {
Expression<String> concat1 = criteriaBuilder.concat(root.get("name"),root.get("phone"));
Expression<String> concat2 = criteriaBuilder.concat(root.get("user_name"),root.get("station"));
predicates.add(criteriaBuilder.like(criteriaBuilder.concat(concat1,concat2), "%"+condition+"%"));
// predicates.add(criteriaBuilder.like(criteriaBuilder.concat(root.get("name"),root.get("phone")), "%"+condition+"%"));
}
//排序
criteriaQuery.orderBy(criteriaBuilder.desc(root.get("user_id")));
return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
},request);
}
/**
* 构建PageRequest对象
* @param num 当前页码
* @param size 每页条数
* @param asc 排列顺序
* @param string 排列字段
* @return PageRequest 分页对象
*/
private PageRequest buildPageRequest(int num, int size) {
return new PageRequest(num-1, size);
}
dao层,注意继承JpaSpecificationExecutor接口
/**
* 用户管理的Dao接口
*/
public interface UserRepositories extends JpaRepository<User, Integer>,JpaSpecificationExecutor<User>{
}