隶属于文章系列:技术架构 https://www.jianshu.com/p/4fa9b271bdcf
在mysql建库、建表后,用mybatis自动生成代码:
- 配置generatorConfig.xml
- 修改maven的pom.xml
- 执行生成命令
配置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>
<!-- 指定connector jar包的路径 -->
<classPathEntry
location="D:\mavenrepo\mysql\mysql-connector-java\5.0.5\mysql-connector-java-5.0.5.jar" />
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://172.20.176.138:3306/auth" userId="dishy" password="dishy"/>
<!-- model javafile -->
<javaModelGenerator targetPackage="kbds.auth.model"
targetProject="D:/mybatis">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射文件 mapper xml 所在包和路径-->
<sqlMapGenerator targetPackage="kbds.auth.mapper"
targetProject="D:/mybatis">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 接口 interface 所在包和路径-->
<javaClientGenerator targetPackage="kbds.auth.dao"
targetProject="D:/mybatis"
type="XMLMAPPER">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 表和bean的对应关系-->
<table tableName="t_group" domainObjectName="Group"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
修改maven的pom.xml
在build--》plugins内添加新的plugin
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml
</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行生成命令
进入pom.xml 所在目录:
mvn -e -X mybatis-generator:generate
在D:/mybatis
下可以看到相关文件,复制到IDE目录。