MyBatis通用Mapper和PageHelper的過程詳解
如果項目中使用到了MyBatis框架,那么使用通用Mapper和PageHelper分頁插件將極大的簡化我們的操作。通用Mapper可以簡化對單表的CRUD操作,PageHelper分頁插件可以幫我們自動拼接分頁SQL,并且可以使用MyBatis Geneator來自動生成實體類,Mapper接口和Mapper xml代碼,非常的方便。插件地址及作者鏈接https://gitee.com/free 。
引入依賴
這里使用Spring Boot來構(gòu)建,可參考Spring-Boot中使用Mybatis.html搭建一個Spring boot + MyBatis的框架,然后在pom中引入:
<!-- 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.1.5</version> </dependency> <!-- pagehelper 分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
接著在pom中配置MyBatis Geneator:
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <!-- 數(shù)據(jù)庫連接驅(qū)動 --> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>6.0</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.0</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>package</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!--允許移動生成的文件 --> <verbose>true</verbose> <!-- 是否覆蓋 --> <overwrite>true</overwrite> <!-- 自動生成的配置 --> <configurationFile>src/main/resources/mybatis-generator.xml</configurationFile> </configuration> </plugin> </plugins> </build>
src/main/resources/mybatis-generator.xml為生成器的配置,下文會介紹到。
配置插件
在Spring Boot配置文件application.yml中配置MyBatis:
mybatis: # type-aliases掃描路徑 type-aliases-package: com.springboot.bean # mapper xml實現(xiàn)掃描路徑 mapper-locations: classpath:mapper/*.xml property: order: BEFORE
接下來開始配置插件。
配置通用Mapper
在Spring Boot配置文件application.yml中配置通用Mapper:
#mappers 多個接口時逗號隔開 mapper: mappers: com.springboot.config.MyMapper not-empty: false identity: oracle
關于參數(shù)的說明,參考https://gitee.com/free/Mapper/blob/master/wiki/mapper3/2.Integration.md中的可配參數(shù)介紹。
除此之外,我們需要定義一個MyMapper接口:
import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> { }
值得注意的是,該接口不能被掃描到,應該和自己定義的Mapper分開。自己定義的Mapper都需要繼承這個接口。
配置PageHelper
在Spring Boot配置文件application.yml中配置通用配置PageHelper:
#pagehelper pagehelper: helperDialect: oracle reasonable: true supportMethodsArguments: true params: count=countSql
參數(shù)相關說明參考https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md中的分頁插件參數(shù)介紹。
配置Geneator*
在路徑src/main/resources/下新建mybatis-generator.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> <context id="oracle" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <!-- 該配置會使生產(chǎn)的Mapper自動繼承MyMapper --> <property name="mappers" value="com.springboot.config.MyMapper" /> <!-- caseSensitive默認false,當數(shù)據(jù)庫表名區(qū)分大小寫時,可以將該屬性設置為true --> <property name="caseSensitive" value="false"/> </plugin> <!-- 阻止生成自動注釋 --> <commentGenerator> <property name="javaFileEncoding" value="UTF-8"/> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 數(shù)據(jù)庫鏈接地址賬號密碼 --> <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:ORCL" userId="scott" password="6742530"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成Model類存放位置 --> <javaModelGenerator targetPackage="com.springboot.bean" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件存放位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成Dao類存放位置 --> <!-- 客戶端代碼,生成易于使用的針對Model對象和XML配置文件的代碼 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 配置需要生成的表 --> <table tableName="T_USER" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="id" sqlStatement="oralce" identity="true"/> </table> </context> </generatorConfiguration>
轉(zhuǎn)載:https://mrbird.cc/MyBatis%20common%20Mapper%20PageHelper.html
到此這篇關于MyBatis通用Mapper和PageHelper的文章就介紹到這了,更多相關MyBatis通用Mapper和PageHelper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring中@ConditionalOnProperty注解的作用詳解
這篇文章主要介紹了Spring中@ConditionalOnProperty注解的作用詳解,@ConditionalOnProperty注解主要是用來判斷配置文件中的內(nèi)容來決定配置類是否生效用的,如果條件不匹配,則配置類不生效,需要的朋友可以參考下2024-01-01使用Feign調(diào)用時添加驗證信息token到請求頭方式
這篇文章主要介紹了使用Feign調(diào)用時添加驗證信息token到請求頭方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03