詳解在idea 中使用Mybatis Generator逆向工程生成代碼
通過MAVEN完成 Mybatis 逆向工程
1. POM文件中添加插件
在 pom 文件的build 標(biāo)簽中 添加 plugin 插件和 數(shù)據(jù)庫連接 jdbc 的依賴。
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
<configuration>
<!-- 輸出詳細(xì)信息 -->
<verbose>true</verbose>
<!-- 覆蓋生成文件 -->
<overwrite>true</overwrite>
<!-- 定義配置文件 -->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
若不在pom文件中引入數(shù)據(jù)庫連接依賴,也可在配置文件中通過本地方式啟動(dòng)連接。
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ú)配置屬性,可將其配入properties后 通過此方式導(dǎo)入屬性 ${userId} -->
<!-- <properties resource="generator.properties"></properties>-->
<!-- 數(shù)據(jù)庫驅(qū)動(dòng): 若之前未在build里配置數(shù)據(jù)庫驅(qū)動(dòng)包,可選擇本地硬盤上面的數(shù)據(jù)庫驅(qū)動(dòng)包-->
<classPathEntry location="D:\Maven\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
<!-- targetRuntime 默認(rèn)為MyBatis3DynamicSql,該值不會(huì)生成xml文件, 可選擇Mybatis3 -->
<context id="default" targetRuntime="Mybatis3">
<!-- optional,旨在創(chuàng)建class時(shí),對注釋進(jìn)行控制 -->
<commentGenerator>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置數(shù)據(jù)庫連接 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai"
userId="root"
password="123456">
</jdbcConnection>
<!-- 非必需,類型處理器,在數(shù)據(jù)庫類型和java類型之間的轉(zhuǎn)換控制-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類
targetPackage 指定生成的model生成所在的包名
targetProject 指定在該項(xiàng)目下所在的路徑
-->
<javaModelGenerator targetPackage="com.demo.dao.pojo" targetProject="src/main/java">
<!-- 是否允許子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
<!-- 是否對model添加 構(gòu)造函數(shù) -->
<property name="constructorBased" value="false"/>
<!-- 是否對類CHAR類型的列的數(shù)據(jù)進(jìn)行trim操作 -->
<property name="trimStrings" value="false"/>
<!-- 建立的Model對象是否 不可改變 即生成的Model對象不會(huì)有 setter方法,只有構(gòu)造方法 -->
<property name="immutable" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper" 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文件和獨(dú)立的Mapper接口
-->
<javaClientGenerator targetPackage="com.demo.dao.mapper" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table tableName="aging_demotion" domainObjectName="AgingDemotion"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 插入時(shí)自動(dòng)返回主鍵ID -->
<generatedKey column="aging_demotion_id" sqlStatement="Mysql" identity="true" />
</table>
<table tableName="aging_listener" domainObjectName="AgingListener"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="aging_state" domainObjectName="AgingState"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
XML配置信息可參考mybatis官網(wǎng):http://mybatis.org/generator/configreference/xmlconfig.html
3.通過maven啟動(dòng)

點(diǎn)擊mybatis-generate:generate即可生成對應(yīng) java,mapper 和 pojo實(shí)體類。(若maven沒有顯示此插件,可點(diǎn)擊左上角刷新)
4.Insert時(shí)返回自增主鍵
通過generatedKey 使其插入時(shí)返回ID,其值必須為數(shù)值型自增主鍵。
其逆向生成的代碼為:
<selectKey keyProperty="agingDemotionId" order="AFTER" resultType="java.lang.Long"> SELECT LAST_INSERT_ID() </selectKey>
也可自己通過這種方式實(shí)現(xiàn)返回自增ID。
<insert id="insert" useGeneratedKeys="true" keyProperty="agingDemotionId"
parameterType="com.jd.aging.presentation.domain.AgingDemotionEntity">
這種方式只適用于傳入對象時(shí),insert方法成功依舊返回的是 1, 不過傳入的實(shí)體類對象中 主鍵 ID 的值 不再為 null, 而是獲得該插入實(shí)體類的主鍵ID值。

到此這篇關(guān)于詳解在idea 中使用Mybatis Generator逆向工程生成代碼的文章就介紹到這了,更多相關(guān)MAVEN完成 Mybatis 逆向工程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談使用java實(shí)現(xiàn)阿里云消息隊(duì)列簡單封裝
這篇文章主要介紹了淺談使用java實(shí)現(xiàn)阿里云消息隊(duì)列簡單封裝,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
java實(shí)現(xiàn)本地日期時(shí)間處理
這篇文章主要介紹了本地日期時(shí)間處理的程序,實(shí)現(xiàn)了下面的功能,大家參考使用吧2014-01-01
java編程實(shí)現(xiàn)獲取服務(wù)器IP地址及MAC地址的方法
這篇文章主要介紹了java編程實(shí)現(xiàn)獲取機(jī)器IP地址及MAC地址的方法,實(shí)例分析了Java分別針對單網(wǎng)卡及多網(wǎng)卡的情況下獲取服務(wù)器IP地址與MAC地址的相關(guān)技巧,需要的朋友可以參考下2015-11-11
在windows下揪出java程序占用cpu很高的線程并完美解決
這篇文章主要介紹了在windows下揪出java程序占用cpu很高的線程并完美解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
SpringBoot中使用@Scheduled注解創(chuàng)建定時(shí)任務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot中使用@Scheduled注解創(chuàng)建定時(shí)任務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

