Commit 22afffd7 authored by 以墨为白's avatar 以墨为白 🎧

3 MybatisGenerator

parent cdb08408
...@@ -121,6 +121,12 @@ ...@@ -121,6 +121,12 @@
<artifactId>orika-core</artifactId> <artifactId>orika-core</artifactId>
<version>1.5.4</version> <version>1.5.4</version>
</dependency> </dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -143,6 +149,21 @@ ...@@ -143,6 +149,21 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
<!-- mybatis逆向工程 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</plugin>
</plugins> </plugins>
<resources> <resources>
<resource> <resource>
......
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;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 代码方式生成逆向代码.MyBatis逆向工程.
* 不通过插件方式运行,因为自定义的注释类用插件方式会报错Cannot instantiate object of type com.zksy.szpt.config.MybatisGenerator
* 直接运行此类即可
*/
public class MybatisGenerator {
public static void main(String[] args) throws Exception {
//MBG执行谊程中的警告信息
List<String> warnings = new ArrayList<>();
//当生成的代码重复时,质盖原代码
boolean overwrite = true;
//读取MBG配置文件
InputStream is = MybatisGenerator.class.getResourceAsStream("./generatorConfig.xml");
ConfigurationParser cParse = new ConfigurationParser(warnings);
Configuration configuration = cParse.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
//创建MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings);
//执行生成代码
myBatisGenerator.generate(null);
//输出警告信息
for (String warning : warnings) {
System.out.println(warning);
}
}
}
package com.zksy.szpt.config;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* 官方的默认不支持根据表注释生成实体类注释
* 通过自定义的方式根据表注释生成实体类注释
*/
public class MybatisGenerator extends DefaultCommentGenerator {
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String currentDateStr;
public MybatisGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString().replace(" ", " "));
field.addJavaDocLine(" */");
}
}
<?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>
<!-- mysql数据源配置文件路径 -->
<properties resource="mysql.properties"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--使用自定义主食类,将表注释写入实体类注释中-->
<commentGenerator type="com.zksy.szpt.config.MybatisGenerator">
<!--去掉mybatisgenerator自己的注释-->
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--配置数据库连接-->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${db.url}"
userId="${db.username}"
password="${db.password}">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--指定生成javabean的位置-->
<javaModelGenerator targetPackage="com.zksy.szpt.domain.po" targetProject=".\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--指定sql的映射文件-->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--指定dao接口生成的位置,mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zksy.szpt.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--table的生成策略-->
<!-- <table tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> -->
<!--可多写 要生成的表名-->
<table tableName="%" schema="szpt">
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
spring: spring:
datasource: datasource:
url: jdbc:mysql://192.168.168.110:3306/swagger?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 url: jdbc:mysql://192.168.168.110:3306/szpt?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
username: root username: root
password: 123456 password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
......
<?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>
<!-- mysql数据源配置文件路径 -->
<properties resource="mysql.properties"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--使用自定义主食类,将表注释写入实体类注释中-->
<commentGenerator type="com.zksy.szpt.config.MybatisGenerator">
<!--去掉mybatisgenerator自己的注释-->
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--配置数据库连接-->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${db.url}"
userId="${db.username}"
password="${db.password}">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--指定生成javabean的位置-->
<javaModelGenerator targetPackage="com.zksy.szpt.domain.po" targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--指定sql的映射文件-->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--指定dao接口生成的位置,mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zksy.szpt.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--table的生成策略-->
<!-- <table tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> -->
<!--可多写 要生成的表名-->
<table tableName="%" schema="szpt">
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
#连接数据库的驱动
jdbc.driver=com.mysql.jdbc.Driver
#数据库连接字符串
db.url=jdbc:mysql://192.168.168.110:3306/szpt?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
db.username=root
db.password=123456
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment