springboot+mybatis中layui的分页
1.各种包需要提前导入注意路径
2.Dao层
public interface UserDao {
/**
* 查询用户信息
* @param map
* @return
*/
List<Map> getUserList(Map map);
/**
* 用户分页
* @return
*/
int getUserCount();
}
3.Service
public interface UserService {
/**
* 获取用户
* @param map
* @return
*/
List<Map> getUserList(Map map);
/**
* 用户分页
* @return
*/
int getUserCount();
}
4.ServiceImpl
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public List<Map> getUserList(Map map) {
int pageNo = map.get("pageNo") == null ? 1 : Integer.valueOf(map.get("pageNo")+"");
int pageSize = map.get("pageSize") == null ? 10 : Integer.valueOf(map.get("pageSize")+"");
map.put("start",(pageNo-1)*pageSize);
map.put("end",pageNo*pageSize+1);
return userDao.getUserList(map);
}
@Override
public int getUserCount() {
return userDao.getUserCount();
}
}
4.Controller
package com.pro.sm.controller.user;
import com.pro.sm.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* className:UserController
* discriptoin:
* author:张晓峰
* createTime:2018-11-23 09:28
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
@ResponseBody
public Object getUserList(@RequestParam Map map){
List<Map> user = userService.getUserList(map);
int count = userService.getUserCount();
System.out.println(count);
Map tempMap = new HashMap();
tempMap.put("code",0);
tempMap.put("msg","");
tempMap.put("count",count);
tempMap.put("data",user);
System.out.println(user);
return tempMap;
}
@RequestMapping("/toList")
public String toList(Map map){
return "user";
}
}
5.Mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pro.sm.dao.user.UserDao">
<select id="getUserList" resultType="map">
select * from (select rownum rn,t.* from tb_user t where rownum < #{end}) a where a.rn > #{start}
</select>
<select id="getUserCount" resultType="int">
select count (*) from tb_user
</select>
</mapper>
6.Html界面
layui.use('table', function(){
var table = layui.table;
table.render({
elem: '#userlist'
,title: '用户数据表'
,height:790
,width:1910
,url: '/sb/user/list'
,toolbar: '#toolbarDemo'
,page: true
,even: true
,size: 'lg'
,cols:[ [
{checkbox: true, fixed: true}
,{field: 'USER_ID', title: '序号', width:300, sort: true, unresize: true ,fixed: 'left'}
,{field: 'USER_NAME', title: '姓名', width:310}
,{field: 'USER_GENDER', title: '性别', width:310,templet:'#SEX'}
,{field: 'USER_AGE', title: '年龄', width:310}
,{field: 'USER_EMAIL', title: '邮箱', width:310}
,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:310}
] ]
,request: {
pageName: 'pageNo',//页码的参数名称,默认:page
limitName: 'pageSize' //每页数据量的参数名,默认:limit
},
limits: [5, 10, 15], //显示
limit: 5 //每页默认显示的数量
})
})