以下案例为实际开发中运用
环境:idea+mysql5.7+maven3
1.maven配置mybatis和mysql驱动
<dependencies>
<!--mybatis generator生成实体-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>${mybatis.generator.core.version}</version>
</dependency>
<!--end :mybatis generator生成实体-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<version>${mybatis-plus-annotation.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
在项目resources下面创建如下文件:
generator.properties 数据库信息和实体生成路径
#mysql连接驱动
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.202.181:3306/mdsp?autoReconnect=true&characterEncoding=utf8&useUnicode=true&generateSimpleParameterMetadata=true
jdbc.username=root
jdbc.password=123456
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
#mybatis生成实体,xml,接口路径
package.model=com.suning.mdsp.persistence.domain.entity.solution
package.mapper.java=com.suning.mdsp.persistence.mapper.solution
package.mapper.xml=sqlMap.solution
#mybatis生成文件存储路径
target.project=src/main/java
target.resources=src/main/resources
generatorConfig.xml mybatis生成文件配置
<?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="generator.properties"/>
<context id="mysql" targetRuntime="MyBatis3">
<!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,
比如ORACLE就是双引号,MYSQL默认是`反引号; -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 指定生成的java文件的编码,没有直接生成到项目时中文可能会乱码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 使用自带序列化插件 -->
<!--<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>-->
<!-- 使用自定义的lombok插件(idea必须先安装lombok插件否则该配置无效) -->
<plugin type="LombokPlugin"/>
<!--使用自定义生成实体注解-->
<commentGenerator type="DefaultCommentGenerator">
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!--数据库连接信息-->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!--
使用自定义类型转换:
将数据库类型tinyint(1)smallint(4)decimal(10,4)转换为Integer,Integer,BigDecimal
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<javaTypeResolver type="JavaTypeResolverDefaultImpl">
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--实体生成路径和Package路径-->
<javaModelGenerator targetPackage="${package.model}"
targetProject="${target.project}">
<!--是否让schema作为包的后缀,默认false-->
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
<!--备注:所有实体类继承BasePojo类(value值替换为自己实体类路径)-->
<!--<property name="rootClass" value="com.suning.entity.BaseTableEntity"/>-->
</javaModelGenerator>
<!--实体对应的mapper.xml文件 -->
<!-- <sqlMapGenerator targetPackage="${package.mapper.xml}" targetProject="${target.resources}">
<!– 是否让schema作为包的后缀,默认false –>
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>-->
<!-- 对应的Mapper接口类文件 -->
<!-- <javaClientGenerator type="XMLMAPPER" targetPackage="${package.mapper.java}" targetProject="${target.project}">
<!– 是否让schema作为包的后缀,默认false –>
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>-->
<!--
列出要生成代码的所有表,这里配置的是不生成Example文件
tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名
可以生成很多表,生成在本工程结构里面
<property name="useActualColumnNames" value="false" />
如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,
比如BRAND_CODE,生成的属性名字就是BRAND_CODE,而不会是brandCode
再比如:数据库里的原本列名是:`is_show_name`(都是小写),
如果true就会生成:is_show_name(还是数据库实际的字段名字,原始名字);生成的setter/getter为:getIs_show_name()和setIs_show_name()(只是第一个字母大写)
如果false就会生成:isShowName(去掉了下划线,变成了驼峰命名方法);生成的setter/getter为:getIsShowName() 和setIsShowName() (只是第一个字母大写)
注意:如果明确的使用columnOverride元素指定了字段对应的实体的属性名,那么useActualColumnNames会被忽略。
-->
<table schema="general" tableName="t_mip_insight" domainObjectName="TMipInsightEntity"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false" />
</table>
<table schema="general" tableName="t_mip_insight_age" domainObjectName="TMipInsightAgeEntity"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false" />
</table>
</context>
</generatorConfiguration>
自定义注解类:DefaultCommentGenerator
/**
* Copyright 2006-2018 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.Set;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
/**
* @author Jeff Butler
*/
public class DefaultCommentGenerator implements CommentGenerator {
private Properties properties;
private boolean suppressDate;
private boolean suppressAllComments;
/** If suppressAllComments is true, this option is ignored. */
private boolean addRemarkComments;
private SimpleDateFormat dateFormat;
public DefaultCommentGenerator() {
super();
properties = new Properties();
suppressDate = false;
suppressAllComments = false;
addRemarkComments = false;
}
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
// add no file level comments by default
}
/**
* Adds a suitable comment to warn users that the element was generated, and when it was generated.
*
* @param xmlElement
* the xml element
*/
@Override
public void addComment(XmlElement xmlElement) {
if (suppressAllComments) {
return;
}
xmlElement.addElement(new TextElement("<!--")); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
sb.append(" WARNING - "); //$NON-NLS-1$
//sb.append(MergeConstants.NEW_ELEMENT_TAG);
xmlElement.addElement(new TextElement(sb.toString()));
xmlElement
.addElement(new TextElement(
" This element is automatically generated by MyBatis Generator, do not modify.")); //$NON-NLS-1$
//
//String s = getDateString();
//if (s != null) {
// sb.setLength(0);
// sb.append(" This element was generated on "); //$NON-NLS-1$
// sb.append(s);
// sb.append('.');
// xmlElement.addElement(new TextElement(sb.toString()));
//}
xmlElement.addElement(new TextElement("-->")); //$NON-NLS-1$
}
@Override
public void addRootComment(XmlElement rootElement) {
// add no document level comments by default
}
@Override
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
addRemarkComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
if (StringUtility.stringHasValue(dateFormatString)) {
dateFormat = new SimpleDateFormat(dateFormatString);
}
}
/**
* This method adds the custom javadoc tag for. You may do nothing if you do not wish to include the Javadoc tag -
* however, if you do not include the Javadoc tag then the Java merge capability of the eclipse plugin will break.
*
* @param javaElement
* the java element
* @param markAsDoNotDelete
* the mark as do not delete
*/
protected void addJavadocTag(JavaElement javaElement,
boolean markAsDoNotDelete) {
javaElement.addJavaDocLine(" *"); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
sb.append(" * "); //$NON-NLS-1$
//sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge"); //$NON-NLS-1$
}
javaElement.addJavaDocLine(sb.toString());
}
/**
* Returns a formated date string to include in the Javadoc tag
* and XML comments. You may return null if you do not want the date in
* these documentation elements.
*
* @return a string representing the current timestamp, or null
*/
@Override
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
innerClass.addJavaDocLine(sb.toString());
addJavadocTag(innerClass, false);
innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
addJavadocTag(innerClass, markAsDoNotDelete);
innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
if (suppressAllComments || !addRemarkComments) {
return;
}
topLevelClass.addJavaDocLine("/**"); //$NON-NLS-1$
String remarks = introspectedTable.getRemarks();
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$
for (String remarkLine : remarkLines) {
topLevelClass.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$
}
}
topLevelClass.addJavaDocLine(" *"); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
sb.append(" * 表名:");
sb.append(introspectedTable.getFullyQualifiedTable());
topLevelClass.addJavaDocLine(sb.toString());
topLevelClass.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addEnumComment(InnerEnum innerEnum,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString());
addJavadocTag(innerEnum, false);
innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addFieldComment(Field field,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
field.addJavaDocLine("/**"); //$NON-NLS-1$
String remarks = introspectedColumn.getRemarks();
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$
for (String remarkLine : remarkLines) {
field.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$
}
}
//TODO 去除表字段注释
field.addJavaDocLine(" *"); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
sb.append(" * " + "表字段:");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
field.addJavaDocLine(sb.toString());
// addJavadocTag(field, false);
field.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
field.addJavaDocLine(sb.toString());
addJavadocTag(field, false);
field.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addGeneralMethodComment(Method method,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
method.addJavaDocLine(sb.toString());
addJavadocTag(method, false);
method.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addGetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *"); //$NON-NLS-1$
sb.setLength(0);
sb.append(" * @return the value of "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
addJavadocTag(method, false);
method.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addSetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *"); //$NON-NLS-1$
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param "); //$NON-NLS-1$
sb.append(parm.getName());
sb.append(" the value for "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
addJavadocTag(method, false);
method.addJavaDocLine(" */"); //$NON-NLS-1$
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
method.addAnnotation(getGeneratedAnnotation(comment));
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source field: " //$NON-NLS-1$
+ introspectedTable.getFullyQualifiedTable().toString()
+ "." //$NON-NLS-1$
+ introspectedColumn.getActualColumnName();
method.addAnnotation(getGeneratedAnnotation(comment));
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
field.addAnnotation(getGeneratedAnnotation(comment));
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source field: " //$NON-NLS-1$
+ introspectedTable.getFullyQualifiedTable().toString()
+ "." //$NON-NLS-1$
+ introspectedColumn.getActualColumnName();
field.addAnnotation(getGeneratedAnnotation(comment));
if (!suppressAllComments && addRemarkComments) {
String remarks = introspectedColumn.getRemarks();
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
field.addJavaDocLine("/**"); //$NON-NLS-1$
String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$
for (String remarkLine : remarkLines) {
field.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$
}
field.addJavaDocLine(" */"); //$NON-NLS-1$
}
}
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
Set<FullyQualifiedJavaType> imports) {
imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$
String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
innerClass.addAnnotation(getGeneratedAnnotation(comment));
}
private String getGeneratedAnnotation(String comment) {
StringBuilder buffer = new StringBuilder();
buffer.append("@Generated("); //$NON-NLS-1$
if (suppressAllComments) {
buffer.append('\"');
} else {
buffer.append("value=\""); //$NON-NLS-1$
}
buffer.append(MyBatisGenerator.class.getName());
buffer.append('\"');
if (!suppressDate && !suppressAllComments) {
buffer.append(", date=\""); //$NON-NLS-1$
buffer.append(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
buffer.append('\"');
}
if (!suppressAllComments) {
buffer.append(", comments=\""); //$NON-NLS-1$
buffer.append(comment);
buffer.append('\"');
}
buffer.append(')');
return buffer.toString();
}
}
自定义数据类型:JavaTypeResolverDefaultImpl:
/**
* Copyright 2006-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.JavaTypeResolver;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;
import java.math.BigDecimal;
import java.sql.Types;
import java.util.*;
/**
*
* @author Jeff Butler
*/
public class JavaTypeResolverDefaultImpl implements JavaTypeResolver {
protected List<String> warnings;
protected Properties properties;
protected Context context;
protected boolean forceBigDecimals;
protected boolean useJSR310Types;
protected Map<Integer, JdbcTypeInformation> typeMap;
// TODO - remove when we get to JDK 8
private static final int TIME_WITH_TIMEZONE = 2013;
private static final int TIMESTAMP_WITH_TIMEZONE = 2014;
public JavaTypeResolverDefaultImpl() {
super();
properties = new Properties();
typeMap = new HashMap<Integer, JdbcTypeInformation>();
typeMap.put(Types.ARRAY, new JdbcTypeInformation("ARRAY", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.BIGINT, new JdbcTypeInformation("BIGINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Long.class.getName())));
typeMap.put(Types.BINARY, new JdbcTypeInformation("BINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.BIT, new JdbcTypeInformation("BIT", //$NON-NLS-1$
new FullyQualifiedJavaType(Boolean.class.getName())));
typeMap.put(Types.BLOB, new JdbcTypeInformation("BLOB", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.BOOLEAN, new JdbcTypeInformation("BOOLEAN", //$NON-NLS-1$
new FullyQualifiedJavaType(Boolean.class.getName())));
typeMap.put(Types.CHAR, new JdbcTypeInformation("CHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.CLOB, new JdbcTypeInformation("CLOB", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.DATALINK, new JdbcTypeInformation("DATALINK", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.DATE, new JdbcTypeInformation("DATE", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.DECIMAL, new JdbcTypeInformation("DECIMAL", //$NON-NLS-1$
new FullyQualifiedJavaType(BigDecimal.class.getName())));
typeMap.put(Types.DISTINCT, new JdbcTypeInformation("DISTINCT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.DOUBLE, new JdbcTypeInformation("DOUBLE", //$NON-NLS-1$
new FullyQualifiedJavaType(Double.class.getName())));
typeMap.put(Types.FLOAT, new JdbcTypeInformation("FLOAT", //$NON-NLS-1$
new FullyQualifiedJavaType(Double.class.getName())));
typeMap.put(Types.INTEGER, new JdbcTypeInformation("INTEGER", //$NON-NLS-1$
new FullyQualifiedJavaType(Integer.class.getName())));
typeMap.put(Types.JAVA_OBJECT, new JdbcTypeInformation("JAVA_OBJECT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.LONGNVARCHAR, new JdbcTypeInformation("LONGNVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.LONGVARBINARY, new JdbcTypeInformation(
"LONGVARBINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.LONGVARCHAR, new JdbcTypeInformation("LONGVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NCHAR, new JdbcTypeInformation("NCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NCLOB, new JdbcTypeInformation("NCLOB", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NVARCHAR, new JdbcTypeInformation("NVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NULL, new JdbcTypeInformation("NULL", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.NUMERIC, new JdbcTypeInformation("NUMERIC", //$NON-NLS-1$
new FullyQualifiedJavaType(BigDecimal.class.getName())));
typeMap.put(Types.OTHER, new JdbcTypeInformation("OTHER", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.REAL, new JdbcTypeInformation("REAL", //$NON-NLS-1$
new FullyQualifiedJavaType(Float.class.getName())));
typeMap.put(Types.REF, new JdbcTypeInformation("REF", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.SMALLINT, new JdbcTypeInformation("SMALLINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Integer.class.getName())));
typeMap.put(Types.STRUCT, new JdbcTypeInformation("STRUCT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.TIME, new JdbcTypeInformation("TIME", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.TIMESTAMP, new JdbcTypeInformation("TIMESTAMP", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.TINYINT, new JdbcTypeInformation("TINYINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Integer.class.getName())));
typeMap.put(Types.VARBINARY, new JdbcTypeInformation("VARBINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.VARCHAR, new JdbcTypeInformation("VARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
// JDK 1.8 types
typeMap.put(TIME_WITH_TIMEZONE, new JdbcTypeInformation("TIME_WITH_TIMEZONE", //$NON-NLS-1$
new FullyQualifiedJavaType("java.time.OffsetTime"))); //$NON-NLS-1$
typeMap.put(TIMESTAMP_WITH_TIMEZONE, new JdbcTypeInformation("TIMESTAMP_WITH_TIMEZONE", //$NON-NLS-1$
new FullyQualifiedJavaType("java.time.OffsetDateTime"))); //$NON-NLS-1$
}
@Override
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
forceBigDecimals = StringUtility
.isTrue(properties
.getProperty(PropertyRegistry.TYPE_RESOLVER_FORCE_BIG_DECIMALS));
useJSR310Types = StringUtility
.isTrue(properties
.getProperty(PropertyRegistry.TYPE_RESOLVER_USE_JSR310_TYPES));
}
@Override
public FullyQualifiedJavaType calculateJavaType(
IntrospectedColumn introspectedColumn) {
FullyQualifiedJavaType answer = null;
JdbcTypeInformation jdbcTypeInformation = typeMap
.get(introspectedColumn.getJdbcType());
if (jdbcTypeInformation != null) {
answer = jdbcTypeInformation.getFullyQualifiedJavaType();
answer = overrideDefaultType(introspectedColumn, answer);
}
return answer;
}
protected FullyQualifiedJavaType overrideDefaultType(IntrospectedColumn column, FullyQualifiedJavaType defaultType) {
FullyQualifiedJavaType answer = defaultType;
switch (column.getJdbcType()) {
case Types.BIT:
answer = calculateBitReplacement(column, defaultType);
break;
case Types.DATE:
answer = calculateDateType(column, defaultType);
break;
case Types.DECIMAL:
case Types.NUMERIC:
answer = calculateBigDecimalReplacement(column, defaultType);
break;
case Types.TIME:
answer = calculateTimeType(column, defaultType);
break;
case Types.TIMESTAMP:
answer = calculateTimestampType(column, defaultType);
break;
default:
break;
}
return answer;
}
protected FullyQualifiedJavaType calculateDateType(IntrospectedColumn column, FullyQualifiedJavaType defaultType) {
FullyQualifiedJavaType answer;
if (useJSR310Types) {
answer = new FullyQualifiedJavaType("java.time.LocalDate"); //$NON-NLS-1$
} else {
answer = defaultType;
}
return answer;
}
protected FullyQualifiedJavaType calculateTimeType(IntrospectedColumn column, FullyQualifiedJavaType defaultType) {
FullyQualifiedJavaType answer;
if (useJSR310Types) {
answer = new FullyQualifiedJavaType("java.time.LocalTime"); //$NON-NLS-1$
} else {
answer = defaultType;
}
return answer;
}
protected FullyQualifiedJavaType calculateTimestampType(IntrospectedColumn column, FullyQualifiedJavaType defaultType) {
FullyQualifiedJavaType answer;
if (useJSR310Types) {
answer = new FullyQualifiedJavaType("java.time.LocalDateTime"); //$NON-NLS-1$
} else {
answer = defaultType;
}
return answer;
}
protected FullyQualifiedJavaType calculateBitReplacement(IntrospectedColumn column, FullyQualifiedJavaType defaultType) {
FullyQualifiedJavaType answer;
if (column.getLength() > 1) {
answer = new FullyQualifiedJavaType("byte[]"); //$NON-NLS-1$
} else {
answer = defaultType;
}
return answer;
}
protected FullyQualifiedJavaType calculateBigDecimalReplacement(IntrospectedColumn column, FullyQualifiedJavaType defaultType) {
FullyQualifiedJavaType answer;
if (column.getScale() > 0 || column.getLength() > 18 || forceBigDecimals) {
answer = defaultType;
} else if (column.getLength() > 9) {
answer = new FullyQualifiedJavaType(Long.class.getName());
} else if (column.getLength() > 4) {
answer = new FullyQualifiedJavaType(Integer.class.getName());
} else {
answer = new FullyQualifiedJavaType(Short.class.getName());
}
return answer;
}
@Override
public String calculateJdbcTypeName(IntrospectedColumn introspectedColumn) {
String answer = null;
JdbcTypeInformation jdbcTypeInformation = typeMap
.get(introspectedColumn.getJdbcType());
if (jdbcTypeInformation != null) {
answer = jdbcTypeInformation.getJdbcTypeName();
}
return answer;
}
@Override
public void setWarnings(List<String> warnings) {
this.warnings = warnings;
}
@Override
public void setContext(Context context) {
this.context = context;
}
public static class JdbcTypeInformation {
private String jdbcTypeName;
private FullyQualifiedJavaType fullyQualifiedJavaType;
public JdbcTypeInformation(String jdbcTypeName,
FullyQualifiedJavaType fullyQualifiedJavaType) {
this.jdbcTypeName = jdbcTypeName;
this.fullyQualifiedJavaType = fullyQualifiedJavaType;
}
public String getJdbcTypeName() {
return jdbcTypeName;
}
public FullyQualifiedJavaType getFullyQualifiedJavaType() {
return fullyQualifiedJavaType;
}
}
}
自定义LombokPlugin:
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.util.*;
/**
* A MyBatis Generator plugin to use Lombok's annotations.
* For example, use @Data annotation instead of getter ands setter.
*
* @author Paolo Predonzani (http://softwareloop.com/)
*/
public class LombokPlugin extends PluginAdapter {
private final Collection<Annotations> annotations;
/**
* LombokPlugin constructor
*/
public LombokPlugin() {
annotations = new LinkedHashSet<Annotations>(Annotations.values().length);
}
/**
* @param warnings list of warnings
* @return always true
*/
public boolean validate(List<String> warnings) {
return true;
}
/**
* Intercepts base record class generation
*
* @param topLevelClass the generated base record class
* @param introspectedTable The class containing information about the table as
* introspected from the database
* @return always true
*/
@Override
public boolean modelBaseRecordClassGenerated(
TopLevelClass topLevelClass,
IntrospectedTable introspectedTable
) {
addAnnotations(topLevelClass,introspectedTable);
return true;
}
/**
* Intercepts primary key class generation
*
* @param topLevelClass the generated primary key class
* @param introspectedTable The class containing information about the table as
* introspected from the database
* @return always true
*/
@Override
public boolean modelPrimaryKeyClassGenerated(
TopLevelClass topLevelClass,
IntrospectedTable introspectedTable
) {
addAnnotations(topLevelClass,introspectedTable);
return true;
}
/**
* Intercepts "record with blob" class generation
*
* @param topLevelClass the generated record with BLOBs class
* @param introspectedTable The class containing information about the table as
* introspected from the database
* @return always true
*/
@Override
public boolean modelRecordWithBLOBsClassGenerated(
TopLevelClass topLevelClass,
IntrospectedTable introspectedTable
) {
addAnnotations(topLevelClass,introspectedTable);
return true;
}
/**
* Prevents all getters from being generated.
* See SimpleModelGenerator
*
* @param method the getter, or accessor, method generated for the specified
* column
* @param topLevelClass the partially implemented model class
* @param introspectedColumn The class containing information about the column related
* to this field as introspected from the database
* @param introspectedTable The class containing information about the table as
* introspected from the database
* @param modelClassType the type of class that the field is generated for
*/
@Override
public boolean modelGetterMethodGenerated(
Method method,
TopLevelClass topLevelClass,
IntrospectedColumn introspectedColumn,
IntrospectedTable introspectedTable,
ModelClassType modelClassType
) {
return false;
}
/**
* Prevents all setters from being generated
* See SimpleModelGenerator
*
* @param method the setter, or mutator, method generated for the specified
* column
* @param topLevelClass the partially implemented model class
* @param introspectedColumn The class containing information about the column related
* to this field as introspected from the database
* @param introspectedTable The class containing information about the table as
* introspected from the database
* @param modelClassType the type of class that the field is generated for
* @return always false
*/
@Override
public boolean modelSetterMethodGenerated(
Method method,
TopLevelClass topLevelClass,
IntrospectedColumn introspectedColumn,
IntrospectedTable introspectedTable,
ModelClassType modelClassType
) {
return false;
}
/**
* Adds the lombok annotations' imports and annotations to the class
*
* @param topLevelClass the partially implemented model class
*/
private void addAnnotations(TopLevelClass topLevelClass,IntrospectedTable introspectedTable) {
for (Annotations annotation : annotations) {
topLevelClass.addImportedType(annotation.javaType);
topLevelClass.addAnnotation(annotation.asAnnotation(introspectedTable));
}
}
@Override
public void setProperties(Properties properties) {
super.setProperties(properties);
//@Data is default annotation
annotations.add(Annotations.DATA);
annotations.add(Annotations.ACCESSORS);
annotations.add(Annotations.TableName);
for (String annotationName : properties.stringPropertyNames()) {
if (annotationName.contains(".")) {
// Not an annotation name
continue;
}
String value = properties.getProperty(annotationName);
if (!Boolean.parseBoolean(value)) {
// The annotation is disabled, skip it
continue;
}
Annotations annotation = Annotations.getValueOf(annotationName);
if (annotation == null) {
continue;
}
String optionsPrefix = annotationName + ".";
for (String propertyName : properties.stringPropertyNames()) {
if (!propertyName.startsWith(optionsPrefix)) {
// A property not related to this annotation
continue;
}
String propertyValue = properties.getProperty(propertyName);
//annotation.appendOptions(propertyName, propertyValue);
annotations.add(annotation);
annotations.addAll(Annotations.getDependencies(annotation));
}
}
}
@Override
public boolean clientGenerated(
Interface interfaze,
TopLevelClass topLevelClass,
IntrospectedTable introspectedTable
) {
interfaze.addImportedType(new FullyQualifiedJavaType(
"org.apache.ibatis.annotations.Mapper"));
interfaze.addAnnotation("@Mapper");
return true;
}
private enum Annotations {
DATA("data", "@Data", "lombok.Data", ""),
BUILDER("builder", "@Builder", "lombok.Builder", ""),
ALL_ARGS_CONSTRUCTOR("allArgsConstructor", "@AllArgsConstructor", "lombok.AllArgsConstructor", ""),
NO_ARGS_CONSTRUCTOR("noArgsConstructor", "@NoArgsConstructor", "lombok.NoArgsConstructor", ""),
ACCESSORS("accessors", "@Accessors", "lombok.experimental.Accessors", "chain = true"),
TO_STRING("toString", "@ToString", "lombok.ToString", ""),
TableName("TableName","@TableName","com.baomidou.mybatisplus.annotation.TableName","no null");
private final String paramName;
private final String name;
private final FullyQualifiedJavaType javaType;
private final String options;
Annotations(String paramName, String name, String className, String options) {
this.paramName = paramName;
this.name = name;
this.javaType = new FullyQualifiedJavaType(className);
this.options = options;
}
private static Annotations getValueOf(String paramName) {
for (Annotations annotation : Annotations.values())
if (String.CASE_INSENSITIVE_ORDER.compare(paramName, annotation.paramName) == 0)
return annotation;
return null;
}
private static Collection<Annotations> getDependencies(Annotations annotation) {
if (annotation == ALL_ARGS_CONSTRUCTOR)
return Collections.singleton(NO_ARGS_CONSTRUCTOR);
else
return Collections.emptyList();
}
// A trivial quoting.
// Because Lombok annotation options type is almost String or boolean.
private static String quote(String value) {
if (Boolean.TRUE.toString().equals(value) || Boolean.FALSE.toString().equals(value))
// case of boolean, not passed as an array.
return value;
return value.replaceAll("[\\w]+", "\"$0\"");
}
//private void appendOptions(String key, String value) {
// String keyPart = key.substring(key.indexOf(".") + 1);
// String valuePart = value.contains(",") ? String.format("{%s}", value) : value;
// this.options.add(String.format("%s=%s", keyPart, quote(valuePart)));
//}
private String asAnnotation(IntrospectedTable introspectedTable) {
if (options.isEmpty()) {
return name;
}
StringBuilder sb = new StringBuilder();
sb.append(name);
sb.append("(");
//boolean first = true;
//for (String option : options) {
// if (first) {
// first = false;
// } else {
// sb.append(", ");
// }
// sb.append(option);
//}
if (Annotations.TableName.toString().equals(paramName)){
sb.append("\""+introspectedTable.getFullyQualifiedTable()+"\"");
}else{
sb.append(options);
}
sb.append(")");
return sb.toString();
}
}
}
使用main方法生成数据库所有表:
/**
* 功能描述:
* @author 作者 88404677@cnsuning.com
* @version 1.0.0
* @created 2019-01-02 17:39:51
* @date 2019-01-02 17:39:51
*/
public class GenTable {
public static void main(String[] args) {
//待需要生成的数据库表名
String customTables = "solution_jrtt_report_total,solution_gdt_report_total";
String[] split = customTables.split(",");
StringBuilder all = new StringBuilder();
for (int i = 0; i < split.length; i++) {
all.append("<table tableName=\""+ split[i] +"\" domainObjectName=\""+camelCaseName(split[i])+"\" "
+ "enableCountByExample=\"false\"\n"
+ "\t enableUpdateByExample=\"false\"\n"
+ "\t enableDeleteByExample=\"false\" enableSelectByExample=\"false\" "
+ "selectByExampleQueryId=\"false\">\n"
+ "\t<property name=\"useActualColumnNames\" value=\"false\"/>\n"
+ "</table> \n");
}
System.out.println(all);
}
public static String camelCaseName(String underscoreName) {
StringBuilder result = new StringBuilder();
if (underscoreName != null && underscoreName.length() > 0) {
boolean flag = false;
for (int i = 0; i < underscoreName.length(); i++) {
char ch = underscoreName.charAt(i);
if ("_".charAt(0) == ch) {
flag = true;
} else {
if (flag) {
result.append(Character.toUpperCase(ch));
flag = false;
} else if (i == 0) {
result.append(Character.toUpperCase(ch));
} else {
result.append(ch);
}
}
}
}
result.append("Entity");
return result.toString();
}
}
控制台运行结果:
GenMain:将上诉方法得到的table复制到generator.xml文件里,然后运行如下代码,则生成实体,xml,接口
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 java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class GenMain {
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
String fileName="generatorConfig.xml";
String filePath= GenMain.class.getClassLoader().getResource(fileName).getPath();
System.out.println("filePath:"+filePath);
File configFile = new File(filePath);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
//小心,这里是覆盖以前生成的,如果你有修改以前的,注意换路径生
boolean overwrite = true;
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}