如何自動生成Mybatis的Mapper文件詳解
前言
工作中使用mybatis時我們需要根據(jù)數(shù)據(jù)表字段創(chuàng)建pojo類、mapper文件以及dao類,并且需要配置它們之間的依賴關(guān)系,這樣的工作很瑣碎和重復(fù),mybatis官方也發(fā)現(xiàn)了這個問題,因此給我們提供了mybatis generator工具來幫我們自動創(chuàng)建pojo類、mapper文件以及dao類并且會幫我們配置好它們的依賴關(guān)系。
實際上,最非常流行MyBatis-Plus中內(nèi)置了代碼生成器:采用代碼或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 層代碼,支持模板引擎,有超多自定義配置等,在這主要介紹Mybatis的自動生成步驟。
1|1插件依賴
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</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/generatorConfig.xml </configurationFile> </configuration> </plugin> </plugins> </build>
注意:mysql-connector-java的版本問題,如果你的驅(qū)動是com.mysql.cj.jdbc.Driver,你就需要6.0.x的版本。如果是com.mysql.jdbc.Driver 則是5.1.x的版本。
1|2配置generatorConfig.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> <!--導(dǎo)入屬性配置--> <properties resource="datasource.properties"></properties> <!-- context 是逆向工程的主要配置信息 --> <!-- id:name --> <!-- targetRuntime:設(shè)置生成的文件適用于那個 mybatis 版本 --> <context id="default" targetRuntime="MyBatis3"> <!-- 生成的 Java 文件的編碼 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- optional,旨在創(chuàng)建class時,對注釋進行控制 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的數(shù)據(jù)庫連接 --> <jdbcConnection driverClass="${db.driverClassName}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}"> </jdbcConnection> <!-- 非必需,類型處理器,在數(shù)據(jù)庫類型和java類型之間的轉(zhuǎn)換控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該項目下所在的路徑 --> <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">--> <javaModelGenerator targetPackage="com.ke.likehouse.model" targetProject="./src/main/java"> <!-- 是否允許子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 是否對model添加 構(gòu)造函數(shù) --> <property name="constructorBased" value="true"/> <!-- 是否對類CHAR類型的列的數(shù)據(jù)進行trim操作 --> <property name="trimStrings" value="true"/> <!-- 建立的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構(gòu)造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!--mapper映射文件生成所在的目錄 為每一個數(shù)據(jù)庫的表生成對應(yīng)的SqlMap文件 --> <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">--> <sqlMapGenerator targetPackage="mybatis/mappers" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客戶端代碼,生成易于使用的針對Model對象和XML配置文件 的代碼 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象 type="MIXEDMAPPER",生成基于注解的Java Model 和相應(yīng)的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 --> <!-- targetPackage:mapper接口dao生成的位置 --> <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.ke.likehouse.dao" targetProject="./src/main/java"> <!-- enableSubPackages:是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!--生成的表--> <!--domainObjectName:生成的domain類的名字,如果不設(shè)置,直接使用表名作為domain類的名字;可以設(shè)置為somepck.domainName,那么會自動把domainName類再放到somepck包里面;--> <!--enableInsert(默認(rèn)true):指定是否生成insert語句;--> <!--enableSelectByPrimaryKey(默認(rèn)true):指定是否生成按照主鍵查詢對象的語句(就是getById或get);--> <!--enableSelectByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)查詢語句;--> <!--enableUpdateByPrimaryKey(默認(rèn)true):指定是否生成按照主鍵修改對象的語句(即update);--> <!--enableDeleteByPrimaryKey(默認(rèn)true):指定是否生成按照主鍵刪除對象的語句(即delete);--> <!--enableDeleteByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)刪除語句;--> <!--enableCountByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)查詢總條數(shù)語句(用于分頁的總條數(shù)查詢);--> <!--enableUpdateByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)修改語句(只修改對象中不為空的屬性);--> <table tableName="agent" domainObjectName="Agent" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!-- geelynote mybatis插件的搭建 --> </context> </generatorConfiguration>
1|3提供datasource.properties
db.driverClassName = com.mysql.cj.jdbc.Driver db.url = jdbc:mysql://localhost:3306/twelve?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true db.username = root db.password = 你的密碼
1|4執(zhí)行maven命令
方式一:通過IDEA的MAVEN工具執(zhí)行
方式二:通過MAVEN命令
配置命令:mybatis-generator:generate -e
然后Run就好了:
1|5可能出現(xiàn)的BUG
如果你復(fù)制粘貼了代碼卻出現(xiàn)稀奇古怪的BUG,很有可能是:
- 你的maven的配置文件問題
- 引用的mysql-connector-java與driverClassName版本不匹配
如果你的驅(qū)動是com.mysql.cj.jdbc.Driver,你就需要6.x.x的版本。如果是com.mysql.jdbc.Driver 則是5.x.x的版本。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Spring中@PostConstruct的實現(xiàn)方法
大多數(shù)java程序員都使用過@PostConstruct注解,它的作用就是在Bean初始化完成后執(zhí)行,相當(dāng)于我們常說的init()方法,但是我們看@PostConstruct只有單單的一個注解,它到底是如何實現(xiàn)在Bean初始化完成后就被調(diào)用的呢,本文將詳細(xì)給大家介紹一下2023-06-06Redis?command?timed?out兩種異常情況的解決方式
Redis是我們開發(fā)中常用的數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Redis?command?timed?out兩種異常情況的解決方式,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之隊列
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之隊列,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01Java后臺實現(xiàn)瀏覽器一鍵導(dǎo)出下載zip壓縮包
這篇文章主要為大家詳細(xì)介紹了Java后臺實現(xiàn)瀏覽器一鍵導(dǎo)出下載zip壓縮包,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Spring學(xué)習(xí)筆記1之IOC詳解盡量使用注解以及java代碼
這篇文章主要介紹了Spring學(xué)習(xí)筆記1之IOC詳解盡量使用注解以及java代碼 的相關(guān)資料,需要的朋友可以參考下2016-07-07