Ms-springboot: Open source MES architecture -Mybatis Generator
Mybatis Generator is a code generator for Mybatis Mybatis and iBATIS. It will generate code for all versions of MyBatis and iBATIS versions after version 2.2.0. It will introspect the database tables (or many tables) and will generate artifacts that can be used to access the tables. This reduces the initial hassle of setting up objects and configuration files to interact with database tables. MBG is designed to have a significant impact on the large number of database operations that are simple CRUD (create, retrieve, update, delete). You still need to manually write SQL and objects for join queries or stored procedures.
1. Modify the SpringBoot configuration file
Add data source configuration and mapper.xml path configuration for MyBatis in application.yml
server:
port: 9999
spring:
application:
name: mes-demo
datasource:
url: jdbc:mysql://localhost:3306/mymes? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: reader
password: 123456
mybatis:
mapper-locations:
- classpath:mapper/*.xml
- classpath*:com/**/mapper/*.xml
Copy the code
Add the data source configuration in application.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/mymes? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.userId=reader
jdbc.password=123456
Copy the code
Mybatis Generator configuration file
<? 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="application.properties"/>
<context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<! Generate serialization method for model -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<! Create a toString method for the generated Java model
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<! Create mapper.xml and overwrite the original file.
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<! -- You can customize the code comment to generate model -->
<commentGenerator type="com.cn.mymes.mgb.MyMesCommentGenerator">
<! Whether to remove automatically generated commentstrue: it is:false: no - >
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<! -- Configure database connection -->
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
<! Mysql > update mysql > update mysql > update mysql > update mysql
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<! -- Specify the path to generate the model -->
<javaModelGenerator targetPackage="com.cn.mymes.mgb.model" targetProject="src\main\java"/>
<! Mapper.xml -->
<sqlMapGenerator targetPackage="com.cn.mymes.mgb.mapper" targetProject="src\main\resources"/>
<! -- Specify the path to generate mapper interface -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.cn.mymes.mgb.mapper"
targetProject="src\main\java"/>
<! TableName = %-->
<! Create a single table tableName set to for table names -->
<table tableName="%">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
Copy the code
Run MyMesGenerator’s main function to generate the code
package com.cn.mymes.mgb;
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;
/ * *
* Code to produce MBG
*
* /
public class MyMesGenerator {
public static void main(String[] args) throws Exception {
// Warning message during MBG execution
List<String> warnings = new ArrayList<String>();
// If the generated code is duplicated, override the original code
boolean overwrite = true;
// Read our MBG configuration file
InputStream is = MyMesGenerator.class.getResourceAsStream("/mymesgeneratorconfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
is.close();
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
/ / create the MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
// Execute the generated code
myBatisGenerator.generate(null);
// Displays a warning message
for (String warning : warnings) {
System.out.println(warning);
}
System.out.println("end");
}
}
Copy the code
Add MyMesCommentGenerator custom comment generator
package com.cn.mymes.mgb;
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 org.mybatis.generator.internal.util.StringUtility;
import java.util.Properties;
/ * *
* Custom comment generator
* /
public class MyMesCommentGenerator extends DefaultCommentGenerator {
private boolean addRemarkComments = false;
/ * *
* Set user - configured parameters
* /
@Override
public void addConfigurationProperties(Properties properties) {
super.addConfigurationProperties(properties);
this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments"));
}
/ * *
* Add comments to fields
* /
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
String remarks = introspectedColumn.getRemarks();
// Determine whether to add remarks based on the parameters and remarks
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
addFieldJavaDoc(field, remarks);
}
}
/ * *
* Add comments to the fields of model
* /
private void addFieldJavaDoc(Field field, String remarks) {
// Document comment begins
field.addJavaDocLine("/ * *");
// Get the remarks of database fields
String[] remarkLines = remarks.split(System.getProperty("line.separator"));
for (String remarkLine : remarkLines) {
field.addJavaDocLine("*" + remarkLine);
}
addJavadocTag(field, false);
field.addJavaDocLine(" */");
}
}
Copy the code
Add the Java configuration for MyBatis
Used to configure the path of the Mapper interface to be dynamically generated
package com.cn.mymes.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
/ * *
* MyMesBatisConfig
*
* /
@Configuration
@MapperScan("com.cn.mymes.mgb.mapper")
public class MyMesBatisConfig {
}
Copy the code
Before running MyMesGenerator
After running MyMesGenerator
Tomorrow, MyMes will integrate Swagger-UI to implement online API documentation