黑马十次方项目day02-10之问答模块代码的编写
文章目录
api分析
都是get请求, 传递标签的id,和分页相关的当前页,每页显示的条数
Controller层
在Controller层,调用service层,获取Page对象 .该对象返回了总记录数,和当前页的内容.
@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {
@Autowired
private ProblemService problemService;
@RequestMapping(value = "/newlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
public Result newList(@PathVariable String labelid, @PathVariable int page ,@PathVariable int size) {
Page<Problem> pageData = problemService.newList(labelid, page, size);
return new Result(true,StatusCode.OK,"查询成功!",new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
}
@RequestMapping(value = "/hotlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
public Result hotlist(@PathVariable String labelid, @PathVariable int page ,@PathVariable int size) {
Page<Problem> pageData = problemService.hotList(labelid, page, size);
return new Result(true,StatusCode.OK,"查询成功!",new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
}
@RequestMapping(value = "/waitlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
public Result waitlist(@PathVariable String labelid, @PathVariable int page ,@PathVariable int size) {
Page<Problem> pageData = problemService.waitList(labelid, page, size);
return new Result(true,StatusCode.OK,"查询成功!",new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
}
}
Service层
Service层直接调用dao层, 把当前页数减一 .
@Service
public class ProblemService {
@Autowired
private ProblemDao problemDao;
@Autowired
private IdWorker idWorker;
public Page<Problem> newList(String labelid, int page, int rows) {
Pageable pageAble=new PageRequest(page-1,rows);
return problemDao.newList(labelid,pageAble);
}
public Page<Problem> hotList(String labelid, int page, int rows) {
Pageable pageAble=new PageRequest(page-1,rows);
return problemDao.hotList(labelid,pageAble);
}
public Page<Problem> waitList(String labelid, int page, int rows) {
Pageable pageAble=new PageRequest(page-1,rows);
return problemDao.waitList(labelid,pageAble);
}
}
dao
在dao层,直接写sql语句进行查询, 注意要把nativeQuery 设置为true.
/**
* 数据访问接口
* @author Administrator
*
*/
public interface ProblemDao extends JpaRepository<Problem,String>,JpaSpecificationExecutor<Problem>{
/**
* 方法名: newList
* 方法描述: 最新回答列表
* 修改日期: 2019/1/13 11:31
* @param labelid
* @return java.util.List<com.tensquare.qa.pojo.Problem>
* @author taohongchao
* @throws
*/
@Query(value = "select * from tb_problem a ,tb_pl b WHERE a.id = b.problemid and b.labelid=? ORDER BY a.replytime desc" ,nativeQuery = true)
public Page<Problem> newList(String labelid, Pageable pageable);
/**
* 方法名: hotList
* 方法描述: 热门回答列表. 回复数最多的
* 修改日期: 2019/1/13 11:31
* @param
* @return java.util.List<com.tensquare.qa.pojo.Problem>
* @author taohongchao
* @throws
*/
@Query(value = "select * from tb_problem a ,tb_pl b WHERE a.id = b.problemid and b.labelid=? ORDER BY a.reply desc" ,nativeQuery = true)
public Page<Problem> hotList(String labelid, Pageable pageable);
/**
* 方法名: waitList
* 方法描述: 等待回答列表. 回复数为0的
* 修改日期: 2019/1/13 11:31
* @param
* @return java.util.List<com.tensquare.qa.pojo.Problem>
* @author taohongchao
* @throws
*/
@Query(value = "select * from tb_problem a ,tb_pl b WHERE a.id = b.problemid and b.labelid=? and a.reply=0 ORDER BY a.createtime desc",nativeQuery = true)
public Page<Problem> waitList(String labelid, Pageable pageable);
}
测试
测试数据库中的数据如下
运行QaApplication启动类运行项目.
调用最新问题接口http://localhost:9003/problem/newlist/1/1/6
,返回数据如下. 根据创建时间字段排序
调用热门回答接口 http://localhost:9003/problem/hotlist/1/1/6
,根据reply排序
调用等待回答的接口http://localhost:9003/problem/waitlist/1/1/10
,返回数据如下, 只返回reply为0的