Mybatis自動(dòng)生成實(shí)體類(lèi)和Mapper文件
我們?cè)谧鲰?xiàng)目時(shí),少不了用到Mybatis框架往枣,更少不了創(chuàng)建實(shí)體類(lèi)和書(shū)寫(xiě)sql語(yǔ)句伐庭,當(dāng)數(shù)據(jù)庫(kù)表很多時(shí),手動(dòng)創(chuàng)建會(huì)耗時(shí)耗力分冈,做著大量重復(fù)性的工作圾另,很讓人頭疼,下面我將介紹如何配置利用Mybatis插件自動(dòng)創(chuàng)建生成雕沉。
- 在pom.xml中添加如下依賴(lài):
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<build>
<finalName>springboot-mybatis</finalName>
<plugins>
<!--設(shè)置生成實(shí)體類(lèi)和mapper的插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose><!--允許移動(dòng)生成文件-->
<overwrite>true</overwrite><!--允許自動(dòng)覆蓋文件-->
<tableNames>tableName</tableNames> <!--你要生成的表名-->
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 在resources目錄下創(chuàng)建generatorConfig.properties文件集乔,并添加如下的配置:
#生成實(shí)體類(lèi)的所需數(shù)據(jù)連接參數(shù)
driverClass= com.mysql.cj.jdbc.Driver
connectionURL=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username=root
password=123456
# 生成的實(shí)體類(lèi)和包的路徑
modelPackage=com.demo.tools.domain
daoPackage=com.demo.tools.persistence
- 在resources目錄下創(chuàng)建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="generatorConfig.properties"/>
<context id="mysqlTables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
<commentGenerator>
<property name="type" value="DEFAULT"/>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<!-- 這個(gè)元素用來(lái)去除指定生成的注釋中是否包含生成的日期 false:表示保護(hù) -->
<!-- 如果生成日期,會(huì)造成即使修改一個(gè)字段坡椒,整個(gè)實(shí)體類(lèi)所有屬性都會(huì)發(fā)生變化扰路,不利于版本控制,所以設(shè)置為true -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--數(shù)據(jù)庫(kù)鏈接URL倔叼,用戶(hù)名汗唱、密碼 -->
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}"
userId="${username}"
password="${password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="${modelPackage}" targetProject="MAVEN">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成DAO的包名和位置-->
<sqlMapGenerator targetPackage="${sqlMapPackage}" targetProject="MAVEN">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="${daoPackage}" targetProject="MAVEN">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="tableName" domainObjectName="TableName" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
注意在上面的"<table tableName='tableName'"....>把你要生成的表名換過(guò)去,比如你要生成person表,你就這樣寫(xiě):
<table tableName="person" domainObjectName="Person" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
-
在IDEA內(nèi)置的Terminal中輸入命令:mvn clean mybatis-generator:generate
如圖,即可成功:
success
然后到你的target目錄下,找到你要生成的文件,復(fù)制到你的項(xiàng)目里就好了!!!
image.png