sqlSession2.clearCache(); //清空一级缓存
<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.zhnx.mybatis</groupId>
<artifactId>mybatis_mgb02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mybatis_mgb02</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- mybatis逆向工程依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<!-- 构建过程中用到的插件 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<!-- 具体插件,逆向工程的操作是以构建过程中插件形式出现的 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<!-- 插件的依赖 -->
<dependencies>
<!-- 逆向工程的核心依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除生成的注解 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接配置 -->
<!-- 注意xml中不支持&,用&代替 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8"
userId="root" password="123456"></jdbcConnection>
<!-- 处理NUMERIC和DECIMAL类型的策略 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 配置pojo生成的位置 -->
<javaModelGenerator targetPackage="com.zhnx.mybatis.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 配置sql映射文件的生成位置 -->
<sqlMapGenerator targetPackage="com.zhnx.mybatis.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</sqlMapGenerator>
<!-- 配置dao接口的生成位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zhnx.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaClientGenerator>
<!-- 指定逆向依据的数据表 -->
<table tableName="t_emp" domainObjectName="Emp"/>
<table tableName="t_dept" domainObjectName="Dept"/>
</context>
</generatorConfiguration>
<?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>
<!-- Mybatis 核心配置文件,标签的顺序
properties?,settings?,typeAliases?,typeHandlers?,
objectFactory?,objectWrapperFactory?,reflectorFactory?,
plugins?,environments?,databaseIdProvider?,mappers?
-->
<!--引入properties文件,然后才可以在当前文件中使用的方式访问 -->
<!-- ”jdbc.properties“ 的文件名 -->
<properties resource="jdbc.properties"/>
<!--设置类型别名 -->
<typeAliases>
<!-- 比较 好的方式就是以包为单位,将报下所有的类型设置成默认的类型别名,类名不区分大小写 -->
<!-- 类所在包的全类名:com.zhnx.mybatis.pojo-->
<package name="com.zhnx.mybatis.pojo"/>
</typeAliases>
<!--设置连接数据库的环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 设置数据库连接的驱动 ${jdbc.driver}-->
<property name="driver" value="${jdbc.driver}"/>
<!-- 设置数据库的连接地址 ${jdbc.url}-->
<property name="url" value="${jdbc.url}"/>
<!-- 设置连接数据库的用户名 ${jdbc.username}-->
<property name="username" value="${jdbc.username}"/>
<!-- 设置连接数据库的密码 ${jdbc.password}-->
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<!-- 要求: 1、mapper接口所在的包要和映射文件所在的包一致
2、mapper接口要和映射文件的名字一致
3、如:com.zhnx.mybatis.mapper-->
<package name="com.zhnx.mybatis.mapper"/>
</mappers>
</configuration>
@Test
public void testMBG(){
try{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
mapper.updateByPrimaryKeySelective(new Emp(1,"admin",22,"男",3));
}catch (IOException e) {
e.printStackTrace();
}
}
分页功能的底层实现就不赘述啦,直接上插件
在pom.xml中添加依赖
<!--分页插件的依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
核心配置文件中添加插件
<!--配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
开始测试
@Test
public void PageTest(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//查询之前开启分页功能
Page<Object> page = PageHelper.startPage(1, 4);
/*
* Page{count=true, pageNum=1, pageSize=4,
* startRow=0, endRow=4, total=14, pages=4,
* reasonable=false, pageSizeZero=false}
*/
List<Emp> list = mapper.selectByExample(null);
//查询功能之后,获取和分页相关的所有数据
PageInfo<Emp> pageInfo = new PageInfo<>(list,5);
/*
PageInfo{pageNum=1, pageSize=4, size=4,
startRow=1, endRow=4, total=14, pages=4,
list=Page{count=true, pageNum=1, pageSize=4,
startRow=0, endRow=4, total=14, pages=4,
reasonable=false, pageSizeZero=false}
[Emp{empId=1, empName='admin', age=22, gender='男', deptId=3},
Emp{empId=2, empName='李希', age=21, gender='男', deptId=2},
Emp{empId=3, empName='猪尾巴', age=67, gender='男', deptId=3},
Emp{empId=4, empName='赵六', age=33, gender='男', deptId=1}],
prePage=0, nextPage=2, isFirstPage=true,
isLastPage=false, hasPreviousPage=false,
hasNextPage=true, navigatePages=5,
navigateFirstPage=1, navigateLastPage=4,
navigatepageNums=[1, 2, 3, 4]}
*/
list.forEach(System.out::println);
System.out.println(pageInfo);
}
}