标签:通过 use nts int start 记录 where ash Map集合 autowire
首先我们要清楚java分页的思路
第一我们要明白前端页面需要向java后台传递当前页码数以及每页显示多少条数据
第二java后台代码需要向前端页面传递每页显示的数据,以及总条数以及总页数
代码如下:
首先我们要创建一个分页类用来存储数据
public class PageObject<T> implements Serializable {
private static final long serialVersionUID = 1L;
/** 当前页的页码值 */
private Integer pageCurrent = 1;
/** 页面大小 */
private Integer pageSize = 10;
/** 总行数(通过查询获得) */
private Integer rowCount = 0;
/** 总页数(通过计算获得) */
private Integer pageCount = 0;
/** 当前页记录 */
private List<T> records;
public Integer getPageCurrent() {
return pageCurrent;
}
public void setPageCurrent(Integer pageCurrent) {
this.pageCurrent = pageCurrent;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getRowCount() {
return rowCount;
}
public void setRowCount(Integer rowCount) {
this.rowCount = rowCount;
}
public Integer getPageCount() {
pageCount = rowCount / pageSize;
if (rowCount % pageSize != 0) {
pageCount++;
}
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
@Override
public String toString() {
return "PageObject [pageCurrent=" + pageCurrent + ", pageSize=" + pageSize + ", rowCount=" + rowCount
+ ", pageCount=" + pageCount + ", records=" + records + "]";
}
}
Controller层方法:
@RequestMapping("/getLog")
@ResponseBody
public PageObject<SysLog> getLog(@RequestBody Map<String, Object> param) throws ParseException {
System.out.println("Map集合参数" + param);
String user = (String) param.get("user");
String date = (String) param.get("date");
Date parse=null;
if(date!=null) {
SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
parse= dFormat.parse(date); // Date date = (Date) param.get("date");
}
System.out.println("解析后日期格式" + parse);
int pageCurrent = Integer.parseInt("" + param.get("pageCurrent"));
int pageSize = Integer.parseInt("" + param.get("pageSize"));
if (pageCurrent == 0) {
pageCurrent = 1;
}
if (pageSize == 0) {
pageSize = 10;
}
return logService.getLog(pageCurrent, pageSize, user, parse);
}
Service层代码:
@Autowired
SysLogDao logdao;
/***
* 根据日期或者用户查询
*/
@Override
public PageObject<SysLog> getLog( int pageCurrent, int pageSize,String user,Date date) {
PageObject<SysLog> pageObject = new PageObject<SysLog>();
// 设置当前页
pageObject.setPageCurrent(pageCurrent);
// 设置每页显示的条数
pageObject.setPageSize(pageSize);
// 设置总记录数
Map<String, Object> map=new HashMap<String, Object>();
map.put("user", user);
map.put("date", date);
int count = logdao.getLogCount(user,date);
System.out.println("=============");
System.out.println("总条数"+count);
pageObject.setPageCount(count);
// 设置总页数
int page = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
pageObject.setRowCount(page);
// 设置每页显示数据集合
// 开始的记录数
int start = (pageCurrent - 1) * pageSize;
System.out.println(start+"-"+pageSize);
List<SysLog> byPage = logdao.getLog( start, pageSize,user,date);
pageObject.setRecords(byPage);
System.out.println(start+"-"+byPage);
return pageObject;
}
Mapper.xml代码如下:
<!-- 查询总条数 -->
<select id="getLogCount" resultType="int">
select count(*) from sys_login_log
<where>
<if test="user !=null">
and login_user=#{user}
</if>
<if test="date !=null ">
and log_date=#{date}
</if>
</where>
</select>
<!-- 查询每页显示条数 -->
<select id="getLog" resultType="SysLog" >
select * from sys_login_log
<where>
<if test="user!=null">
and login_user=#{user}
</if>
<if test="date !=null ">
and log_date=#{date}
</if>
</where>
limit #{start},#{pageSize}
</select>
标签:通过 use nts int start 记录 where ash Map集合 autowire
原文地址:https://www.cnblogs.com/zhangxiaozhen/p/10545022.html