springboot植入pagerHelper的超詳細(xì)教程
簡(jiǎn)介
前面?zhèn)€已經(jīng)講過(guò)mybatis的批量更新操作。批量操作還有時(shí)分頁(yè)查詢(xún),針對(duì)項(xiàng)目的完善性,來(lái)講解一下分頁(yè)工具的植入pagerHelper和tk.mybatis使用。其實(shí)官網(wǎng)已經(jīng)有具體代碼,代價(jià)有空可以多多參考官網(wǎng)操作。鏈接地址MyBatis-Spring-Boot
技術(shù)方案
maven jar導(dǎo)入
查看官方說(shuō)明引入依賴(lài),如下:
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <!--pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
maven plugin配置
引入完jar依賴(lài)之后,配置plugin插件,插件時(shí)根據(jù)maven來(lái)識(shí)別的,可以直接拷貝官網(wǎng)的配置即可,如下:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-generator</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
配置generatorConfig.xml
根據(jù)自己喜歡,可以定制化配置generatorConfig.xml,下面是我個(gè)人基本配置,更多配置說(shuō)明,請(qǐng)查看官方說(shuō)明MyBatis Generator 詳解
<?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>
<properties resource="generator/application-dev.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.lgh.common.util.MyMapper"/>
</plugin>
<jdbcConnection driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
</jdbcConnection>
<javaModelGenerator targetPackage="com.lgh.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<javaClientGenerator targetPackage="com.lgh.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<!-- 數(shù)據(jù)庫(kù)表 以及實(shí)體類(lèi)命名 -->
<!-- <table schema="CL_DEMO" tableName="tb_user" domainObjectName="User"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="tb_role" domainObjectName="Role"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="tb_menu" domainObjectName="Menu"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="tb_resource" domainObjectName="Resource"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="user_role" domainObjectName="UserRole"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="role_menu" domainObjectName="RoleMenu"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="menu_resource" domainObjectName="MenuResource"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="role_resource" domainObjectName="RoleResource"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />
<table schema="CL_DEMO" tableName="logon" domainObjectName="Logon"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" />-->
</context>
</generatorConfiguration>
測(cè)試樣例

點(diǎn)擊mybatis-generator:generate即可生成對(duì)象和映射文件,具體如上圖
一般分頁(yè)個(gè)人喜好建議用jdk8的lambda表達(dá)式,如://對(duì)應(yīng)的lambda用法更多請(qǐng)查看官網(wǎng)分頁(yè)使用方式
pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(() -> userMapper.selectGroupBy());,
總結(jié)&反思
基本操作對(duì)象,我們不要再手動(dòng)一個(gè)一個(gè)的寫(xiě)啦,直接用mybatis插件生成?;綾urd不要再自己編寫(xiě)xml,直接用tk.mysql操作即可。一對(duì)多情況,分頁(yè)無(wú)法實(shí)現(xiàn)謹(jǐn)慎使用
源碼地址
到此這篇關(guān)于springboot植入pagerHelper的文章就介紹到這了,更多相關(guān)springboot植入pagerHelper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JDK1.7中HashMap的死循環(huán)問(wèn)題及解決方案
這篇文章主要為大家介紹了JDK1.7中HashMap的死循環(huán)問(wèn)題及解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Spring MVC集成springfox-swagger2構(gòu)建restful API的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring MVC集成springfox-swagger2構(gòu)建restful API的相關(guān)資料,文中介紹介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06
java 過(guò)濾器filter防sql注入的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇java 過(guò)濾器filter防sql注入的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
Java線(xiàn)程Dump分析工具jstack解析及使用場(chǎng)景
這篇文章主要介紹了Java線(xiàn)程Dump分析工具jstack解析及使用場(chǎng)景,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
如何在springboot中實(shí)現(xiàn)頁(yè)面的國(guó)際化
今天帶大家學(xué)習(xí)如何在springboot中實(shí)現(xiàn)頁(yè)面的國(guó)際化,文中有非常詳細(xì)的圖文解說(shuō)及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05

