目录
一、Springboot 结合 Mybatis-generator
官方文档
一、Springboot 结合 Mybatis-generator
1,修改Pom文件添加Maven引用,添加完成在idea右侧可看到mybatis-generator:generate插件。
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>false</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>src/main/resources/mybatis-generater.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
2,在resources添加文件mybatis-generater.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>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://172.16.2.100:3306/my_dubbo_db?serverTimezone=UTC"
userId="root"
password="root">
<property name="useInformationSchema" value="true"/>
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.study.wzf.myuserprovider.dal.entity"
targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.study.wzf.myuserprovider.dal.persistence" targetProject="src/main/java"/>
<javaClientGenerator targetPackage="com.study.wzf.myuserprovider.dal.persistence"
targetProject="src/main/java"
type="XMLMAPPER"/>
<!--已生成的表
<table tableName="tb_payment" domainObjectName="Payment"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
-->
<table tableName="wzf_member" domainObjectName="WfMember"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="wzf_member_address" domainObjectName="WfMemberAddress"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
3,双击mybatis-generator:generate,即可生成对应的代码。
二,Spring结合 Mybatis-generator
1,其他不变,添加maven插件,添加配置文件mybatis-generater.xml
2,修改配置文件,这样可以更灵活的生成代码,如下:
<!--导入属性配置-->
<properties resource="application-test.properties"/>
<!--数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
<property name="useInformationSchema" value="true"/>
</jdbcConnection>