mybatis
搭建Mybatis环境
-
需要导入的依赖文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.xf</groupId> <artifactId>demo1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <!--Mybatis核心--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!--junit测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--Mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.3</version> </dependency> <!--日志--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
-
mybatis-config.xml的配置
<?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"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--引入配置文件--> <mappers> <mapper resource="mappers/userMapper.xml"/> </mappers> </configuration>
-
映射文件的配置
<?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="org.xf.mapper.userMapper"> <!--insertUser--> <insert id="insertUser"> insert into user values (null ,"RUSH","123456",18,"男") </insert> <!--selectUser--> <select id="selectUser" resultType="org.xf.entity.User"> select * from user where id="1001" </select> </mapper>
-
数据库的配置(修改身份验证权限)
-
接口调用
public interface userMapper { int insertUser(); ArrayList<User> selectUser(); }
-
日志的配置以及日志级别
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <param name="Encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m(%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="info" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration>
-
测试
public class MyBatisTest { @Test public void testMyBatis() throws IOException { //加载核心配置文件 InputStream is =Resources.getResourceAsStream("mybatis-config.xml"); //获取sqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is); //获取SqlSession SqlSession sqlSession=sqlSessionFactory.openSession(true);//true表示自动提交事务 //获取mapper的接口对象 userMapper mapper=sqlSession.getMapper(userMapper.class); //测试功能 ArrayList<User> result=mapper.selectUser(); // sqlSession.commit(); System.out.println("result:"+result); } }
-
总结:MyBatis的配置,安装其依赖包,搭建核心配置文件,通过配置映射文件与接口相互调用,即可对数据库进行操作,其中查询功能需要建立实体类,在映射文件中select标签中设置属性resultType的值为实体类的路径。注意查询结果的返回值类型。
MyBatis核心配置文件
-
environment:用于配置数据库环境
<environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments>
MyBatis可配置多个数据库环境,environments中的default属性用于选取多个环境中的一个环境
-
transactionManager:设置事务管理方式,type可选:“JDBC/MANAGED”
JDBC:表示当前环境中,执行SQL时使用的是JDBC中原生事务管理方式,事务的提交或回滚需要手动处理
-
dataSource:配置数据源
属性:type:设置数据源的类型
type=“POOLED|UNPOOLED|JNDI”
POOLED:表示数据库连接池缓存数据库连接
UNPOOLED:表示不使用数据库连接池
JDNI:表示使用上下文中的数据源
-
properties:通过引入的方式配置数据源中的属性值
jdbc.driven=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8 jdbc.username=root jdbc.password=xingfeng66
<properties resource="jdbc.properties"/>
-
typeAliases(类型别名):用于配置映射文件中的resultType中的别名,其位置放置有规则
子节点typeAlias:设置别名,属性有type(指定映射文件中resultType中的包的路径),alias:设置别名,可以为空,不进行设置则以类型名为别名(且不区分大小写)。
<typeAlias type="org.xf.entity.User"/>
子节点package:以包为单位,设置默认的类型别名,且类名不区分大小写
-
mappers:引入映射文件
子节点:mapper,引入单个映射文件
package,引入包下的全部映射文件。(要求:mapper接口所在的包要和映射文件所在的包一致;mapper接口和映射文件的名字一致)
注意resource下创建包的方式
模板文件的添加与测试类的封装
-
模板文件的添加
-
测试类的封装
public class SqlSessionUtils { public static SqlSession getSqlSession(){ SqlSession sqlSession=null; //加载核心配置文件 InputStream is = null; try { is = Resources.getResourceAsStream("mybatis-config.xml"); //获取sqlSessionFactoryBuilder //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //获取SqlSession sqlSession = sqlSessionFactory.openSession(true);//true表示自动提交事务 } catch (IOException e) { e.printStackTrace(); } return sqlSession; } }
MyBatis获取参数的两种方式
-
MyBatis获取参数的两种方式:${} #{}
${}:本质是字符串拼接:拼接sql时,若为字符串类型或日期类型的字段进行赋值时,需要手动添加单引号
#{}的本质是占位符赋值:拼接sql时,若为字符串类型或日期类型的字段进行赋值时,无需添加单引号,可自动添加单引号
-
单个字面类型的参数
若mapper接口中的方法参数为单个字面量类型,此时可以使用KaTeX parse error: Expected 'EOF', got '#' at position 4: {}和#̲{}任意的名称获取参数的值,注…{}需要手动添加单引号
//interface userMapper User getUserById(String id);
//userMapper <!--getUserById--> <select id="getUserById" resultType="User"> select * from user where id=#{id} <!--select * from user where id='${id}'--> </select>
//TestClass User user=mapper.getUserById("1003");
-
多个字面量类型的参数
若mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1…为键,以参数位置;以param1,param2…为键,以参数为值;因此只需要通过${}和#{}访问map集合的键就可以获取相对应的值。
<select id="getUserById" resultType="User"> select * from user where username=#{arg0} and password=#{arg1} <!--select * from user where username='${arg0}' and password='${arg1}'--> </select>
-
map集合类型的参数
若mapper接口方法的参数有多个时,可以手动将这些参数放在一个map中存储,因此只需要通过${}和#{}访问map集合的键就可以获取相对应的值(同3)
-
mapper接口方法的参数是实体类型的参数
<!--insertUser(User user)--> <insert id="insertUser"> insert into user values (null ,#{username},#{password},#{age},#{gender}) </insert>
-
使用@Param注解命名参数,此时MyBatis会自动将这些参数放在一个map集合中,以@Param里的值为键,以参数位置;以param1,param2…为键,以参数为值;因此只需要通过${}和#{}访问map集合的键就可以获取相对应的值。(结合3,4)
MyBatis各种查询功能
-
查询一个实体类对象
若查询的数据只有一条:
可以通过实体类对象接受
可以通过list集合接受
可以通过map集合接受
若查询的数据有多条
可以通过实体类类型的list集合接收
可以通过map类型的list集合接收
可以在mapper接口的方法上添加注解@MapKey注解,此时以map集合作为值,以某个字段做为键
特殊SQL的执行
-
模糊查询
-
批量删除
-
动态设置名:只能使用${}
-
添加功能获取自增的主键:
useGeneratedKeys=“true” :设置当前标签中的sql使用了自增的逐渐
keyProperty=“id”:将自增的逐渐的值赋值给传输到映射文件中参数的某个属性
自定义映射resultMap
-
resultMap处理字段和属性的映射关系
字段名与属性名不一致的情况:通过设置别名
select id,username name,age,gender from user;
通过全局配置,将_自动映射为驼峰式
<settings> <setting name="mapUserscoreToCamelCase" value="true"></setting> </settings>
通过resultMap=userResultMap设置自定义的映射关系
属性:id:唯一标识 type:映射关系中实体类类型
子标签:id:设置逐渐的映射关系
result:设置普通字段的映射关系
<resultMap id="userResultMap" type="User"> <id property="id" cloumn="id"></id> <result property="name" cloumn="username"></result> <result property="password" cloumn="password"></result> <result property="age" cloumn="age"></result> <result property="gender" cloumn="gender"></result> </resultMap>
-
处理多对一映射的关系
通过级联属性赋值解决多对一的映射关系
案例场景:员工与部门的关系
select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.id=#{eid}
<resultMap id="empAndDeptResultMapOne" type="Emp"> <id property="eid" cloumn="eid"></id> <result property="name" cloumn="username"></result> <result property="password" cloumn="password"></result> <result property="age" cloumn="age"></result> <result property="gender" cloumn="gender"></result> <result property="dept.did" cloumn="did"></result> <result property="dept.deptName" cloumn="dept_name"></result> </resultMap>
通过associate标签实现
属性:property:需要处理多对一的映射关系的属性名
JavaType:该属性的类型
<resultMap id="empAndDeptResultMapOne" type="Emp"> <id property="eid" cloumn="eid"></id> <result property="name" cloumn="username"></result> <result property="password" cloumn="password"></result> <result property="age" cloumn="age"></result> <result property="gender" cloumn="gender"></result> <associate property="dept" javaType="Dept"> <id property="did" cloumn="did"></id> <result property="deptName" cloumn="dept_name"></result> </associate> </resultMap>
通过分步查询实现多对一的映射关系:可实现延迟加载
select:设置分布查询的 sql的唯一标识,(namespace.SQLId或mapper接口的全类名.方法名)
column:设置分布查询的条件
fetchType:当开启了全局加载的延迟加载之后,可通过此属性控制延迟加载的效果
<resultMap id="empAndDeptResultMapOne" type="Emp"> <id property="eid" cloumn="eid"></id> <result property="name" cloumn="username"></result> <result property="password" cloumn="password"></result> <result property="age" cloumn="age"></result> <result property="gender" cloumn="gender"></result> <associate property="dept" select="" column=""> </associate> </resultMap>
分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled:延迟加载的全局开关,当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法调用都会加载该类对象的所有属性,否则,每个属性会按需加载
此时就可以实现按需加载,获取的数据是什么,就会执行相应的sql,此时可通过associlation和collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType=“lazy(延迟加载) /eager(立即加载)”
-
处理一对多的映射关系
通过collection解决一对多的映射关系
属性值
ofType:表示该属性多对应的集合中存储数据类型的类型
<resultMap id="empAndDeptResultMapOne" type="Emp"> <id property="did" cloumn="did"></id> <result property="deptName" cloumn="deptName"></result> <collection property="emps" ofType="emp"> <id property="eid" cloumn="eid"></id> <result property="name" cloumn="username"></result> <result property="password" cloumn="password"></result> <result property="age" cloumn="age"></result> <result property="gender" cloumn="gender"></result> </collection> </resultMap>
通过分布查询的方式解决一对多的映射关系
动态SQL简介
MyBatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题
-
动态SQL:
if
if:根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到sql中
where
where:当where标签中没有内容时,会自动生成where关键字,并且将内容前多余的and或or去掉
当where标签中没有内容时,此时where标签没有任何效果
不能将内容后的多余的and或者or去掉
trim
trim:
若标签中没有内容时:
prefix|suffix:将trim标签中内容前面或者后面添加指定内容
suffixOverrides|prefixOverrides:将trim标签中内容前面或者后面去掉指定内容
若标签中有内容时,trim标签也没有任何效果
choose、when、otherwise
choose、when、otherwise:相当于if…else if…else
when至少有一个,otherwise最多只能有一个
<choose> <when></when> <when></when> <when></when> <otherwise></otherwise> </choose>
foreach:
collection:设置需要循环的数组或集合
item:表示数组或集合中的每一个数据
separator:循环体之间的分隔符
open:foreach标签所循环的所有内容的开始符
close:foreach标签所循环的所有内容的结束符
<delete id="deleteMoreByArray"> delete fromt_temp where eid in <foreach collection="eids" item="eid" separator="," open="(" close=")"> #{eid} </foreach> </delete>
其中定义参数时需要添加@Param注解
sql标签:
设置sql片段<sql id="empColumns">eid,emp_name,age,sex,email</sql> 引用SQL片段<include refid="empColumns"></include>
MyBatis的缓存
-
MyBatis的一级缓存
一级缓存是SqlSession级别的,通过同一个SqlSession查询的数据会被缓存,下次查询相同的数据时会从缓存中直接获取,不会从数据库重新访问
使得一级缓存失效的四种情况:
- 不同的SqlSession对应不同的一级缓存
- 同一个SqlSession但是查询条件不同
- 同一个SqlSession两次查询期间执行了任何一次增删改查操作
- 同一个SqlSession两次查询期间手动清空了缓存
-
MyBatis的二级缓存
二级缓存时SqlSessionFactory级别,通过同一个SqlSessionFanctory创建的SqlSession查询的结果会被缓存;此后若再次执行相同的查询语句,结果就会从缓存中获取
二级缓存开启的条件:
- 在核心配置文件中,设置全局配置属性cacheEnable=“true”,默认为true,不需要设置
- 在映射文件中设置标签
- 二级缓存必须在SqlSession关闭或提交之后有效
- 查询的数据所转换的实体类类型必须实现序列化的接口
时二级缓存失效的情况:
- 两次查询之间执行了任意增删改查操作,会使一级缓存和二级缓存同时失效
-
二级缓存的相关配置
在mapper配置文件中添加的cache标签可以设置一些属性
-
eviction属性:缓存回收策略
LRU(Least Recently Used)——最近最少使用的:移除最长时间不被使用的对象
FIFO(First in First out)——先进先出:按对象进入缓存的顺序来移除它们
SOFT——软引用:移除基于垃圾回收器状态和软引用规则的对象
WEAK——弱应用:更积极的移除基于垃圾回收器状态和弱引用规则的对象
默认的是LRU。
-
flushInterval属性:刷新间隔,单位毫秒
默认情况下是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新
-
size属性:引用数目,正整数
代表缓存最多可以存储多少个对象,太大容易导致溢出
-
readOnly属性:只读,true/false
true:只读缓存;会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。
false:读写缓存;会返回缓存对象的拷贝(通过序列化)。这会慢一些,但是安全,因此默认false
-
-
MyBatis缓存查询的顺序
先查询二级缓存,因为二级缓存中可能会有其它程序已经查出来的数据,可以拿来直接使用
如果二级缓存没有命中,再查询一级缓存
如果一级缓存也没有命中,则查询数据库
SqlSession关闭之后,一级缓存中的数据会写入到二级缓存
-
整合第三方缓存EHCache
-
添加依赖
<!--MyBatis EHCache整合包--> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.2.1</version> </dependency> <!--slf4j日志门面的一个具体实现--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
-
各jar包功能
jar包名称 作用 mybatis-ehcache MyBatis和EHCache的整合包 ehcache EHCache核心包 slf4j-api SLF4j日志门面包 logback-classic 支持SLF4J门面接口的一个具体实现 -
创建第三方缓存EHCache的配置文件ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- 磁盘保存路径 --> <diskStore path="D:\atguigu\ehcache" /> <defaultCache maxElementsInMemory="1000" maxElementsOnDisk="10000000" eternal="false" overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="120" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> </ehcache>
-
配置 cache 标签(在映射文件userMapper中配置)
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
-
MyBaties逆向工程
-
简介
MyBatis Generator: 简称 MBG,是一个专门为 MyBatis 框架使用者定制的代码生成器, 可以快速的根据表生成对应的映射文件,接口,以及 bean 类。支持基本的增删改查, 以及 QBC 风格的条件查询。但是表连接、存储过程等这些复杂 sql 的定义需要我们手工编写
-
逆向工程的配置
-
插件依赖的配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xf.mvc</groupId> <artifactId>demo2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <!--MyBatis核心包--> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> </dependencies> <!--控制Maven在构建过程中用到的插件--> <build> <plugins> <!--具体插件,逆向工程的操作是以构建过程中插件形式出现的--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.0</version> <!--插件的依赖--> <dependencies> <!--逆向工程的核心依赖--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!--数据库连接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <!--MySQL驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.8</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
-
创建相关文件及引入相关依赖包
<!--MyBatis核心包--> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!--junit测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--Mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.3</version> </dependency> <!--日志--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
-
配置generatorConfig.xml(文件名不能改变)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- targetRuntime: 执行生成的逆向工程的版本 MyBatis3Simple: 生成基本的CRUD MyBatis3: 生成带条件的CRUD --> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8" userId="root" password="root"> </jdbcConnection> <!-- javaBean的生成策略--> <javaModelGenerator targetPackage="com.xf.mybatis.beans" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- SQL映射文件的生成策略 --> <sqlMapGenerator targetPackage="com.xf.mybatis.dao" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- Mapper接口的生成策略 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xf.mybatis.dao" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 逆向分析的表 --> <table tableName="user" domainObjectName="User"></table> </context> </generatorConfiguration>
-
双击插件执行即可生成相对应的包
在核心配置的映射文件中添加对应的包
<typeAliases> <!-- <typeAlias type=""/>--> <package name="com.xf.mybatis.dao"/> </typeAliases> <mappers> <!-- <mapper resource=""/>--> <package name="com.xf.mybatis.dao"/> </mappers>
-
测试
public class TestMBG { @Test public void testMBG(){ try { InputStream is= Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession=sqlSessionFactory.openSession(true); UserMapper mapper=sqlSession.getMapper(UserMapper.class); // List<User> users =mapper.selectByExample(null); // users.forEach(user -> System.out.println(user)); UserExample userExample=new UserExample(); userExample.createCriteria().andUsernameEqualTo("admin"); System.out.println(mapper.selectByExample(userExample)); } catch (IOException e) { throw new RuntimeException(e); } } }
-
MyBatis的分页插件
-
分页插件使用配置
-
添加依赖
<!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency>
-
配置分页插件(在mybatis核心配置文件中配置)
<plugins> <!--配置分页插件--> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
-
-
分页插件的使用
/** * limit、index、pageSize * index:当前页的起始索引 * pageSize:每页显示的条数 * pageNum:当前页的页码 * index=(pageNum-1)*pageSize * PageHelper.startPage(int pageNum,int pageSize); */ @Test public void testMBG(){ try { InputStream is= Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession=sqlSessionFactory.openSession(true); UserMapper mapper=sqlSession.getMapper(UserMapper.class); // mapper.insert(new User(null,"rush","123321",20,"女")); PageHelper.startPage(2,4); List<User> users =mapper.selectByExample(null); users.forEach(user -> System.out.println(user)); } catch (IOException e) { throw new RuntimeException(e); } }