mybatis分页
一、mybatis两种分页实现:
a):将分页方法中的当前页、和一页显示的记录数封装到Map中
public List<User> selectAllUsersByPage(int currentPage, int pageSize) { SqlSession sqlSession=null; List<User> userList = null; try { sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); Map<String, Integer> map = new HashMap<String,Integer>(); map.put("index", (currentPage-1)*pageSize); map.put("pageSize", pageSize); userList = sqlSession.selectList("edu.aeon.mybatis.entity.UserMapper.selectAllUsersByPage",map); } catch (IOException e) { e.printStackTrace(); } return userList; }
<select id="selectAllUsersByPage" resultType="User" parameterType="java.util.Map"> select uid,uname,upw from user limit #{index},#{pageSize} </select>
测试代码:
public static void testSelectAllUsersByPage(){ UserDao userDao =new UserDaoImpl(); List<User> userList = userDao.selectAllUsersByPage(1,2); for(User user:userList){ System.out.println(user); } }
b):通过RowBounds实现、这种方式实现分页无需修改查询SQL语句
public List<User> selectAllUsersByPage1(int currentPage, int pageSize) { SqlSession sqlSession=null; List<User> userList = null; try { sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); RowBounds rowBounds = new RowBounds((currentPage-1)*pageSize,pageSize); userList = sqlSession.selectList("edu.aeon.mybatis.entity.UserMapper.selectAllUsers",null,rowBounds); } catch (IOException e) { e.printStackTrace(); } return userList; }
<select id="selectAllUsers" resultType="User"> select uid,uname,upw from user </select>
测试代码:
public static void testSelectAllUsersByPage1(){ UserDao userDao =new UserDaoImpl(); List<User> userList = userDao.selectAllUsersByPage1(1,2); for(User user:userList){ System.out.println(user); } }
当然还有另外一种就是自己封装个page类,既然我们能用更简单的方式去实现同样的功能,为什么我们还要去多此一举呢?我认为没必要!所以此处我将分页分为两种!
注解分页:
package edu.aeon.mybatis.dao; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import edu.aeon.mybatis.entity.User; public interface UserDao { @Insert("insert into user(uname,upw) values(#{uname},#{upw})") int add(User user); @Delete("delete from user where uid=#{uid}") int delete(int uid); @Update("update user set uname=#{uname},upw=#{upw} where uid=#{uid}") int update(User user); @Select("select uid,uname,upw from user where uid=#{uid}") User selectUserById(int uid); @Select("select uid,uname,upw from user") List<User> selectAllUsers(); @Select("select uid,uname,upw from user limit #{index},#{pageSize}") List<User> selectAllUsersByPage(@Param("index")int currentPage,@Param("pageSize")int pageSize); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="database/database_mysql.properties"/> <typeAliases> <!-- 目的:简化配置文件的繁琐性、以前由包名+类名的形式均可变成所指定的别名类型、或者通过包名所配置后的类类型 --> <!-- 第一种方式:为type属性中的内容指定引用别名为alias中的内容、指定之后所有关于type中的类型均可由alias内容代替 --> <!-- <typeAlias type="edu.aeon.mybatis.entity.User" alias="User"/> --> <!-- 第二种方式:通过包名配置、配置后所有该包下的类型均可有类名代替、不需要由以前的包名+类名的形式 --> <package name="edu.aeon.mybatis.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers>
<!--此处需注意--> <mapper class="edu.aeon.mybatis.dao.UserDao"/> </mappers> </configuration>
package edu.aeon.mybatis.test; import java.io.IOException; import java.util.List; import org.apache.ibatis.session.SqlSession; import edu.aeon.mybatis.dao.UserDao; import edu.aeon.mybatis.entity.User; import edu.aeon.mybatis.utils.MyBatisUtils; public class Test { public static void testInsertUser(){ try { SqlSession sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); UserDao userDao = sqlSession.getMapper(UserDao.class); User user = new User(); user.setUname("testAnnotation"); user.setUpw("testAnnotation"); int flag = userDao.add(user); sqlSession.commit(); if(flag>0){ System.out.println("插入数据成功!"); }else{ System.out.println("插入数据失败!"); } } catch (IOException e) { e.printStackTrace(); } } public static void testdeleteUserById(){ try { SqlSession sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); UserDao userDao = sqlSession.getMapper(UserDao.class); int flag = userDao.delete(5); sqlSession.commit(); if(flag>0){ System.out.println("删除数据成功!"); }else{ System.out.println("删除数据失败!"); } } catch (IOException e) { e.printStackTrace(); } } public static void testUpdateUserById(int uid){ try { SqlSession sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); UserDao userDao = sqlSession.getMapper(UserDao.class); User user = userDao.selectUserById(uid); user.setUname("testUpdate1"); user.setUpw("testUpdate1"); int flag = userDao.update(user); sqlSession.commit(); if(flag>0){ System.out.println("更新数据成功!"); }else{ System.out.println("更新数据失败!"); } } catch (IOException e) { e.printStackTrace(); } } public static void testSelectUserById(int uid){ SqlSession sqlSession = null; try { sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); UserDao userDao = sqlSession.getMapper(UserDao.class); User user = userDao.selectUserById(uid); System.out.println(user); } catch (IOException e) { e.printStackTrace(); } } public static void testSelectAllUsers(){ try { SqlSession sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); UserDao userDao = sqlSession.getMapper(UserDao.class); List<User> userList = userDao.selectAllUsers(); for(User user:userList){ System.out.println(user); } } catch (IOException e) { e.printStackTrace(); } } public static void testSelectAllUsersByPage(int index,int pageSize){ try { SqlSession sqlSession = MyBatisUtils.getSqlSesion("mybatis/mybatis.xml"); UserDao userDao = sqlSession.getMapper(UserDao.class); List<User> userList = userDao.selectAllUsersByPage((index-1)*pageSize, pageSize); for(User user:userList){ System.out.println(user); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { /*testSelectAllUsers(); //ok...*/ /*testInsertUser(); //ok...*/ /*testdeleteUserById(); //ok...*/ /*testUpdateUserById(4);ok...*/ /*testSelectUserById(2);ok...*/ testSelectAllUsersByPage(2,2); } }