Mybatis 解决持久层问题,封装了jdbc细节,提高开发效率。
JDBC编程
public void findStudent(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据的基本信息url,username,password
String url = "jdbc:mysql://localhost:3306/springdb";
String username = "root";
String password = "123456";
//创建连接对象
conn = DriverManager.getConnection(url,username,password);
//保存查询结果
List<Student> stuList = new ArrayList<>();
//创建Statement,用来执行sql语句
stmt = conn.createStatement();
//执行查询,创建记录集
rs = stmt.executeQuery("select* from student");
while(rs.next()){
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setAge(rs.getInt("age"));
//从数据库中取出数据转化为student对象,封装到List集合
stuList.add(stu);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
//关闭资源
if(rs != null){
rs.close();
}
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Mybatis框架
解决的问题
关注于SQL语句处理,减轻JDBC的复杂性
包结构
src
main
java
包名
xxxDao
xxxDao.xml文件(为xxxDao的mapper映射文件)
resources(需要右键设为资源根目录)
mybatis.xml(mybatis的主配置文件)
database.properties
test
包名
测试代码
pom.xml
从前到后的演进思路:
1.测试代码实现大部分逻辑
//测试mybatis执行sql语句
@Test
public void testSelectStudentById() throws IOException {
//调用mybatis某个对象的方法执行mapper文件中的sql语句
//mybatis核心类:SqlSessionFactory
//1.定义mybatis主配置文件的位置,从类路径开始的相对路径
String config = "mybatis.xml";
//2.读取主配置文件。使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建sqlSessionFactory对象,使用SqlSessionFactoryBuilder类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession session = factory.openSession();
//5.指定要执行的sql语句的id sql的id=namespace+"."+<select|update|insert|delete>标签的id属性值
String sqlId = "com.weibo.dao.StudentDao"+"."+"selectStudentById";
//6.通过SqlSession的方法执行sql语句
Student student = session.selectOne(sqlId);
System.out.println(student);
//7.关闭sqlSession的对象
session.close();
}
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置数据源,创建connection对象-->
<dataSource type="POOLED">
<!--
driver:驱动的内容
连接数据库的url
-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<!--
指定其他mapper文件的位置,找到其他mapper文件目的是找到其他文件的sql语句
mapper的resources属性指定mapper文件的路径,从target/classes开始的
使用注意:resource="mapper文件的路径,使用/作为分割"
一个mapper resource指定一个mapper文件
-->
<mappers>
<mapper resource="com/weibo/dao/StudentDao.xml"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!--约束文件 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"
作用:定义和限制当前文件中可以使用的标签和属性,出现标签的顺序
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--mapper是根标签
namespace属性必须有值,不能为空,唯一值推荐使用Dao接口的全限定名称,
作用:参与识别sql语句的作用
mapper里面可以写insert、update、delete、select等标签,分别执行各自的语句
-->
<mapper namespace="com.weibo.dao.StudentDao">
<!--查询一个学生Student
id:要执行的sql语句唯一标识,是一个自定义标识符,推荐使用dao接口中的方法名称
resultType:告诉mybatis,执行sql语句后,把数据赋值给哪个类型的java对象
resultType的值现在使用的java对象的全限定名称
-->
<select id="selectStudentById" resultType="com.weibo.domain.Student">
select id,name,email,age from student where id = 1001
</select>
</mapper>
特别注意,语句后面不能写分号
//5.指定要执行的sql语句的id sql的id=namespace+"."+<select|update|insert|delete>标签的id属性值
String sqlId = "com.weibo.dao.StudentDao"+"."+"selectStudentById2";
//6.通过SqlSession的方法执行sql语句
Student student = session.selectOne(sqlId,1001);
插入数据
@Test
public void testInsertStudent() throws IOException {
//调用mybatis某个对象的方法执行mapper文件中的sql语句
//mybatis核心类:SqlSessionFactory
//1.定义mybatis主配置文件的位置,从类路径开始的相对路径
String config = "mybatis.xml";
//2.读取主配置文件。使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//3.创建sqlSessionFactory对象,使用SqlSessionFactoryBuilder类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
//4.获取SqlSession对象
SqlSession session = factory.openSession();
//5.指定要执行的sql语句的id sql的id=namespace+"."+<select|update|insert|delete>标签的id属性值
Student student = new Student(1004,"李思思","lisisi@qq.com",22);
String sqlId = "com.weibo.dao.StudentDao"+"."+"insertStudent";
int rows = session.insert(sqlId,student);
System.out.println("添加了"+rows+"行学生");
//mybatis默认执行的sql语句是手工提交模式,做insert、delete、update后需要提交事务
session.commit();
//7.关闭sqlSession的对象
session.close();
}
<!--
如果传入给mybatis的是一个java对象,使用#{属性名} 获取此属性的值,会放到占位符里,mybatis执行此属性的对应的getxxx()方法
-->
<!-- <insert id="insertStudent">
insert into student values(1003,"李峰","lifeng@qq.com",26)
</insert>-->
<insert id="insertStudent">
insert into student values(#{id},#{name},#{email},#{age})
</insert>
Mybatis对象分析
Resources:mybatis框架中对象,一个作用:读取主配置信息
InputStream inputStream = Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder:负责创建SqlSessionFactory对象
SqlSessionFactory重要对象
SqlSessionFactory是重量级对象:创建此对象,需要更多的资源和时间。
SqlSessionFactory是一个接口
DefaultSqlSessionFactory是SqlSessionFactory的真正实现类
openSession():获取一个默认的SqlSession对象,默认需要手工提交事务
openSession(boolean):boolean参数表示是否自动提交事务
true:创建一个自动提交事务的SqlSession
false:等同于没有参数的openSession
SqlSession对象
SqlSession对象是通过SqlSessionFactory获取的,SqlSession本身是接口,作用是提供了大量的执行sql语句的方法
selectOne:执行sql语句,最多得到一行记录,多于一行是会报错
selectList:执行sql语句,返回多行数据
slectMap:执行sql语句,得到一个map结果
insert:执行insert语句
update:执行update语句
delete:执行删除语句
commit:提交事务
rollback:回滚事务
注意SqlSession不是线程安全的,使用步骤:
1)在方法的内部,执行sql语句之前,先获取SqlSession对象
2)调用SqlSession的方法,执行Sql语句
3)关闭SqlSession对象,执行SqlSession.close();
SqlSession对象属于局部变量,不会被线程共享
Dao代理
/*
* com.weibo.dao.StudentDao
*
* 1)studentDao:通过反射能够得到 全限定名称
* dao是StudentDao类型的 全限定名称com.weibo.dao.StudentDao
* 2)selectById: studentDao中的方法名称 方法名称是mapper文件中标签的id
* 通过StudentDao.selectById()得到 sqlId = "com.weibo.dao.StudentDao.selectById"
*
* 3)确定调用SqlSession的哪个方法
* 1.根据dao接口方法的返回值,如果返回一个对象,例如Student,调用selectOne()
* 如果dao接口的方法返回的是List,调用SqlSession的selectList();
* 2.根据mapper文件中的标签,如果标签是<insert>,调用SqlSession.insert()
*
* mybatis框架,使用dao的方法调用能够确定执行sql语句的必要信息,mysql简化dao对象的实现
* 由mybatis框架在程序执行期间,根据你的Dao接口,创建一个内存的接口的实现类对象
*
* mybatis把这个技术叫做dao技术(动态代理,dao的动态代理)
*
* dao代理技术:由mybatis创建StudentDao接口的实现类StudentDaoImpl,使用框架创建的实现类来代替手工实现类
*
* 使用dao的代理要求:
* 1.mapper文件中的namespace:必须是dao接口的全限定名称
* 2.mapper文件中的标签d是dao接口中的方法名称一致
*
* */
mybatis提供代理:mybatis创建Dao接口的实现类对象,完成对sql地执行。mybatis创建一个对象代替你的dao实现类功能
#### mybatis代理实现方式
使用SqlSession对象的方法getMapper(dao.class)
例如:现有studentDao接口
```
Sqlsession session = MyBatisUtils.getSession();
studentDao dao = session.getMapper(studentDao.class);
Student student = dao.selectById(1001);
--------------------------------------------------------------------
studentDao dao = session.getMapper(studentDao.class);
等同于
StudentDao dao = new StudentDaoImpl();
```
###
理解参数
理解参数是:通过java程序把数据传入到mapper文件中的sql语句。参数主要是指dao接口方法的形参。
parameterType
parameterType表示参数的类型,指定dao方法的形参数据类型,这个参数的数据类型是给mybatis使用,mybatis在给sql语句的参数赋值时使用。
传递简单类型参数
接口中的方法的参数只有一个简单类型(java基本类型和String),使用占位符#{任意字符},和方法的参数名无关。
<select id="selectByEmail" resultType="com.weibo.domain.Student">
select * from Student where email = #{studentEmail}
</select>
dao接口方法有多个简单类型参数
@Param:命名参数,在方法的形参前面使用,定义参数名,这个名称可以用在mapper文件中。
/*
* 多个简单类型,使用@Param命名参数,注解是mybatis提供的
* 位置:在形参定义的前面
* 属性:value自定义的参数名称
* */
List<Student> selectByNameOrAge(@Param("myname") String name, @Param("myage") Integer age);
<!--
多个简单类型参数,
当使用了@Param命名后,例如@Param("myname")
在mapper中使用#{命名的参数},例如#{myname}
-->
<select id="selectByNameOrAge" resultType="com.weibo.domain.Student">
select * from Student where name=#{myname} or age=#{myage}
</select>
使用对象作为参数
方法的形参是一个java对象,这个java对象表示多个参数,使用对象的属性值作为参数使用
/*
* 一个java对象作为参数(对象有属性,每个属性有get和set方法)
* */
List<Student> selectByObject(Student student);
<!--
一个java对象作为方法的参数,使用对象的属性作为参数值使用
简单语法:#{属性名} mybatis调用此属性的getxxx()方法获取属性值
-->
<select id="selectByObject" resultType="com.weibo.domain.Student">
select * from Student where name=#{name} or age=#{age}
</select>
dao接口中多个简单类型的参数,使用位置
参数:dao接口中方法的形参列表,从左到右参数的位置是0,1,2…
语法格式:#{arg0},#{arg1}
使用map作为参数
占位符
#和$
#占位符(比较推荐)
语法:#{字符}
mybatis处理#{}使用jdbc对象时PrepareStatement对象
特点:
使用prepareStatement对象,执行sql语句,效率高
使用prepareStatement对象,能避免sql注入,sql语句更加安全
#{}常常作为列值使用的,位于等号右侧。#{}位置的数据是和数据类型有关的
$占位符
语法:${字符}
使用和#{字符}用法一样
表 示 字 符 串 连 接 , 把 s q l 语 句 的 其 他 内 容 和 {}表示字符串连接,把sql语句的其他内容和 表示字符串连接,把sql语句的其他内容和{}内容使用字符串连接的方式连接在一起。
select * from Student where email = ${studentEmail};
相当于:String sql = "select * from Student where email = "+"1001";
mybatis创建Statement对象,执行sql语句
Statement stmt = conn.createStatement(sql);
ResultSet rs = stmt.executeQuery();
${}特点:
1.使用Statement对象,执行sql语句,效率低(sql语句每次使用时都需要重新编译,#{}只需要编译一次。
2.${}占位符的值,采用字符串连接方式,有sql注入风险,有代码安全问题。
3.${}数据是原样的,不会区分数据类型。
原样替换,字符串不会给加引号,需要自己加单引号。 没有#{}好用,#{}会自动进行数据类型转换。
用来做列名或表名来使用,在能保证数据安全的情况下使用${}
<!-- 按照列名排序-->
<select id="selectStudentOrderByColName" resultType="com.weibo.domain.Student">
select* from student where name = #{myname} order by ${mycolName}
</select>
/*按照列名排序*/
List<Student> selectStudentOrderByColName(@Param("myname") String name,@Param("mycolName") String colName);
封装Mybatis输出结果
封装输出结果:Mybatis执行sql语句,得到ResultSet,转为java对象。
ResultType
1. ResultType表示自定义对象
是一个属性,在执行select时使用,作为标签的属性出现。
表示结果类型,mysql执行sql语句,得到java对象的类型,它的值有两种:
1.java类型的全限定名称
2.使用别名
mybatis会做以下操作:
1.调用com.weibo.domain.Student的无参构造方法,创建对象
使用反射创建对象Student student = new Student();
2.同名的列赋值给同名的属性
student.setId(rs.getId(“id”));
student.setName(rs.getName(“name”));
3.得到java对象,如果dao接口返回值是List集合,mybatis把student对象放入到List集合
2.ResultType表示简单类型
long countStudent();
<!--执行sql值,得到的是一个值-->
<select id="countStudent" resultType="java.lang.Long">
select count(*) from student
</select>
@Test
public void testStudentCount(){
SqlSession session = MybatisUtil.getSqlSession();
StudentDao studentDao = session.getMapper(StudentDao.class);
long rows = studentDao.countStudent();
System.out.println(rows);
session.commit();
session.close();
}
3.ResultType表示一个map结构
<!--
执行select返回一个map结构,执行sql语句,将ResultSet转化为Map
列名作为map的key 列值作为map的value
dao接口返回是一个map, sql语句最多只能返回一行,返回多行是错误的。
-->
<select id="selectMap" resultType="java.util.HashMap">
select id,name from student where id = #{stuid}
</select>
ResultMap
结果映射,自定义列名和java对象属性的对应关系,常用在列名和属性名不同的情况下。
ResultMap和ResultType不能同时使用,二选一。
用法:
先定义resultMap标签,指定列名和属性名称对应的关系
在select标签使用resultMap属性,指定上面定义的resultMap的id值
<!--使用resultMap定义列和属性的关系-->
<!--定义resultMap id:给映射关系起个名称(唯一值) type:java的全限定名称-->
<resultMap id="customerobject" type="com.weibo.vo.CustomerObject">
<!--
定义列名和属性名的对应
主键类型使用id标签
-->
<id column="id" property="cid" />
<result column="name" property="cname" />
<!--列名和属性名相同,不用定义-->
</resultMap>
<select id="selectById2" resultMap="customerobject">
select * from student where id=#{stuid}
</select>
自定义别名
mybatis提供的对java类型定义简短的好记的名称
自定义别名的步骤:
1.在mybatis主配置文件,使用typeAliase标签声明别名
2.在mapper文件中,resultType=“别名”
<typeAliases>
<!--
第一种语法格式:
type:java类型的全限定名称(自定义类型)
alias:自定义别名
-->
<typeAlias type="com.weibo.domain.Student" alias="stu"></typeAlias>
</typeAliases>
在主配置文件里面
优点:别名可以自定义
缺点:每个类型必须单独定义
第二种方式
<!--
第二种方式:
name:包名
mybatis会把这个包中所有类名作为别名(不用区分大小写)
优点:使用方便,一次给多个类定义别名
缺点:别名不能自定义,必须是类名。当不同包中存在相同的类时会产生冲突
-->
<package name="com.weibo.domain"/>
<select id="selectById" resultType="student">
select * from Student where id=#{studentId}
</select>
列名和java对象属性名称不一样解决方式
1.使用ResultMap:自定义列名和属性名称对应关系
2.使用ResultType:使用列别名,让别名和java属性名称一样
模糊查询
like
第一种方式,在java程序中,把like内容组装好,把这个内容传入到like中
<!--like第一种方式-->
<select id="selectLikeOne" resultType="com.weibo.domain.Student">
select * from student where name like #{name}
</select>
//like第一种方式
List<Student> selectLikeOne(@Param("name") String name);
第二种方式. 在sql语句中组织like内容
sql语句格式:”where name like “%” #{} “%”"
<!--like第二种方式-->
<select id="selectLikeOne" resultType="com.weibo.domain.Student">
select * from student where name like "%" #{name} "%"
</select>
第四章 动态sql
什么是动态sql: 同一个dao的方法,根据不同的条件,表示不同的sql语句,主要是where条件有变化
使用mybatis提供的标签,实现动态sql的能力,主要讲if、where、foreach、sql
使用动态sql时候,dao方法的形参使用java对象
if标签
语法:
<if test="boolean判断结果">
sql代码
</if>
可以有多个if
在mapper文件中
<select id = "selectStudent" resultType="com.weibo.domain.Student">
select * from student
<if test="条件">
sql语句1
</if>
<if test="条件2">
sql语句2
</if>
</select>
where标签
使用if标签时,容易引起sql语句语法错误,使用where标签解决if产生的语法问题。
使用时where,里面是一个或多个if标签,当有一个if标签判断为true,where标签会转换为where关键字附加到sql语句后面,如果if没有一个条件为真,就忽略里面的where和里面的if
语法:
<where>
<if test="条件1">sql语句</if>
<if test="条件2">sql语句</if>
</where>
<!--where-->
<select id="selectWhere" resultType="com.weibo.domain.Student">
select * from student
<where>
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age > 0">
or age > #{age}
</if>
</where>
</select>
where标签会删除离他最近的or或者and
foreach循环
使用foreach可以循环数组,list集合,一般使用在in语句中。
语法:
<foreach colleaction="集合类型" open="开始字符" close="结束的字符"
item="集合中的成员" separtor="集合成员间的分割符">
#{item的值}
</foreach>
collection:表示循环的对象是数组还是list集合。如果dao接口方法的形参是数组,
collection="array",如果dao接口形参是List,collection = "list"
open:循环开始的字符 sql.append("(");
close:循环结束的字符 sql.close(")");
item:集合成员 Iteger item = idlist.get(i);
separtor:集合成员间的分隔符 sql.separator(",");
#{item}:获取集合成员的值
<!--foreach第一种方式,循环简单类型的foreach-->
<select id="selectForeach" resultType="com.weibo.domain.Student">
select * from student
<if test="list != null and list.size() > 0">
where id in
<foreach collection="list" open="(" close=")" separator="," item="myid">
#{myid}
</foreach>
</if>
</select>
@Test
public void testSelectStudentForeachOne(){
SqlSession session = MybatisUtil.getSqlSession();
StudentDao studentDao = session.getMapper(StudentDao.class);
List<Integer> list = new ArrayList<>();
list.add(1002);
list.add(1004);
list.add(1008);
list.add(1005);
List<Student> students = studentDao.selectForeach(list);
for(Student student1:students){
System.out.println(student1);
}
session.commit();
session.close();
}
<!--foreach第二种方式,循环List<Student>-->
<select id="selectForeach2" resultType="com.weibo.domain.Student">
select * from student
<if test="list != null and list.size() > 0">
where id in
<foreach collection="list" open="(" close=")" separator="," item="stu">
#{stu.id}
</foreach>
</if>
</select>
@Test
public void testSelectStudentForeachTwo(){
SqlSession session = MybatisUtil.getSqlSession();
StudentDao studentDao = session.getMapper(StudentDao.class);
List<Student> list = new ArrayList<>();
list.add(new Student(1004,null,null,null));
list.add(new Student(1005,null,null,null));
List<Student> students = studentDao.selectForeach2(list);
for(Student student1:students){
System.out.println(student1);
}
session.commit();
session.close();
}
代码片段
sql标签标识一段sql片段,可以是表名,几个字段,where条件都可以,可以在其他地方复用sql标签的内容
使用方式:
1)在mapper文件中定义 sql代码片段 部分sql语句
2)在其他位置,使用include标签引用某个代码片段
<!--定义代码片段-->
<sql id="selectStudent">
select* from student
</sql>
<sql id="studentFieldList">
id,name,email
</sql>
<select id="selectIf" resultType="com.weibo.domain.Student">
<include refid="selectStudent"></include>
where
/*test使用对象的属性值来作为条件*/
<if test="name != null and name != ''">
name=#{name}
</if>
<if test="age > 0">
or age=#{age}
</if>
</select>
<!--where-->
<select id="selectWhere" resultType="com.weibo.domain.Student">
select <include refid="studentFieldList"/> from student
<where>
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age > 0">
or age > #{age}
</if>
</where>
</select>
Mybatis配置文件
mybatis配置文件:
1.mybatis主配置文件,提供mybatis全局设置,包含内容日志,数据源,mapper文件位置
2.mapper文件:写sql语句,一个表一个mapper文件
settings部分
settings是mybatis的全局设置,影响整个mybatis运行,这个设置使用默认值
typeAliases设置别名
有两种方式。
environment配置信息
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置数据源,创建connection对象-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
environments:环境标签,在他里面可以配置多个environment
environment:表示一个数据库的连接信息
属性 id 自定义的环境的标识 唯一
属性 transactionManager 事务管理器的类型
属性值:1) JDBC 使用Connection对象,由mybatis自己完成事务的处理
2) MANAGED 管理,表示把事务的处理交给容器实现(由其他软件完成事务的提交,回滚)
dataSource:数据源,创建Connection对象,连接数据库
属性:type 数据源的类型
属性值:1)POOLED,mybatis会在内存中创建PooledDataSource类,管理多个Connection连接对象,使用的连接池
2) UNPOOLED 不使用连接池,mybatis创建一个unPooledDataSource类,每次执行sql语句先创建Connection对象,再执行sql语句
3)JNDI:java的命名和目录服务
使用数据库属性配置文件
1)需要把数据库配置信息放到一个单独文件中,独立管理,这个文件扩展名为properties,使用自定义的key=value格式表示数据
2)在mybatis主配置文件中,使用properties标签引用外部的属性配置文件
3)在使用值的位置,使用${key}获取key对应的value
mapper标签
使用mapper指定其他mapper文件的位置
mapper标签使用的格式有两种常用的方式
<mappers>
<!--第一种方式:resources="mapper文件的路径"
优点:文件清晰,加载的文件是明确的,文件的位置比较灵活
缺点:文件比较多,代码量比较大,管理难度大
-->
<mapper resource="com/weibo/dao/StudentDao.xml"/>
</mappers>
<mappers>
<!--
第二种方式:使用<package>
name:包名,mapper文件所在的包名
特点:把这个包中所有的mapper文件一次加载。
使用要求:
1.mapper文件和dao接口在同一目录
2.mapper文件的dao接口名称完全一致
-->
<package name="com.weibo.dao"/>
</mappers>
PageHelper做数据分页
在select语句后面加上分页的sql内容,如果使用sql数据库,就是在select* from student后面加入limit语句。
使用步骤:
1.加入依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
2.在mybatis主配置文件,环境之前加入plugin声明
在<environment>之前加入
<plugins>
<plugin interceptor = "com.github.pagehelper.PageInterceptor" />
</plugins>
3.在select语句之前,调用PageHelper.startPage(页码,每页大小)