MyBatis Generator 只生成带注释的entity
2020-05-23 本文已影响0人
星钻首席小管家
MyBatis Generator 只生成带注释的entity
1.pom.xml
<dependencies>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<!-- mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
2.application.yml
spring:
# 数据库相关配置
datasource:
url: jdbc:mysql://localhost:3306/demo_test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
initialSize: 1
minIdle: 1
maxActive: 5
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 180000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
driver-class-name: com.mysql.cj.jdbc.Driver
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>
<context id="mssqlGenerator" targetRuntime="MyBatis3">
<plugin type="com.mybatis.generator.config.MyBatisPlugin" >
<property name="hasLombok" value="true"/>
</plugin>
<!--关闭注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/demo_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8" userId="root" password="123456" >
<!-- <property name="useInformationSchema" value="true"></property>-->
<!-- 下面指定不生成key和withblobs文件 -->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.mybatis.generator.entity" targetProject="src/main/java" />
<!-- <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" />-->
<!-- <javaClientGenerator targetPackage="com.example.demo.dao.mapper" targetProject="src/main/java" type="XMLMAPPER" />-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!-- <columnOverride column="request_data" jdbcType="VARCHAR"/> <!–解决长文本生成带WithBLOBs问题–>-->
<!-- <columnOverride column="response_data" jdbcType="VARCHAR"/>-->
</table>
</context>
</generatorConfiguration>
3.自定义MyBatisPlugin
package com.mybatis.generator.config;
import org.apache.commons.lang3.time.DateFormatUtils;
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.*;
import org.mybatis.generator.internal.util.StringUtility;
import java.util.Date;
import java.util.List;
/**
* @Description:
* @Auther: sp
* @Date: 2020/5/23 11:46
*/
public class MyBatisPlugin extends PluginAdapter {
@Override
public boolean validate(List<String> list) {
return true;
}
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//添加domain的import
topLevelClass.addImportedType("lombok.Data");
//添加domain的注解
topLevelClass.addAnnotation("@Data");
topLevelClass.addJavaDocLine("/**");
String remarks = introspectedTable.getRemarks();
if (StringUtility.stringHasValue(remarks)) {
String[] remarkLines = remarks.split(System.getProperty("line.separator"));
for (String remarkLine : remarkLines) {
topLevelClass.addJavaDocLine(" * " + remarkLine);
}
}
StringBuilder sb = new StringBuilder();
sb.append(" * ").append(introspectedTable.getFullyQualifiedTable());
topLevelClass.addJavaDocLine(sb.toString());
sb.setLength(0);
sb.append(" * @author ").append(System.getProperties().getProperty("user.name"));
topLevelClass.addJavaDocLine(sb.toString());
sb.setLength(0);
sb.append(" * @date ");
sb.append(getDateString());
topLevelClass.addJavaDocLine(sb.toString());
topLevelClass.addJavaDocLine(" */");
return true;
}
@Override
public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
IntrospectedTable introspectedTable, ModelClassType modelClassType) {
field.addJavaDocLine("/**");
String remarks = introspectedColumn.getRemarks();
if (StringUtility.stringHasValue(remarks)) {
String[] remarkLines = remarks.split(System.getProperty("line.separator"));
for (String remarkLine : remarkLines) {
field.addJavaDocLine(" * " + remarkLine);
}
}
field.addJavaDocLine(" */");
return true;
}
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//添加Mapper的import
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
//添加Mapper的注解
interfaze.addAnnotation("@Mapper");
return true;
}
@Override
public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//不生成getter
return false;
}
@Override
public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//不生成setter
return false;
}
protected String getDateString() {
return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
}
}
4.用java代码去生成(MyBatisGen)
package com.mybatis.generator.config;
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 org.springframework.core.io.ClassPathResource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @Description:
* @Auther: sp
* @Date: 2020/5/23 11:48
*/
public class MyBatisGen {
public static void main(String[] args) throws Exception {
generator();
}
private static void generator() throws Exception {
File configFile = new ClassPathResource("generatorConfig.xml").getFile();
List<String> warnings = new ArrayList<String>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.out.println("代码生成完毕>>>>>>>>>>>>");
}
}
5.最后执行mian方法就可以生成entity了,效果如下
package com.mybatis.generator.entity;
import java.util.Date;
import lombok.Data;
/**
* user
* @author Administrator
* @date 2020-05-23 17:12:42
*/
@Data
public class User {
/**
* id
*/
private Integer id;
/**
* 名字
*/
private String name;
/**
* 用户Email
*/
private String email;
/**
* 角色ID
*/
private Integer roleId;
/**
* 创建时间
*/
private Date createTime;
}