XML中代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="test">
<typeAlias alias="PAGES" type="com.xiao4r.crm.util.Pager"/>
<!-- 多条件查询 -->
<select id="loadPayQuery" resultClass="PayQuery" parameterClass="PAGES">
SELECT * FROM
(SELECT A.*, ROWNUM RN FROM
(SELECT * FROM jchome_uinion_multichannel) A
where A.cancelamount_status=‘1‘
<!-- 属性值不为空时拼接查询语句 -->
<isNotEmpty prepend="AND" property="map.starttime">
A.pay_date > #map.starttime#
</isNotEmpty>
<isNotEmpty prepend="AND" property="map.endtime">
#map.endtime# >= A.pay_date
</isNotEmpty>
<isNotEmpty prepend="AND" property="map.source">
A.SOURCE=#map.source#
</isNotEmpty>
<isNotEmpty prepend="AND" property="map.service_name">
A.SERVICE_NAME=#map.service_name#
</isNotEmpty>
order by A.pay_date desc)
WHERE RN BETWEEN #pageSize#*(#currentPage#-1)+1 AND #pageSize#*#currentPage#
</select>
<select id="totalRowsAll" resultClass="java.lang.Integer" parameterClass="java.util.Map">
select count(*) from jchome_uinion_multichannel
where cancelamount_status=‘1‘
<isNotEmpty prepend="AND" property="starttime">
pay_date > #starttime#
</isNotEmpty>
<isNotEmpty prepend="AND" property="endtime">
#object.endtime# >= pay_date
</isNotEmpty>
<isNotEmpty prepend="AND" property="source">
SOURCE=#source#
</isNotEmpty>
<isNotEmpty prepend="AND" property="service_name">
SERVICE_NAME=#service_name#
</isNotEmpty>
</select>
</sqlMap>
Pager.java 类
package com.xiao4r.crm.util;
import java.util.List;
import java.util.Map;
public class Pager<T> {
private int totalRows = 0; // 总行数
private int pageSize = 10; // 每页显示的行数,默认值为10
private int currentPage = 1; // 当前页号
private int totalPages = 0; //总页数
private List<T> results;// 此页查询出的结果
private String param;//查询条件字符串
//传递参数那里本来准备用 泛型来手机参数,但是在xml文件中 点出查询属性 报错:找不到object 对应属性值,所以后来采用map集合来接受多个参数
private T object;//查询条件字符串
//多条件查询可用map集合来接受参数
private Map<Object, Object> map;
public void setPageInfo(int totalRows){
this.setTotalRows(totalRows);
this.totalPages = totalRows / pageSize + (totalRows % pageSize == 0 ? 0 : 1);
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public List<T> getResults() {
return results;
}
public void setResults(List<T> results) {
this.results = results;
}
public boolean canShowFirst() {
if (this.currentPage == 1 || this.totalPages == 0)
return false;
return true;
}
public boolean canShowLast() {
if (this.currentPage == this.totalPages || this.totalPages == 0)
return false;
return true;
}
public boolean canShowPrevious() {
if (this.currentPage == 1 || this.totalPages == 0)
return false;
return true;
}
public boolean canShowNext() {
if (this.currentPage == this.totalPages || this.totalPages == 0)
return false;
return true;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public T getObject() {
return object;
}
public void setObject(T object) {
this.object = object;
}
public Map<Object, Object> getMap() {
return map;
}
public void setMap(Map<Object, Object> map) {
this.map = map;
}
}
//Dao调用类
package com.xiao4r.crm.dao.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;
import com.xiao4r.crm.dao.PayQueryDao;
import com.xiao4r.crm.model.PayQuery;
import com.xiao4r.crm.util.Pager;
@Repository("payqueryDao")
public class PayQueryDaoImpl implements PayQueryDao {
@Autowired
private SqlMapClientTemplate sqlMapClient;
/*
* 分页查询
*/
@Override
public Pager<PayQuery> loadPayQuery(Pager<PayQuery> pager) {
Integer totalRows=(Integer)sqlMapClient.queryForObject("payquery.totalRowsAll",pager.getMap());
List<PayQuery> results=sqlMapClient.queryForList("payquery.loadPayQuery",pager);
pager.setResults(results);
pager.setPageInfo(totalRows);
return pager;
}
}