官方文档说明如下
In some cases, MBG will generate MyBatis3 Compatible SQL Map XML Files. MBG generates SQL for simple CRUD functions on each table in a configuration. The generated SQL statements include:
- insert
- update by primary key
- update by example (using a dynamic where clause)
- delete by primary key
- delete by example (using a dynamic where clause)
- select by primary key
- select by example (using a dynamic where clause)
- count by example
There are different variations of these statements depending on the structure of the table (for example, if the table doesn‘t have a primary key, then MBG will not generate an update by primary key function).
大概意思是可以帮我们自动生成一些CRUD操作的代码,方便了平时的开发
在 pom.xml
文件中添加插件配置
<build>
<plugins>
<!--mybatis自动生成代码插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!-- 是否覆盖,true表示会替换生成的JAVA文件,false则不覆盖 -->
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
在resources目录下添加文件 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>
<context id="MySql">
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/数据库名"
userId="用户名"
password="密码"/>
<javaModelGenerator targetPackage="com.generator.entity" targetProject="src/main/java"></javaModelGenerator>
<table tableName="表名"></table>
</context>
</generatorConfiguration>
上面的配置只会生成一个与表结构对应的实体类,生成其他的配置下文会再说明。
点击IDEA右边栏的maven,可以看到之前导入的插件,双击运行
就会在目录 com.generator.entity
下生成文件了,此时只会生成一个实体类文件。
如果想还想生成映射文件,可以修改配置文件
<?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>
<!-- defaultModelType="flat" 设置复合主键时不单独为主键创建实体 -->
<context id="MySql" defaultModelType="flat">
<!-- 生成的POJO实现java.io.Serializable接口 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!--注释-->
<commentGenerator>
<!-- 将数据库中表的字段描述信息添加到注释 -->
<property name="addRemarkComments" value="true"/>
<!-- 注释里不添加日期 -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库连接 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://172.22.23.210:3306/money_moment"
userId="kf_root"
password="H_,,3Hbc_3bbb_253H7c35d0,#3d"/>
<!-- 生成POJO对象,并将类放到com.generator.entity包下 -->
<javaModelGenerator targetPackage="com.generator.entity" targetProject="src/main/java"></javaModelGenerator>
<!-- 生成mapper xml文件,并放到resources下的mapper文件夹下 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"></sqlMapGenerator>
<!-- 生成mapper xml对应dao接口,放到com.generator.mapper包下-->
<javaClientGenerator targetPackage="com.generator.mapper" targetProject="src/main/java" type="XMLMAPPER"></javaClientGenerator>
<!-- table标签可以有多个,至少一个,tableName指定表名,可以使用_和%通配符 -->
<table tableName="t_feed_record">
<!-- 是否只生成POJO对象 -->
<property name="modelOnly" value="false"/>
<!-- 数据库中表名有时我们都会带个前缀,而实体又不想带前缀,这个配置可以把实体的前缀去掉 -->
<domainObjectRenamingRule searchString="^T" replaceString=""/>
</table>
</context>
</generatorConfiguration>
点击生成,效果如下:
参考
https://blog.csdn.net/gnail_oug/article/details/80404870
mybatis-generator官方文档:http://mybatis.org/generator/index.html
原文:https://www.cnblogs.com/codeclock/p/12455422.html