一、准备工作
新建maven项目
连接mysql数据库
修改pom.xml:
1.设置打包方式为jar
<groupId>com.atguigu.mybatis</groupId>
<artifactId>MyBatis_demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
2.导入mysql、junit、mybatis、log4j依赖
<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>8.0.15</version>
</dependency>
<!-- log4j日志 --> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
把log4j.xml放在resources下
<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration debug="true">
<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>
二、pojo与mapper
pojo
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String sex;
private String email;}
mapper接口
public interface UserMapper {
/*
* 1、映射文件的namespace 要和mapper接口的全类名保持一致
* 2、映射文件中的SQL语句的id要和mapper接口中的方法名保持一致
* */
int insertUser();
/**
* 修改用户信息
*/
void updateUser();
void deleteUser();
User getUserById();
List<User> getAllUser();
}
三、创建映射文件模板
1、jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=root
2、核心配置文件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>
<!--
properties?,settings?,typeAliases?,
typeHandlers?,objectFactory?,objectWrapperFactory?,
reflectorFactory?,plugins?,environments?,
databaseIdProvider?,mappers?)".-->
<!--引入properties文件-->
<properties resource="jdbc.properties"/>
<!--设置类型别名,不区分大小写,若不设置默认类名,且不区分大小写-->
<typeAliases>
<!--<typeAlias type="com.atguigu.mybatis.pojo.User" alias="User"></typeAlias> -->
<!--以包位单位,设置默认类型别名,即类名且不区分大小写-->
<package name="com.atguigu.mybatis.pojo"/>
</typeAliases>
<!--配置连接数据库的环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<!--<mapper resource="mappers/UserMapper.xml"/>-->
<!--以包为单位引入映射文件
1、mapper接口所在的包要和映射文件所在的包一致
2、mapper接口和映射文件的名字一致
-->
<package name="com.atguigu.mybatis.mapper"/>
</mappers>
</configuration>
3、映射文件
<?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="com.atguigu.mybatis.mapper.UserMapper">
<!--insertUser-->
<insert id="insertUser">
insert into t_user values (null,'admin',123456,23,'男','12345@qq.com')
</insert>
<!--updateUser()-->
<update id="updateUser">
update t_user set username='张四' where id = 4
</update>
<!--deleteUser-->
<delete id="deleteUser">
delete from t_user where id=5
</delete>
<!--getUserById-->
<!--
查寻功能的标签必须设置resultType或者resultMap
resultType:设置默认的映射关系
resultMap:设置自定义的映射关系
-->
<select id="getUserById" resultType="User">
select * from t_user where id=3
</select>
<!--getAllUser-->
<select id="getAllUser" resultType="User">
select * from t_user
</select>
</mapper>
四、SqlSessionUtils工具类和测试
SqlSessionUtil
public class SqlSessionUtils {
public static SqlSession getSqlSession(){
SqlSession sqlSession=null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession(true);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
测试
public class ParameterMapperTest {
@Test
public void testgetAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
List<User> list = mapper.getAllUser();
list.forEach(user -> System.out.println(user));
}
}
五、MyBatis获取参数
/**
* MyBatis获取参数的两种方法:${}和#{}
* ${}本质是字符串拼接 问题:1.sql注入,2、手动输入单引号
* #{}本质是占位符赋值 用这个
* MyBatis获取参数各种情况
* 1、mapper接口方法的参数为单个的字面量类型
* 可以通过${}和#{}以任意的字符串获取参数值,但是需要注意${}的单引号问题
* 2、如果mapper接口方法参数为多个
*此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储
* a>以arg0,arg1....为键,以参数为值
* b>以param1,param2...为键,以参数为值
* 因此只需要通过#{}或${}以键的方式访问值即可, 但是需要注意${}的单引号问题
* 3、若mapper接口方法的参数有多个时,可以手动将参数放在一个map中来存储
* 只需要通过#{}或${}以键的方式访问值即可, 但是需要注意${}的单引号问题
*4、mapper接口方法的参数是一个实体类类型的参数
* 只需要通过#{}或${}以属性的方式访问属性值即可, 但是需要注意${}的单引号问题
* 5、使用@param命名参数
*此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储
* a>以@Param注解的值为键,以参数为值
* b>以param1,param2...为键,以参数为值
* 因此只需要通过#{}或${}以键的方式访问值即可, 但是需要注意${}的单引号问题
* */
接口
public interface ParameterMapper {
//验证登录
User checkLogin(String username,String password);
//使用map验证登录
User checkLoginByMap(Map<String,Object>map);
//添加一个用户信息
int insertUser(User user);
//验证登录(使用@Param注解)
User checkLoginByParam(@Param("username") String username,@Param("password") String password);
User GetUserByUsername(String username);
List<User> getAllUser();
}
映射
<!--getAllUser-->
<select id="getAllUser" resultType="User">
select * from t_user
</select>
<select id="GetUserByUsername" resultType="User">
<!--select * from t_user where username= #{username-->
select * from t_user where username='${name}'
</select>
<select id="checkLogin" resultType="User">
select * from t_user where username=#{arg0} and password=#{arg1}
<!--select * from t_user where username=#{param1} and password=#{param2}-->
<!--select * from t_user where username='${arg0}' and password='${arg1}'-->
</select>
<select id="checkLoginByMap" resultType="User">
select * from t_user where username=#{username} and password=#{password}
</select>
<insert id="insertUser">
insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email});
</insert>
<select id="checkLoginByParam" resultType="User">
select * from t_user where username=#{username} and password=#{password}
</select>
测试
@Test
public void testgetUserByUsername(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.GetUserByUsername("admin");
System.out.println(user);
}
@Test
public void testLogin(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.checkLogin("admin","123456");
System.out.println(user);
}
@Test
public void testCheckLoginByMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
Map<String,Object> map=new HashMap<>();
map.put("username","admin");
map.put("password","123456");
User user = mapper.checkLoginByMap(map);
System.out.println(user);
}
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
int result=mapper.insertUser(new User(null,"李9","32323",23,"男","6328378@qq.com"));
System.out.println(result);
}
@Test
public void testcheckLoginByParam(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user=mapper.checkLoginByParam("admin","123456");
System.out.println(user);
}
@Test
public void testgetAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
List<User> list = mapper.getAllUser();
list.forEach(user -> System.out.println(user));
}
@Test
public void testJDBC() throws Exception {
String username="admin";
Class.forName("");
Connection connection= DriverManager.getConnection("","","");
//字符串拼接
//PreparedStatement preparedStatement = connection.prepareStatement("select * from t_user where username= '" + username + "'");
//占位符赋值
PreparedStatement ps = connection.prepareStatement("select * from t_user where username=?");
ps.setString(1,username);
}
六、MyBatis各种查询功能
/**
* MyBatis的各种查询功能
* 1、若查询出的数据只有一条
* a>可以通过实体类对象
* b>可以通过List集合接收
* c>可以通过map集合来接收结果
* 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin}
* 2、若查询出的数据有多条
* a>可以通过List集合来接收
* b>可以在mapper接口的方法上添加@Mapkey注解,此时就可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中
* 注意:一定不能通过实体类队形接收结果,此时会抛出异常TooManyResultsException
*
*
* MyBatis中设置了默认的类型别名
* Java.lang.Integer-->int,integer
* int-->_int,_integer
* Map-->map
* String-->string
*/
接口
public interface SelectMapper {
/**
* 根据id查寻用户信息
*/
User getUserById(@Param("id") Integer id);
/**
* 查询所有的用户信息
*/
List<User> getAllUser();
Integer getCount();
/**
* 根据id查询用户信息为一个map集合
*/
Map<String,Object>getUserByIdToMap(@Param("id") Integer id);
//查询所有用户信息为map集合
@MapKey("id")
Map<String,Object>getAllUserToMap();
}
映射
<mapper namespace="com.atguigu.mybatis.mapper.SelectMapper">
<!-- User getuserById(@Param("id") Integer id);-->
<select id="getUserById" resultType="User">
select * from t_user where id=#{id}
</select>
<select id="getAllUser" resultType="User">
select * from t_user
</select>
<select id="getCount" resultType="java.lang.Integer">
select count(*)from t_user
</select>
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id=#{id}
</select>
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
</mapper>
测试
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
System.out.println(mapper.getAllUserToMap());
}
七、特殊的Sql执行
1、模糊查询
//根据用户名模糊查询用户信息
List<User> getUserByLike(@Param("username") String username);
<select id="getUserByLike" resultType="User">
<!--select * from t_user where username like '%${username}%'-->
<!-- select * from t_user where username like concat('%',#{username},'%')-->
select * from t_user where username like "%"#{username}"%"
</select>
2、批量删除
//批量删除
int deleteMore(@Param("ids") String ids);
<delete id="deleteMore">
delete from t_user where id in (${ids})
</delete>
@Test
public void testDeleteMore(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SqlMapper mapper = sqlSession.getMapper(SqlMapper.class);
int result = mapper.deleteMore("7,2,3");
System.out.println(result);
}
3、动态设置表名
//查询指定表中的数据
List<User>getUserByTableName(@Param("tablename") String tablename);
<select id="getUserByTableName" resultType="User">
select * from ${tablename}
</select>
@Test
public void testGetUserByTableName(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SqlMapper mapper = sqlSession.getMapper(SqlMapper.class);
List<User> list = mapper.getUserByTableName("t_user");
System.out.println(list);
}
4、添加功能获得自动递增的主键
//添加用户信息
void insertUser(User user);
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
</insert>
User user=new User(null,"王五","1234",23,"男","12426@163.com");
mapper.insertUser(user);
System.out.println(user);
八、自定义映射resultMap
1、字段名和属性名不匹配
* 解决字段名和属性名不一致的情况
* a>为字段名起别名,保持和属性名一致
*b>设置全局配置,将下划线自动映射成驼峰
* <setting name="mapUnderscoreToCamelCase" value="true"/>
* c>通过resultMap设置自定义的映射关系
* <resultMap id="empResultMap" type="Emp">
* <id property="eid" column="eid"></id>
* <result property="empName" column="emp_name"></result>
* <result property="age" column="age"></result>
* <result property="sex" column="sex"></result>
* <result property="email" column="email"></result>
*</resultMap>
//查询所有的员工信息
List<Emp> getAllEmp();
<resultMap id="empResultMap" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
</resultMap>
<select id="getAllEmp" resultMap="empResultMap">
select * from t_emp
</select>
<select id="getAllEmpOld" resultType="Emp">
<!--select eid,emp_name empName,age,sex,email from t_emp-->
select * from t_emp
</select>
<!--设置mybatis全局配置-->
<settings>
<!--将下划线自动映射成驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
2、处理多对一映射关系
// 查询员工以及员工所对应的部门信息
Emp getEmpAndDept(@Param("eid") Integer eid);
a>级联属性赋值
<resultMap id="empAndDeptResultMapOne" type="emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<result property="dept.did" column="did"></result>
<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid=#{eid}
</select>
b>association:处理多对一映射关系
<resultMap id="empAndDeptResultMapTwo" type="emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<association property="dept" javaType="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
</association>
</resultMap>
c>分步查询
//分步查询,查询员工以及员工所对应的部门信息
//第一步:查询员工信息
Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
//分步查询第二步:通过did查询员工对应的部门信息
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from t_dept where did=#{did}
</select>
<mapper namespace="com.atguigu.mybatis.mapper.EmpMapper">
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<association property="dept"
select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did"></association>
</resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select * from t_emp where eid=#{eid}
</select>
3、多对一
a>collection
b>分布
Dept getDeptAndempByStepOne(@Param("did") Integer did)
List<Emp> getDeptAndEmpByStepTwo(@Param("did")Integer did);
<resultMap id="deptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps" select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="did"
></collection>
</resultMap>
<select id="getDeptAndempByStepOne" resultMap="deptAndEmpByStepResultMap">
select * from t_dept where did=#{did}
</select>
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select * from t_emp where did=#{did}
</select>
九、动态Sql
1、if
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp where
<if test="empName!=null and empName!=''">
emp_name=#{empName}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
<if test="email!=null and email!=''">
and age=#{age}
</if>
</select>
2、where
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<where>
<if test="empName!=null and empName!=''">
emp_name=#{empName}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
<if test="email!=null and email!=''">
and age=#{age}
</if>
</where>
</select>
3、trim
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<where>
<if test="empName!=null and empName!=''">
emp_name=#{empName} and
</if>
<if test="age!=null and age!=''">
age=#{age} and
</if>
<if test="sex!=null and sex!=''">
sex=#{sex} and
</if>
<if test="email!=null and email!=''">
email=#{email}
</if>
</where>
</select>
4、foreach批量操作
//通过数组实现批量删除
int deleteMoreByArray(@Param("eids") Integer[] eids);
<delete id="deleteMoreByArray">
delete from t_emp where eid in
(
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>)
</delete>
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid=#{eid}
</foreach>
//通过list实现批量添加
int insertMoreByList(@Param("emps") List<Emp> emps);
<insert id="insertMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
</foreach>
</insert>
5、sql标签
设置sql片段
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
引用sql片段
select <include refid="empColumns"></include> from t_emp where
十、MyBatis的缓存
十一、逆向工程
依赖
<!-- 依赖MyBatis核心包 -->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</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>8.0.15</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
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.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"
userId="root"
password="root">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.atguigu.mybatis.pojo" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.atguigu.mybatis.mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="t_emp" domainObjectName="Emp"/>
<table tableName="t_dept" domainObjectName="Dept"/>
</context>
</generatorConfiguration>
十二、分页插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
List<Emp> list = mapper.selectByExample(null);
PageInfo<Emp> page=new PageInfo<>(list,3);
list.forEach(emp-> System.out.println(emp));
System.out.println(page);