Paging function mybatis framework

Demand Description: Paging achieve increased results list for a query user list of user management functions in descending order according to creation time

/ **
* Requirements NOTE: Paging achieve increased results list in descending order according to the creation time for a query user list function of user management
* @param roleids
* @return
* /
public List <the 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 Page 
2          @Test
 . 3          public  void testGetUserListByPage () {
 . 4              the SqlSession SQLSESSION = null ;
 . 5              String USERCODE = "" ;
 . 6              String the userName = "" ;
 . 7              Integer = userRole. 3 ;
 . 8              Integer = the pageSize. 5 ;
 . 9              Integer The currentPage 0 =; // MySQL database start default starting from 0 
10              List <the User> = userListShow new new the ArrayList <the User> ();
 . 11              the 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     

operation result:

 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

Guess you like

Origin www.cnblogs.com/dongyaotou/p/12080477.html