IDEA實(shí)現(xiàn)通過generator自動(dòng)生成實(shí)體類、dao以及mapper文件
這個(gè)配置看似簡單,還是踩了很多的坑,百度了一圈,總算把所有的問題都解決了,所以寫個(gè)文檔記錄一下,把所有踩的坑和解決的方法都寫一下,防止忘了。
步驟
- 在pom.xml文件中導(dǎo)入mybatis generator 自動(dòng)生成代碼插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>- 在resources先新建generator/generatorConfiguration.xml文件,進(jìn)行如下配置,該配置是自定義的,可以根據(jù)自己的需求進(jìn)行定義,百度下語法進(jìn)行下改造:
<?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>
<!-- 數(shù)據(jù)庫驅(qū)動(dòng):選擇你的本地硬盤上面的數(shù)據(jù)庫驅(qū)動(dòng)包-->
<classPathEntry location="D:\maven\mvnrepository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar"/>
<context id="mysql" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--數(shù)據(jù)庫鏈接URL,用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/vue_shop" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.hwh.vue_demo.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.hwh.vue_demo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是數(shù)據(jù)庫中的表名或視圖名 domainObjectName是實(shí)體類名-->
<table schema="" tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" ></table>
</context>
</generatorConfiguration>- 打開maven窗口,運(yùn)行即可

整合lombok
因?yàn)槭褂胦mbok可以使代碼更加的簡潔,所以百度了一圈,找到了整合的方法
下載 mybatis-generator-lombok-plugin插件
github網(wǎng)址
https://github.com/GuoGuiRong/mybatis-generator-lombok-plugin/blob/master/README.md
可以通過git clone的方式下載到自己放置項(xiàng)目的文件夾中
啟動(dòng)之后,然后使用maven clean install將這個(gè)項(xiàng)目添加到Maven倉庫里去

- pom.xml文件配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自動(dòng)生成代碼插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>com.chrm</groupId>
<artifactId>mybatis-generator-lombok-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>在generatorConfiguration.xml加入如下配置

- 大功告成
解決生成的mapping中的.xml文件出現(xiàn)代碼重復(fù)的問題
- 在schema中加入數(shù)據(jù)庫的名稱

- 然后再數(shù)據(jù)庫的連接中加入如下配置,完美解決

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解如何通過Java實(shí)現(xiàn)類似Nginx代理
最近遇到一個(gè)問題,在內(nèi)網(wǎng)環(huán)境中部署的項(xiàng)目需要調(diào)用外網(wǎng)完成一些應(yīng)用,一般情況我們可以通過增加一臺(tái)機(jī)器,部署到可以訪問外網(wǎng)的服務(wù)器上,然后內(nèi)網(wǎng)直接連接該機(jī)器通過Nginx進(jìn)行代理即可,所以本文介紹了如何通過Java實(shí)現(xiàn)類似Nginx代理,需要的朋友可以參考下2024-08-08
關(guān)于ZooKeeper的會(huì)話機(jī)制Session解讀
這篇文章主要介紹了關(guān)于ZooKeeper的會(huì)話機(jī)制Session解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Java后端Spring?Boot全局異常處理最佳實(shí)踐記錄
Spring Boot通過和提供強(qiáng)大的異常處理機(jī)制,支持統(tǒng)一REST和頁面錯(cuò)誤響應(yīng),這篇文章主要介紹了Java后端Spring?Boot全局異常處理的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-08-08
舉例講解Java設(shè)計(jì)模式編程中Decorator裝飾者模式的運(yùn)用
這篇文章主要介紹了Java設(shè)計(jì)模式編程中Decorator裝飾者模式的運(yùn)用,裝飾者模式就是給一個(gè)對(duì)象動(dòng)態(tài)的添加新的功能,裝飾者和被裝飾者實(shí)現(xiàn)同一個(gè)接口,裝飾者持有被裝飾者的實(shí)例,需要的朋友可以參考下2016-05-05
Java Web基于Session的登錄實(shí)現(xiàn)方法
這篇文章主要介紹了Java Web基于Session的登錄實(shí)現(xiàn)方法,涉及Java針對(duì)session的操作及表單提交與驗(yàn)證技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

