JdbcTemplageSupport 继承自 org.springframework.jdbc.core.JdbcTemplate;
包括了3组方法:
1、传入的参数是List
public PageResult pagedQuery(String sql, List
sql:查询语句
sqlArgs:查询参数List,顺序需要与sql中的?一一对应
mappedClass:查询的结果返回的封装Class
pageParam:page的参数,包括pageIndex,pageSize
public PageResult pagedQuery(String sql, List
page:查询的页码
2、传入的参数是Object[]形式
public PageResult pagedQuery(String sql, Object[] sqlArgs, Class mappedClass, int page, int rows)
public PageResult pagedQuery(String sql, Object[] sqlArgs, Class mappedClass, PageParam pageParam)
参数定义与上述2个方法一样,只是sqlArgs不同
3、传入的参数是Object...形式
public PageResult pagedQuery(String sql, Class mappedClass, PageParam pageParam, Object... sqlArgs)
public PageResult pagedQuery(String sql, Class mappedClass, int page, int rows, Object... sqlArgs)
在spring配置文件中定义:
@Resource(name = "jdbcTemplate")
private JdbcTemplageSupport jdbcTemplate;// 注入JDBC模板
使用方式
@RequestMapping("getDatagridJson2")
@ResponseBody
public PageResult getDatagridJson2(@ModelAttribute DatagridDemoParam datadgridDemoParam) throws IOException {
PageResult page = jdbcDemoService.pagedQuery(datadgridDemoParam);
return page;
}
参考代码:
PageParam
package com.fhzz.core.vo;
/**
* @author: YangYi
* @CreateTime: 2018年2月13日 上午11:13:49
* @Copyright: FHZZ
*/
public class PageParam{
private int page = 1; // 当前页码
private int rows = 10; // 每页的记录数
private int startIndex;// SQL查询起始INDEX
private int endIndex;// SQL查询结束INDEX
private void calculateIndex() {
this.startIndex = (page - 1) * rows;
this.endIndex = startIndex + rows;
}
public PageParam() {
super();
this.calculateIndex();
}
public PageParam(int page, int rows) {
super();
this.page = page;
this.rows = rows;
this.calculateIndex();
}
public void setPage(int page) {
this.page = page;
this.calculateIndex();
}
public void setRows(int rows) {
this.rows = rows;
this.calculateIndex();
}
@Override
public String toString() {
return "PageParam [page=" + page + ", rows=" + rows + "]";
}
public int getStartIndex() {
return startIndex;
}
public int getEndIndex() {
return endIndex;
}
public int getPage() {
return page;
}
public int getRows() {
return rows;
}
}
DatagridDemoParam
public class DatagridDemoParam extends PageParam {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date startTime;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date endTime;
private String name;
private Integer countNum;
PageResult
package com.fhzz.core.vo;
import java.io.Serializable;
import java.util.List;
/**
*
* @author: YangYi
* @CreateTime: 2018年2月13日 下午12:22:10
* @Copyright: FHZZ
*/
public class PageResult implements Serializable {
private static final long serialVersionUID = -1750386840274995765L;
private long total; // 总记录数
private List rows; // 查询出的结果数
public PageResult() {
super();
}
public PageResult(long total, List rows) {
super();
this.total = total;
this.rows = rows;
}
@Override
public String toString() {
return "PageResult [total=" + total + ", rows=" + rows + "]";
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List> getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
JdbcTemplageSupport
package com.fhzz.core.dao.impl.support;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.fhzz.core.vo.PageParam;
import com.fhzz.core.vo.PageResult;
/**
* @author: YangYi
* @CreateTime: 2018年2月13日 上午11:35:19
* @Copyright: FHZZ
*/
public class JdbcTemplageSupport extends JdbcTemplate {
/**
*
* @param sql
* 查询语句
* @param sqlArgs
* 查询参数List,顺序需要与sql中的?一一对应
* @param mappedClass
* 查询的结果返回的封装CLASS
* @param pageParam
* page的参数,包括pageIndex,pageSize
* @return
*/
public PageResult pagedQuery(String sql, List