条件查询
POST /label/search 根据条件查询城市列表
/**
* 构建查询条件
* @param searchMap
* @return
*/
private Specification<Label> createSpecification(Map searchMap){
return new Specification<Label>() {
@Override
public Predicate toPredicate(Root<Label> root, CriteriaQuery<?>
criteriaQuery, CriteriaBuilder cb) {
List<Predicate> predicateList=new ArrayList<>();
if(searchMap.get("labelname")!=null &&
!"".equals(searchMap.get("labelname"))){
predicateList.add(cb.like(
root.get("labelname").as(String.class), "%"+
(String)searchMap.get("labelname")+"%" ) );
}
if(searchMap.get("state")!=null &&
!"".equals(searchMap.get("state"))){
predicateList.add(cb.equal(
root.get("state").as(String.class), (String)searchMap.get("state") ) );
}
if(searchMap.get("recommend")!=null &&
!"".equals(searchMap.get("recommend"))){
predicateList.add(cb.equal(
root.get("recommend").as(String.class),
(String)searchMap.get("recommend") ) );
}
return cb.and( predicateList.toArray( new
Predicate[predicateList.size()]) );
}
};
}
/**
* 条件查询
* @param searchMap
* @return
*/
public List<Label> findSearch(Map searchMap){
北京市昌平区建材城西路金燕龙办公楼一层 电话:400-618-9090
(2)修改LabelController,增加方法
1.2 带分页的条件查询
(1)修改LabelService,增加方法
(2)修改LabelController,增加方法
Specification specification= createSpecification(searchMap);
return labelDao.findAll( specification);
}
修改LabelController,增加方法
/**
* 根据条件查询
* @param searchMap
* @return
*/
@RequestMapping(value="/search",method = RequestMethod.POST)
public Result<List> findSearch( @RequestBody Map searchMap){
return new Result<>(true,StatusCode.OK,"查询成
功",labelService.findSearch(searchMap));
}
带分页的条件查询
/**
* 分页条件查询
* @param searchMap
* @param page
* @param size
* @return
*/
public Page<Label> findSearch(Map searchMap,int page,int size){
Specification specification= createSpecification(searchMap);
PageRequest pageRequest=PageRequest.of(page‐1,size);
return labelDao.findAll( specification ,pageRequest);
}
修改LabelController,增加方法
/**
* 条件+分页查询
* @param searchMap
* @param page
* @param size
* @return
*/
@RequestMapping(value="/search/{page}/{size}",method =
RequestMethod.POST)
public Result<List> findSearch( @RequestBody Map searchMap
,@PathVariable int page,@PathVariable int size ){
Page pageList= labelService.findSearch(searchMap,page,size);
return new Result(true,StatusCode.OK,"查询成功",new PageResult<>
(pageList.getTotalElements(),pageList.getContent() ));
}
需求分析:查询状态为2并以创建日期降序排序,查询前4条记录
在RecruitDao新增方法定义
/**
* 查询最新职位列表(按创建日期降序排序)
* @return
*/
public List<Recruit> findTop4ByStateOrderByCreatetimeDesc(String state);
RecruitService新增方法
/**
* 根据状态查询
* @param state
* @return
*/
public List<Recruit> findTop4ByStateOrderByCreatetimeDesc(String
state){
return recruitDao.findTop4ByStateOrderByCreatetimeDesc(state);
}
RecruitController新增方法
@RequestMapping(value="/search/recommend",method= RequestMethod.GET)
public Result recommend(){
List<Recruit> list =
recruitService.findTop4ByStateOrderByCreatetimeDesc("2");
return new Result(true,StatusCode.OK,"查询成功",list);
}
需求分析:查询状态不为0并以创建日期降序排序,查询前12条记录
在RecruitDao新增方法定义
/**
* 最新职位列表
* @param state
* @return
*/
public List<Recruit> findTop12ByStateNotOrderByCreatetimeDesc(String
state);
RecruitService新增方法
/**
* 最新职位列表
* @return
*/
public List<Recruit> newlist(){
return recruitDao.findTop12ByStateNotOrderByCreatetimeDesc("0");
}
RecruitController新增方法
/**
* 最新职位列表
* @return
*/
@RequestMapping(value="/search/newlist",method= RequestMethod.GET)
public Result newlist(){
return new Result(true,StatusCode.OK,"查询成
功",recruitService.newlist());
}
需求分析:最新回复的问题显示在上方, 按回复时间降序排序。
(1)创建中间表pl的实体类
@Entity
@Table(name="tb_pl")
public class Pl implements Serializable{
@Id
private String problemid;
@Id
private String lableid;
public String getLableid() {
return lableid;
}
public void setLableid(String lableid) {
this.lableid = lableid;
}
public String getProblemid() {
return problemid;
}
public void setProblemid(String problemid) {
this.problemid = problemid;
}
}
ProblemDao新增方法定义
/**
* 根据标签ID查询最新问题列表
* @param labelId
* @param pageable
* @return
*/
@Query("select p from Problem p where id in( select problemid from Pl
where labelid=?1 ) order by replytime desc")
public Page<Problem> findNewListByLabelId(String labelId, Pageable
pageable);
ProblemService新增方法
/**
* 根据标签ID查询问题列表
* @param lableId 标签ID
* @param page 页码
* @param size 页大小
* @return
*/
public Page<Problem> findNewListByLabelId(String lableId,int page,
int size) {
PageRequest pageRequest = PageRequest.of(page‐1, size);
return problemDao.findNewListByLabelId(lableId,pageRequest);
}
ProblemController新增方法
/**
* 根据标签ID查询最新问题列表
* @param labelid
* @return
*/
@RequestMapping(value="/newlist/{labelid}/{page}/{size}",method=RequestMe
thod.GET)
public Result findNewListByLabelId(@PathVariable String
labelid,@PathVariable int page,@PathVariable int size ){
Page<Problem> pageList =
problemService.findNewListByLabelId(labelid, page, size);
PageResult<Problem> pageResult = new PageResult<>
(pageList.getTotalElements(), pageList.getContent());
return new Result(true, StatusCode.OK, "查询成功",pageResult);
}
需求分析:按回复数降序排序
ProblemDao新增方法定义
/**
* 根据标签ID查询热门问题列表
* @param labelId
* @param pageable
* @return
*/
@Query("select p from Problem p where id in( select problemid from Pl
where labelid=?1 ) order by reply desc")
public Page<Problem> findHotListByLabelId(String labelId, Pageable
pageable);
ProblemService新增方法
/**
* 根据标签ID查询热门问题列表
* @param lableId 标签ID
* @param page 页码
* @param size 页大小
* @return
*/
public Page<Problem> findHotListByLabelId(String lableId,int page,
int size) {
PageRequest pageRequest = PageRequest.of(page‐1, size);
return problemDao.findHotListByLabelId(lableId,pageRequest);
}
ProblemController新增方法
/**
* 根据标签ID查询热门问题列表
* @param labelid
* @return
*/
@RequestMapping(value="/hotlist/{labelid}/{page}/{size}",method=RequestMe
thod.GET)
public Result findHotListByLabelId(@PathVariable String
labelid,@PathVariable int page,@PathVariable int size ){
Page<Problem> pageList =
problemService.findHotListByLabelId(labelid, page, size);
PageResult<Problem> pageResult = new PageResult<>
(pageList.getTotalElements(), pageList.getContent());
return new Result(true, StatusCode.OK, "查询成功",pageResult);
}
ProblemDao新增方法定义
/**
* 根据标签ID查询等待回答列表
* @param labelId
* @param pageable
* @return
*/
@Query("select p from Problem p where id in( select problemid from Pl
where labelid=?1 ) and reply=0 order by createtime desc")
public Page<Problem> findWaitListByLabelId(String labelId, Pageable
pageable);
ProblemService新增方法
/**
* 根据标签ID查询等待回答列表
* @param lableId 标签ID
* @param page 页码
* @param size 页大小
* @return
*/
public Page<Problem> findWaitListByLabelId(String lableId,int page,
int size) {
PageRequest pageRequest = PageRequest.of(page‐1, size);
return problemDao.findWaitListByLabelId(lableId,pageRequest);
}
####### ProblemController新增方法
/**
* 根据标签ID查询等待回答列表
* @param labelid
* @return
*/
@RequestMapping(value="/waitlist/{labelid}/{page}/{size}",method=RequestM
ethod.GET)
public Result findWaitListByLabelId(@PathVariable String
labelid,@PathVariable int page,@PathVariable int size ){
Page<Problem> pageList =
problemService.findWaitListByLabelId(labelid, page, size);
PageResult<Problem> pageResult = new PageResult<>
(pageList.getTotalElements(), pageList.getContent());
return new Result(true, StatusCode.OK, "查询成功",pageResult);
}
ArticleDao新增方法
/**
* 审核
* @param id
*/
@Modifying
@Query("update Article set state='1' where id=?1")
public void examine(String id);
ArticleService新增方法
/**
* 文章审核
* @param id
*/
@Transactional
public void examine(String id){
articleDao.examine(id);
}
ArticleController新增方法
/**
* 审核
* @param id
* @return
*/
@RequestMapping(value="/examine/{id}",method=RequestMethod.PUT)
public Result examine(@PathVariable String id){
articleService.examine(id);
return new Result(true, StatusCode.OK, "审核成功!");
}
rides
见spring-coloud全家桶
网友评论