MyBatis?Generator快速生成實體類和映射文件的方法
一、MyBatis Generator 的使用
1.1、生成類和映射文件
1.1.1、在 pom.xml 中引入依賴
在 properties 標簽中加入版本號.
<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>
在 build => plugins 標簽中加入如下配置
<!-- 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>
上述配置中需要注意的是 “配置文件路徑”,這個路徑就是用來生成 實體類和映射文件 配置規(guī)則的位置.
1.1.2、根據(jù) configurationFile 標簽中配置的路徑 創(chuàng)建 generatorConfig.xml 文件
這個文件就是用來描述生成規(guī)則的.
根據(jù)路徑(src/main/resources/mybatis),在 mybatis 目錄下創(chuàng)建 generatorConfig.xml 文件.
Ps:下述配置文件中需要修改的有 數(shù)據(jù)庫連接、實體類和映射文件的路徑、數(shù)據(jù)庫表名
<?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ū)動包路徑,location中路徑替換成??本地路徑 -->
<classPathEntry location="D:\class\source\mysql-connector-java-5.1.49.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 禁??動?成的注釋 -->
<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>
<!-- 實體類?成位置 -->
<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>
<!-- 配置?成表與實例, 只需要修改表名tableName, 與對應類名domainObjectName 即
可-->
<table tableName="j_article" domainObjectName="Article"
enableSelectByExample="false"
enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
enableCountByExample="false"
enableUpdateByExample="false">
<!-- 類的屬性?數(shù)據(jù)庫中的真實字段名做為屬性名, 不指定這個屬性會?動轉(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ū)動包路徑是自己本地倉庫的路徑

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

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

接著你就可以看到對應的生成了

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

2)給啟動類上 或者 新建一個配置類(有 @Configuration 注解)加上 @MapperScan("com.example.cyk.mapper") 注解.
1.1.6、配置 mybatis
在 yml 文件中配置
mybatis: mapper-locations: classpath:mapper/**/*Mapper.xml
1.1.7、測試
@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如何快速生成實體類和映射文件的文章就介紹到這了,更多相關(guān)MyBatis Generator生成 實體類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis打印的sql日志不寫入到log文件的問題及解決
這篇文章主要介紹了mybatis打印的sql日志不寫入到log文件的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
解決idea check out 切換分支時找不到需要的分支問題
這篇文章主要介紹了解決idea check out 切換分支時找不到需要的分支問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
項目總結(jié)之HttpURLConnection的disconnect的問題
這篇文章主要介紹了項目總結(jié)之HttpURLConnection的disconnect的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Springboot處理跨域的實現(xiàn)方式(附Demo)
這篇文章主要介紹了Springboot處理跨域的實現(xiàn)方式(附Demo),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

