1.pom.xml引入jar
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
建dbconfig.properties:
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=mysql
2.generatorConfig.xml
<commentGenerator type=".....">自己重写注释类要加type
<?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="DB2Tables" targetRuntime="MyBatis3">
<!-- 自动生成序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!-- 自动生成toString-->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- 是否去除自动生成的注释 -->
<commentGenerator type=".....">
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!-- 配置数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud" userId="root"
password="mysql">
</jdbcConnection>
<!-- 默认为false,把JDBC DECIMAL 和NUMERIC类型解析为Integer,为true时把JDBC DECIMAL 和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定javaBean生成的位置,targetPackage:表示生成的路径,targetProject:表示生成的文件/项目下-->
<javaModelGenerator targetPackage="cn.zhou.shu.ssm.bean" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- dao接口生成的位置,mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.zhou.shu.ssm.dao" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- table指定每个表的生成策略 -->
<table tableName="tbl_emp" domainObjectName="Employee"></table>
<table tableName="tbl_dept" domainObjectName="Department"></table>
</context>
</generatorConfiguration>
3.逆向工程启动类:
package cn.zhou.shu.ssm.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class test {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/main/resources/generatorConfig.xml");
// 创建配置解析器
ConfigurationParser cp = new ConfigurationParser(warnings);
// 调用解析器创建配置对象()
Configuration config = cp.parseConfiguration(configFile);
// 创建一个ShellCallback对象,shellCallback接口是处理文件的创建和合并,默认是不支持文件合并的。
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
// 创建一个MyBatisGenerator对象。MyBatisGenerator类是真正用来执行生成动作的类
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);
myBatisGenerator.generate(null);
}
}
注意运行后要刷新项目
项目结构图:
------------------------------------------------------------------------------------------------------------------------------------
https://blog.csdn.net/u011781521/article/details/78161201
https://blog.csdn.net/sinat_34104446/article/details/88675224
补充:
生成自定义注释:
1.MyBatis逆向工程默认使用的是org.mybatis.generator.api.CommentGenerator类的
org.mybatis.generator.internal.DefaultCommentGenerator子类生成的注释,
所以我们可以自己实现一个注释类:
2.generatorConfig.xml中
<!-- 通过type指定自定义的注释 -->
<commentGenerator type="cn.zhou.shu.ssm.test.MyCommentGenerator">
<!-- 不要开启,否则将不会使用自定义注释 -->
<!-- <property name="suppressAllComments" value=&