MyBatis Generator代码自动生成
- 当前为Maven项目,修改pom.xml文件,增加MyBatis Generator和MySQL的jar包支持
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.z-f</groupId>
<artifactId>simpleerp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>simpleerp</name>
<url>http://maven.apache.org</url>
<!-- spring boot基本环境 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot基本环境 开始 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot基本环境 结束 -->
<!-- MyBatis Generator 以及MySQL驱动支持 -->
<!-- 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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MyBatis Generator 以及MySQL驱动支持 -->
</dependencies>
<build>
<finalName>simpleerp</finalName>
<plugins>
<!-- spring-boot-maven-plugin插件就是打包spring boot应用的 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- MyBatis Generator 自动生成编译支持 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<!-- MyBatis Generator 自动生成编译支持 -->
</plugins>
</build>
</project>
- 在
src/main/resources文件夹下增加数据库链接配置文件generatorDbSet.properties
# MyBatis Generator db
# mysql driver
db.driverLocation=E:/resources/repository/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://xxx.xxx.x.xxx:3306/dbName?characterEncoding=utf-8
db.username=root
db.password=xxxxx
- 在
src/main/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>
<!--导入属性配置 -->
<properties resource="generatorDbSet.properties"></properties>
<!-- 指定数据库驱动的jdbc驱动jar包的位置 -->
<classPathEntry location="${db.driverLocation}" />
<!-- context 是逆向工程的主要配置信息 -->
<!-- id:起个名字 -->
<!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
<context id="default" targetRuntime="MyBatis3">
<!--optional,旨在创建class时,对注释进行控制 -->
<!-- <commentGenerator>
<property name="suppressDate" value="true" />
是否去除自动生成的注释 true:是 : false:否
<property name="suppressAllComments" value="true" />
</commentGenerator> -->
<!-- 自定义生成,可以生成数据库中文注释 -->
<commentGenerator type="top.z_f.simpleerp.generator.MySQLGenerator">
<property name="author" value="cupid.zhang"/>
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${db.driverClassName}"
connectionURL="${db.url}" userId="${db.username}" password="${db.password}">
<!-- 设置 useInformationSchema 属性为 true -->
<property name="useInformationSchema" value="true" />
</jdbcConnection>
<!--非必须,类型处理器,在数据库类型和java类型之间的转换控制 -->
<javaTypeResolver>
<!-- 默认情况下数据库中的 decimal,bigInt 在 Java 对应是 sql 下的 BigDecimal 类 -->
<!-- 不是 double 和 long 类型 -->
<!-- 使用常用的基本类型代替 sql 包下的引用类型 -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetPackage:生成的实体类所在的包 -->
<!-- targetProject:生成的实体类所在的硬盘位置 -->
<javaModelGenerator targetPackage="top.z_f.simpleerp.entity"
targetProject="src/main/java">
<!-- 是否允许子包 -->
<property name="enableSubPackages" value="true" />
<!-- 是否对modal添加构造函数 -->
<property name="constructorBased" value="true" />
<!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true" />
<!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
<property name="immutable" value="false" />
</javaModelGenerator>
<!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
<sqlMapGenerator targetPackage="mappers"
targetProject="src/main/resources">
<!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="top.z_f.simpleerp.dao" targetProject="src/main/java">
<!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- <table tableName="zf_account" domainObjectName="zfAccount"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table> -->
<!-- 生成指定开头的所有表,可以声明多个,如果生成一个则需要指定表名 -->
<table tableName="zf_%"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="sys_%"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
- 在
src/main/java文件夹下增加包top.z_f.simpleerp.generator
- 在包
top.z_f.simpleerp.generator下增加注释文件自定义代码MySQLGenerator.java
package top.z_f.simpleerp.generator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
/**
*
* MyBaits Generator自动生成代码注释修改为数据库备注名称
*
* @author zhangzhen
* @date 2019-05-24
*/
public class MySQLGenerator extends DefaultCommentGenerator {
private Properties properties;
public MySQLGenerator() {
properties = new Properties();
}
@Override
public void addConfigurationProperties(Properties properties) {
// 获取自定义的 properties
this.properties.putAll(properties);
}
/**
* 类注释设定
*/
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
String author = properties.getProperty("author");
String dateFormat = properties.getProperty("dateFormat", "yyyy-MM-dd");
SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
// 获取表注释
String remarks = introspectedTable.getRemarks();
topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine(" * " + remarks);
topLevelClass.addJavaDocLine(" *");
topLevelClass.addJavaDocLine(" * @author " + author);
topLevelClass.addJavaDocLine(" * @date " + dateFormatter.format(new Date()));
topLevelClass.addJavaDocLine(" */");
}
/**
* 其他方法设定
*/
@Override
public void addGeneralMethodComment(Method method,
IntrospectedTable introspectedTable) {
// method.addJavaDocLine("/**");
// method.addJavaDocLine(" * ");
// method.addJavaDocLine(" */");
}
/**
* 字段属性注释设定
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// 获取列注释
String remarks = introspectedColumn.getRemarks();
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + remarks.replace("\r\n", "\r\n\t * "));
field.addJavaDocLine(" */");
}
/**
* get方法注释设定
*/
@Override
public void addGetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// 获取列注释
String remarks = introspectedColumn.getRemarks();
method.addJavaDocLine("/**");
method.addJavaDocLine(" * 获取 ");
method.addJavaDocLine(" * " + remarks.replace("\r\n", "\r\n\t * "));
method.addJavaDocLine(" */");
}
/**
* set方法注释设定
*/
@Override
public void addSetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// 获取列注释
String remarks = introspectedColumn.getRemarks();
method.addJavaDocLine("/**");
method.addJavaDocLine(" * 设置 ");
method.addJavaDocLine(" * " + remarks.replace("\r\n", "\r\n\t * "));
method.addJavaDocLine(" */");
}
}
- 在包
top.z_f.simpleerp.generator下增加自动生成代码主方法GeneratorTool.java
package top.z_f.simpleerp.generator;
import java.io.IOException;
import java.sql.SQLException;
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.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.cglib.beans.BeanCopier.Generator;
/**
*
* MyBatis自动生成
* @author zhangzhen
*
*/
public class GeneratorTool {
/**
* 自动生成entity、mappers和dao
* @param args
*/
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
// 加载生成规则配置文件
config = cp.parseConfiguration(
Generator.class.getResourceAsStream("/generatorConfig.xml"));
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 右键单击
Run As --> Java Application运行即可
上一篇:Eclipse 创建spring boot 下一篇:spring boot使用druid管理数据库连接池
首页 > 学习总览 > 开发语言 > Java