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
复制代码

posted on   ~码铃薯~  阅读(286)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示