Mybatis配置PageHelper分頁插件、Generator代碼生成器

以前使用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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章