mybatis-generator生成器的使用
记录一下怎么使用这个插件,首先声明我使用的是springboot。
1.pom文件引入插件
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- 主要需要改一下配置文件的位置 -->
<configurationFile>generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
这里主要是要注意配置文件generatorConfig.xml
的位置,就是我注释的那行。我把配置文件和pom文件放在同级目录。也建议都这么做,后面配置文件就可以少改一点。
2.配置generatorConfig.xml
然后是修改配置文件,主要改这么几处:
- jdbc驱动位置
- 数据库的连接信息
- 实体类,dao,xml的包路径及文件路径。
如果你的配置文件generatorConfig.xml
也放在和pom同级目录,这里的文件路径就不怎么需要动,只要改一下包路径。
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>
<!--JDBC驱动jar包的位置,这里就是本地仓库的位置,自己项目配置的mysql驱动-->
<classPathEntry
location="E:\Users\EDZ\.m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/数据库名?tinyInt1isBit=false"
userId="******"
password="*****"/>
<!-- 实体类 -->
<javaModelGenerator targetPackage="com.power.tools.demo.module.admin.model"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- xml文件 -->
<sqlMapGenerator targetPackage="mapping"
targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- dao层 -->
<javaClientGenerator targetPackage="com.power.tools.demo.module.admin.dao"
targetProject="./src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 这里有几张表就复制几个 -->
<table tableName="t_b_account" domainObjectName="Account"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
3.运行插件
前面都配置正确的话,这里就运行一下插件就好了。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-75izeeeE-1579423353179)(mybatis-generator生成器的使用/1579422447644.png)]
就可以看到生成的文件了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qc5DesvU-1579423353181)(mybatis-generator生成器的使用/1579423337454.png)]
行一下插件就好了。
就可以看到生成的文件了
应该还有很多功能,如果你需要一些特别的功能的话,可以去mybatis官网看一下这个插件还有什么地方可以配置的。