以前使用ibatis/mybatis,都是比人配置好的我们只需要用就好了,最近一个人在做一个小项目,需要用到就去网上看了一下教程,发现还挺简单的,PageHelper感觉还不错是通过mybatis的pulgin来实现Interceptor接口
下面开始吧
Mybatis-pagehelper数据库物理分页
1. Maven项目引入依赖Jar包,
<!-- Mybatis数据库物理分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
2. 配置分页拦截器
PageHelper的原理是基于拦截器实现的。拦截器的配置有两种方法,一种是在mybatis的配置文件中配置,一种是直接在spring的配置文件中进行:
我用的是在mybatis-config.xml文件中配置:
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
到这里PageHelper所需要的配置已经完成,下面还需要在service类中加入分页参数的具体代码:
3. 向拦截器传递分页参数
/**
*
* @param map 查询的条件 我是通过Map传参
* @param page 第几页
* @param size 每页显示的长度(条数)
* @return
*/
public PageInfo<User> selectByUsersPageInfo(Map<String, Object> map, int page, int size) {
PageHelper.startPage(page,size);
//selectByUsers调用的是前面没分页的方法
List<User> userPageInfo = userMapper.selectByUsers(map);
return new PageInfo<User>(userPageInfo);
}
这是SelectByUsers的xml,
<select id="selectByUsers" parameterType="java.util.Map" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
<where>
<if test="id!=null and id!=''">
AND ID = #{id}
</if>
<if test="keyy!=null and keyy!=''">
AND Keyy = #{keyy}
</if>
<if test="name!=null and name!=''">
AND Name = #{name}
</if>
</where>
</select>
我认为这种方式不入侵mapper代码。
其实一开始看到这段代码时候,我觉得应该是内存分页。其实插件对mybatis执行流程进行了增强,添加了limit以及count查询,属于物理分页
注意:
PageHelper是针对持久化操作进行拦截的,需要和Mapper放到一起,否者会造成数据的不准确。
这是Controller
PageInfo<User> userPageInfo = userService.selectByUsersPageInfo(map,pageindex,leng);
map.clear();
map.put("rows",userPageInfo.getList());//这是分页好的对象集合
map.put("total",userPageInfo.getTotal()); //一共有多少条符合条件的数据
map.put("pageNum",userPageInfo.getPageNum()); //一共多少页,还有很多,需要的可以自己去试试
return map;
mybatis-generator逆向工程插件
1、pom文件添加插件,
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</plugin>
配置文件,/resource下即可
<?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>
<classPathEntry location="mysql-connector-java.jar的绝对路径"/>
<context id="testTables" targetRuntime="MyBatis3" >
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/数据库名"
userId="xxxxx"
password="xxxxxx">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.test.pojo"
targetProject="src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.test.mapper"
targetProject="src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="update_log" enableSelectByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableCountByExample="false"
enableDeleteByPrimaryKey="false"/>
<!--有些表的字段需要指定java类型-->
<!--<table schema="" tableName="user">
<columnOverride column="birthday" javaType="Date" />
</table>-->
</context>
</generatorConfiguration>
右键 Reimport,就会出现mybatis-generator,双击运行就会自动生成dao、Mapper相关文件
参考项目 : https://github.com/pagehelper/Mybatis-PageHelper