mybatis框架的分页功能

需求说明:为用户管理之查询用户列表功能增加分页实现      列表结果按照创建时间降序排列

/**
* 需求说明:为用户管理之查询用户列表功能增加分页实现 列表结果按照创建时间降序排列
* @param roleids
* @return
*/
public List<User> getUserListByPage(@Param("usercode")String usercode,@Param("userName")String userName,@Param("userRole")Integer userRole,@Param("pageSize")Integer pageSize,@Param("currentPage")Integer currentPage);

<!--分页查询 -->
<select id="getUserListByPage" resultMap="userList" >
  select * from smbms_user a,smbms_role r where 1=1 and a.userrole=r.id
    <if test=" userName!=null and userName!='' "> and a.userName like concat('%',#{userName},'%') </if>
    <if test=" userRole!=null and userRole!='' "> and a.userrole=#{userRole} </if>
  order by a.creationdate desc limit #{currentPage},#{pageSize}
</select>

<!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
<resultMap type="User" id="userList">
  <result property="id" column="id"/>
  <result property="userCode" column="userCode"/>
  <result property="userName" column="userName"/>
  <result property="phone" column="phone"/>
  <result property="birthday" column="birthday"/>
  <result property="gender" column="gender"/>
  <result property="userRole" column="userRole"/>
  <!-- <result property="userRoleName" column="roleName"/> -->
</resultMap>

 1     //mybatis分页
 2         @Test
 3         public void testGetUserListByPage(){
 4             SqlSession sqlSession = null;
 5             String usercode="";
 6             String userName="";
 7             Integer userRole=3;
 8             Integer pageSize=5;
 9             Integer currentPage=0;//mysql数据库默认起步是从0开始
10             List<User> userListShow=new ArrayList<User>();
11             try {
12         
13                 sqlSession = MyBatisUtil.createSqlSession();
14                 userListShow = sqlSession.getMapper(UserMapper.class).getUserListByPage(usercode,userName,userRole,pageSize,currentPage);
15                 
16             } catch (Exception e) {
17                 // TODO: handle exception
18                 e.printStackTrace();
19             }finally{
20                 MyBatisUtil.closeSqlSession(sqlSession);
21             }
22             for(User user: userListShow){
23                 logger.debug("testGetUserListByPage UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
24             }
25         
26                 
27         }
28     

运行结果:

 1 [DEBUG] 2019-12-22 17:53:33,894 cn.smbms.dao.user.UserMapper.getUserListByPage - ==>  Preparing: select * from smbms_user a,smbms_role r where 1=1 and a.userrole=r.id and a.userrole=? order by a.creationdate desc limit ?,? 
 2 [DEBUG] 2019-12-22 17:53:33,911 cn.smbms.dao.user.UserMapper.getUserListByPage - ==> Parameters: 3(Integer), 0(Integer), 5(Integer)
 3 [DEBUG] 2019-12-22 17:53:33,925 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4a738d08]
 4 [DEBUG] 2019-12-22 17:53:33,925 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@4a738d08]
 5 [DEBUG] 2019-12-22 17:53:33,925 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1249086728 to pool.
 6 [DEBUG] 2019-12-22 17:53:33,926 cn.smbms.dao.user.UserMapperTest - testGetUserListByPage UserCode: sunxing and UserName: 孙兴and userRole:3
 7 [DEBUG] 2019-12-22 17:53:33,926 cn.smbms.dao.user.UserMapperTest - testGetUserListByPage UserCode: zhangchen and UserName: 张晨and userRole:3
 8 [DEBUG] 2019-12-22 17:53:33,926 cn.smbms.dao.user.UserMapperTest - testGetUserListByPage UserCode: dengchao and UserName: 邓超and userRole:3
 9 [DEBUG] 2019-12-22 17:53:33,926 cn.smbms.dao.user.UserMapperTest - testGetUserListByPage UserCode: zhaoyan and UserName: 赵燕and userRole:3
10 [DEBUG] 2019-12-22 17:53:33,926 cn.smbms.dao.user.UserMapperTest - testGetUserListByPage UserCode: sunlei and UserName: 孙磊and userRole:3

猜你喜欢

转载自www.cnblogs.com/dongyaotou/p/12080477.html