码迷,mamicode.com
首页 > 数据库 > 详细

动态sql实现分页查询

时间:2018-06-19 22:53:00      阅读:603      评论:0      收藏:0      [点我收藏+]

标签:查询   condition   type   private   short   tst   attr   order by   end   

1.创建实体类对象需要查询的条件com.rl.ecps.model.QueryCondition

  private Long brandId;
  private Short auditStatus;
  private Short showStatus;
  private String itemName;
  private Integer pageNo;
  private Integer startNum;
  private Integer endNum;

2.查询语句

查询条数:

<select id="selectItemByConditionCount" parameterType="com.rl.ecps.model.QueryCondition" resultType="int">
select count(*) from eb_item t
<where>
<if test="brandId != null">
t.brand_id = #{brandId}
</if>
<if test="auditStatus != null">
and t.audit_status = #{auditStatus}
</if>
<if test="showStatus != null">
and t.show_status = #{showStatus}
</if>
<if test="itemName != null and itemName != ‘‘">
and t.item_name like ‘%${itemName}%‘
</if>
</where>
</select>

分页查询:

<select id="selectItemByCondition" parameterType="com.rl.ecps.model.QueryCondition" resultMap="BaseResultMap">
select *
from (select rownum rw, a.*
from (
select * from eb_item t
<where>
<if test="brandId != null">
t.brand_id = #{brandId}
</if>
<if test="auditStatus != null">
and t.audit_status = #{auditStatus}
</if>
<if test="showStatus != null">
and t.show_status = #{showStatus}
</if>
<if test="itemName != null and itemName != ‘‘">
and t.item_name like ‘%${itemName}%‘
</if>
</where>
order by t.item_id desc
<![CDATA[
) a
where rownum < #{endNum}) b
where b.rw > #{startNum}
]]>
</select>

3.分页实体类

public class Page {

/**
* 当前页码 (已知)
*/
private int pageNo = 1;

/**
* 每页记录数(已知)
*/
private int pageSize = 5;

/**
* 指定的查询条件下的总记录数
*/
private int totalCount = 0;

/**
* 指定的查询条件下的总页数
*/
private int totalPage = 1;

/**
* 查询的开始行号
*/
private int startNum = 0;

/**
* 查询的结束行号
*/
private int endNum = 0;

/**
* 查询的结果集
*/
private List<?> list;

4.service与control

service实现:

public Page selectItemByQC(QueryCondition qc) {
//查询当前的查询条件下的总记录数
int totalCount = itemDao.selectItemByConditionCount(qc);
//创建page对象
Page page = new Page();
page.setPageNo(qc.getPageNo());
page.setTotalCount(totalCount);
//计算startNum和endNum
Integer startNum = page.getStartNum();
Integer endNum = page.getEndNum();
//把值设置给sql的查询对象
qc.setStartNum(startNum);
qc.setEndNum(endNum);
//查询结果集
List<EbItem> itemList = itemDao.selectItemByCondition(qc);
page.setList(itemList);
return page;
}

public void saveItem(EbItem item, EbItemClob itemClob,
List<EbParaValue> paraList, List<EbSku> skuList) {
itemDao.saveItem(item);
itemClobDao.saveItemClob(itemClob, item.getItemId());
paraDao.saveParaValue(paraList, item.getItemId());
skuDao.saveSku(skuList, item.getItemId());
}

controller类:

@RequestMapping("/listItem.do")
public String listItem(QueryCondition qc, Model model){
List<EbBrand> bList = brandService.selectBrandAll();
model.addAttribute("bList", bList);
if(qc.getPageNo() == null){
qc.setPageNo(1);
}
Page page = itemService.selectItemByQC(qc);
model.addAttribute("page", page);
//把qc写回去,目的是回显
model.addAttribute("qc", qc);
return "item/list";
}

动态sql实现分页查询

标签:查询   condition   type   private   short   tst   attr   order by   end   

原文地址:https://www.cnblogs.com/ljllove/p/9201254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!