一、分页
1.1、数据库的分页:
select * from user limit 0,2;//显示0到2的页数(记录数),如果只给定一个参数,则从自动0开始查找记录数,#[0 n]
1.2、Mybatis实现查询:
方式一:通过mapper.xml文件的形式
方式二:通过Java代码,sqlSession.selectList()等方式实现查询
1.3、Mybatis实现分页查询:
a.方式一:通过mapper.xml,搭配map实现分页查询
sqlsession对象创建类:
package com;//import jdk.internal.loader.Resource; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MyBatisUtils { private static SqlSessionFactory sqlSessionFactory;//提升作用域, 才能用在public static SqlSession getSqlSession()方法中 static{ try { // a.以流的形式加载核心配置文件mybatis-config.xml; // b.再创建sqlsessionfactory对象,并通过qlsessionfactory对象创建sqlsession会话 InputStream inputStream = null; String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //使用MyBatis第二步:通过sqlsessionfactory获取sqlsession对象: public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }
mapper.xml接口实现类:
parameterType="map" 无需获取key值,会自动查找到map的value,并传入到 #{startindex},#{pagesize}形参中
<select id="getuserBylimit" parameterType="map" resultType="entity.user"> select id,name,sex,age,location from mybatis.user limit #{startindex},#{pagesize} </select>
接口定义的方法:
package main.Dao; import entity.user; import java.util.HashMap; import java.util.List; public interface userDao { List<user> getuserBylimit(HashMap hashMap); }
测试类:
import main.Dao.userDao; import entity.user;import org.apache.ibatis.session.SqlSession; import org.junit.jupiter.api.Test; import com.MyBatisUtils; import java.util.HashMap; import java.util.List; public class userDaotest { @Test public void getUserBylimit() { try{ SqlSession sqlSession = MyBatisUtils.getSqlSession();//获取session会话 userDao userDao = sqlSession.getMapper(main.Dao.userDao.class);//拿到接口实现类,即userMapper.xml文件 HashMap hashMap = new HashMap<String,Integer>(); hashMap.put("startindex",0); hashMap.put("pagesize",2); List<user> user=userDao.getuserBylimit(hashMap);//执行userMapper.xml里面的getuserlist()方法 for (user u:user) { System.out.println(u); } }catch(NullPointerException E){ E.printStackTrace(); System.out.println(E); } } }
执行结果:
user{id=1, name='李壵', age=23, sex=男, location='西南大学'}
user{id=2, name='罗子春', age=24, sex=男, location='临沧'}
b.方式二:通过Java代码RowBounds分页,不再使用SQL实现分页
接口定义的方法:
package main.Dao; import entity.user; import java.util.List; public interface userDao { List<user> getuserByRowbands(); }
mapper.xml:
<?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="main.Dao.userDao"> <select id="getuserByRowbands" resultType="entity.user"> select id,name,sex,age,location from mybatis.user </select> </mapper>
测试类:
import main.Dao.userDao; import entity.user; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.SqlSession; import org.junit.jupiter.api.Test; import com.MyBatisUtils; import java.util.HashMap; import java.util.List; public class userDaotest { @Test public void getuserByRowbands() { RowBounds rowBounds = new RowBounds(1,3); SqlSession sqlSession = MyBatisUtils.getSqlSession();//获取session会话 List<user> users = sqlSession.selectList("Dao.userMapper.getuserByRowbands");//获取接口类的方法getUserByRowbands()
for (user user : users) { System.out.println(user); } } }
运行结果:
user{id=1, name='李壵', age=23, sex=男, location='西南大学'}
user{id=2, name='罗子春', age=24, sex=男, location='临沧'}