MyBatis?Generator快速生成實(shí)體類和映射文件的方法
一、MyBatis Generator 的使用
1.1、生成類和映射文件
1.1.1、在 pom.xml 中引入依賴
在 properties 標(biāo)簽中加入版本號(hào).
<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>
在 build => plugins 標(biāo)簽中加入如下配置
<!-- mybatis ?成器插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis-generator-plugin-version}</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>deploy</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!-- 相關(guān)配置 -->
<configuration>
<!-- 打開?志 -->
<verbose>true</verbose>
<!-- 允許覆蓋 -->
<overwrite>true</overwrite>
<!-- 配置?件路徑 -->
<configurationFile>
src/main/resources/mybatis/generatorConfig.xml
</configurationFile>
</configuration>
</plugin>
上述配置中需要注意的是 “配置文件路徑”,這個(gè)路徑就是用來生成 實(shí)體類和映射文件 配置規(guī)則的位置.
1.1.2、根據(jù) configurationFile 標(biāo)簽中配置的路徑 創(chuàng)建 generatorConfig.xml 文件
這個(gè)文件就是用來描述生成規(guī)則的.
根據(jù)路徑(src/main/resources/mybatis),在 mybatis 目錄下創(chuàng)建 generatorConfig.xml 文件.
Ps:下述配置文件中需要修改的有 數(shù)據(jù)庫(kù)連接、實(shí)體類和映射文件的路徑、數(shù)據(jù)庫(kù)表名
<?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>
<!-- 驅(qū)動(dòng)包路徑,location中路徑替換成??本地路徑 -->
<classPathEntry location="D:\class\source\mysql-connector-java-5.1.49.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 禁??動(dòng)?成的注釋 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 連接配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/javanav_db?
characterEncoding=utf8&useSSL=false"
userId="root"
password="1111">
</jdbcConnection>
<javaTypeResolver>
<!-- ?數(shù)統(tǒng)?轉(zhuǎn)為BigDecimal -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 實(shí)體類?成位置 -->
<javaModelGenerator targetPackage="com.example.cyk.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- mapper.xml?成位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- mapper 接口?成位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.example.cyk.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 配置?成表與實(shí)例, 只需要修改表名tableName, 與對(duì)應(yīng)類名domainObjectName 即
可-->
<table tableName="j_article" domainObjectName="Article"
enableSelectByExample="false"
enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
enableCountByExample="false"
enableUpdateByExample="false">
<!-- 類的屬性?數(shù)據(jù)庫(kù)中的真實(shí)字段名做為屬性名, 不指定這個(gè)屬性會(huì)?動(dòng)轉(zhuǎn)換 _ 為
駝峰命名規(guī)則-->
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="j_article_reply" domainObjectName="ArticleReply"
enableSelectByExample="false"
enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
enableCountByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="j_board" domainObjectName="Board"
enableSelectByExample="false" enableDeleteByExample="false"
enableDeleteByPrimaryKey="false" enableCountByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="j_message" domainObjectName="Message"
enableSelectByExample="false"
enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
enableCountByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="j_user" domainObjectName="User"
enableSelectByExample="false" enableDeleteByExample="false"
enableDeleteByPrimaryKey="false" enableCountByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>注意:
驅(qū)動(dòng)包路徑是自己本地倉(cāng)庫(kù)的路徑

但一定注意!! 需要在非中文的目錄下,因此你可以把這個(gè)驅(qū)動(dòng)包拷貝出來,放到一個(gè)非中文的目錄中即可.

1.1.3、自動(dòng)生成類 和 映射文件
重新加載Maven項(xiàng)?,在Plugins節(jié)點(diǎn)下出現(xiàn)mybatis-generator,雙擊運(yùn)?,在對(duì)應(yīng)的目錄下?成相應(yīng)的類與映射?件:

接著你就可以看到對(duì)應(yīng)的生成了

1.1.4、在 Insert 標(biāo)簽中添加獲取主鍵值的選項(xiàng)
在生成的 xml 文件中,給每一個(gè) insert 標(biāo)簽都添加以下屬性:useGeneratedKeys="true" keyProperty="id"
<!-- useGeneratedKeys = true --> <!-- keyProperty = 主鍵字段--> <!-- 當(dāng)插??條數(shù)據(jù)后,可以通過user.getId()獲取到?動(dòng)?成的Id值,如果?法中需要?即獲取Id值,加?這個(gè)配置 --> <insert id="insert" parameterType="com.example.cyk.model.User" useGeneratedKeys="true" keyProperty="id" >
Ps:這個(gè)選項(xiàng)也可以自動(dòng)生成,但是不理想(有些問題)
1.1.5、掃描配置:添加 @Mapper 注解 / 添加掃描注解
有兩種方式配置掃描 Mapper 接口.
1)給每個(gè) mapper 包下的 mapper 接口都添加 @Mapper 注解.

2)給啟動(dòng)類上 或者 新建一個(gè)配置類(有 @Configuration 注解)加上 @MapperScan("com.example.cyk.mapper") 注解.
1.1.6、配置 mybatis
在 yml 文件中配置
mybatis: mapper-locations: classpath:mapper/**/*Mapper.xml
1.1.7、測(cè)試
@SpringBootTest
public class TestMapper {
@Autowired
private UserMapper userMapper;
@Test
public void select() {
User user = userMapper.selectByPrimaryKey(1L);
System.out.println(user);
}
}

到此這篇關(guān)于MyBatis Generator如何快速生成實(shí)體類和映射文件的文章就介紹到這了,更多相關(guān)MyBatis Generator生成 實(shí)體類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis打印的sql日志不寫入到log文件的問題及解決
這篇文章主要介紹了mybatis打印的sql日志不寫入到log文件的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
在Spring Boot框架中使用AOP的正確姿勢(shì)
aop是spring的兩大功能模塊之一,功能非常強(qiáng)大,為解耦提供了非常優(yōu)秀的解決方案。下面這篇文章主要給大家介紹了如何在Spring Boot框架中使用AOP的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-08-08
解決idea check out 切換分支時(shí)找不到需要的分支問題
這篇文章主要介紹了解決idea check out 切換分支時(shí)找不到需要的分支問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
項(xiàng)目總結(jié)之HttpURLConnection的disconnect的問題
這篇文章主要介紹了項(xiàng)目總結(jié)之HttpURLConnection的disconnect的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Springboot處理跨域的實(shí)現(xiàn)方式(附Demo)
這篇文章主要介紹了Springboot處理跨域的實(shí)現(xiàn)方式(附Demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Spring?注入集合實(shí)現(xiàn)過程示例詳解
這篇文章主要為大家介紹了Spring?注入集合實(shí)現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

