实现根据新闻的分类ID 新闻的标题分页查询新闻信息

Dao

  /**
     * 根据新闻的分类ID和标题  查询总记录数
     * @param categoryId
     * @param title
     * @return
     */
    int count(int categoryId,String title);
    /**
     * 根据新闻的分类ID和新闻的标题 查询当前页的数据
     * @param categoryId
     * @param title
     * @param startRow
     * @param pageSize
     * @return
     */
    List<NewsDetail> selectNewsDetailByPage(int categoryId,String title,int startRow,int pageSize);
public int count(int categoryId, String title) {
        String sql="select count(id) from news_detail where 1=1 ";
        List<Object> params=new ArrayList<>();//sql语句上的参数
        if(categoryId!=0){   //分类不是全部,sql语句中要按照分类查询
            sql+=" and categoryId=?";
            params.add(categoryId);
        }
        if(title!=null && !title.trim().equals("")){ //标题不为空,sql语句中要按照标题查询
            sql+=" and title like concat('%',?,'%')";
            params.add(title);
        }
        int count=0;
        ResultSet resultSet= kgcExecuteQuery(sql,params.toArray());
        try {
            while(resultSet.next()){
                count= resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(resultSet,preparedStatement,connection);
        }
        return count;
    }
public List<NewsDetail> selectNewsDetailByPage(int categoryId, String title, int startRow, int pageSize) {
       String sql="select * from news_detail where 1=1 "; //sql语句
       List<Object> params=new ArrayList<>();//sql语句上的参数
       if(categoryId!=0){
           sql+=" and categoryId=?";
           params.add(categoryId);
       }
       if(title!=null && !title.trim().equals("")){
           sql+=" and title like CONCAT('%',?,'%')";
           params.add(title);
       }
       sql+=" limit ?,?";
       params.add(startRow);
       params.add(pageSize);
       List<NewsDetail> lists=new ArrayList<>();  //返回的当前页的数据
       ResultSet resultSet= kgcExecuteQuery(sql,params);
        try {
            while(resultSet.next()){
                int id= resultSet.getInt("id"); //id
                String author=resultSet.getString("author"); //author
                title=resultSet.getString("title"); //title
                Timestamp timestamp= resultSet.getTimestamp("createDate");
                Date createDate=null;
                if(timestamp!=null){
                    createDate=new Date(timestamp.getTime()); //创建时间
                }
                NewsDetail newsDetail=new NewsDetail(); //将查询到的数据封装到对象中
                newsDetail.setId(id);
                newsDetail.setAuthor(author);
                newsDetail.setTitle(title);
                newsDetail.setCreateDate(createDate);
                lists.add(newsDetail);  //将对象保存到集合中
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(resultSet,preparedStatement,connection);
        }
        return lists;
    }

service

  /**
     * 分页查询新闻信息
     * @param categoryId 分类的ID
     * @param title 标题
     * @param currentPage 当前页
     * @param pageSize 每页显示多少条记录
     * @return
     */
    PageUtil<NewsDetail> queryNewsDetailByPage(int categoryId,String title,
                                               int currentPage,int pageSize);




    /**
     * 分页查询新闻信息
     * @param currentPage 当前页
     * @param pageSize  每页显示多少条记录
     * @return
     */
    PageUtil<NewsDetail> queryNewsDetailByPage(int currentPage,int pageSize);
public PageUtil<NewsDetail> queryNewsDetailByPage(int categoryId, String title,
                                                      int currentPage, int pageSize) {
        PageUtil<NewsDetail> pageUtil=new PageUtil<>(); //封装的是当前页的所有信息
        int count=newsDetailDao.count(categoryId,title); //查询总记录数
        pageUtil.setTotalCount(count); //保存总记录数
        pageUtil.setPageSize(pageSize); //保存每页显示的条数
        pageUtil.setCurrentPage(currentPage); //保存当前页
        int startRow=pageUtil.getStartRow(); //获得每页的开始条数

        List<NewsDetail> lists=newsDetailDao
                .selectNewsDetailByPage(categoryId,title,startRow,pageSize); //获得当前页的数据
        pageUtil.setLists(lists); //保存当前页的数据
        return pageUtil;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值