您当前的位置: 首页 > 

java持续实践

暂无认证

  • 4浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

黑马十次方项目day02-10之问答模块代码的编写

java持续实践 发布时间:2019-01-13 15:14:29 ,浏览量:4

文章目录
      • api分析
      • Controller层
      • Service层
      • dao
      • 测试

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 pageData = problemService.newList(labelid, page, size);
        return new Result(true,StatusCode.OK,"查询成功!",new PageResult(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 pageData = problemService.hotList(labelid, page, size);
        return new Result(true,StatusCode.OK,"查询成功!",new PageResult(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 pageData = problemService.waitList(labelid, page, size);
        return new Result(true,StatusCode.OK,"查询成功!",new PageResult(pageData.getTotalElements(),pageData.getContent()));
    }
}
Service层

Service层直接调用dao层, 把当前页数减一 .

@Service
public class ProblemService {

	@Autowired
	private ProblemDao problemDao;
	
	@Autowired
	private IdWorker idWorker;


    public Page newList(String labelid, int page, int rows) {
        Pageable pageAble=new PageRequest(page-1,rows);
        return problemDao.newList(labelid,pageAble);
    }

    public Page hotList(String labelid, int page, int rows) {
        Pageable pageAble=new PageRequest(page-1,rows);
        return problemDao.hotList(labelid,pageAble);
    }

    public Page 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,JpaSpecificationExecutor{

    /**
     * 方法名: newList
     * 方法描述: 最新回答列表
     * 修改日期: 2019/1/13 11:31
     * @param labelid
     * @return java.util.List
     * @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 newList(String labelid, Pageable pageable);

    /**
     * 方法名: hotList
     * 方法描述: 热门回答列表. 回复数最多的
     * 修改日期: 2019/1/13 11:31
      * @param
     * @return java.util.List
     * @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 hotList(String labelid, Pageable pageable);


    /**
     * 方法名: waitList
     * 方法描述: 等待回答列表. 回复数为0的
     * 修改日期: 2019/1/13 11:31
      * @param
     * @return java.util.List
     * @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 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的

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.2374s