各种文件的模板位置
https://blog.csdn.net/kanglong129/article/details/98362009
Maven
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!--mybatisplus代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.0</version>
</dependency>
<!-- mybatisplus依赖模块包 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
模板文件:
package com.csvw.sx.ca.open.platform.administration;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.*;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class MyBatisPlusGenerator {
/**
* 项目路径 修改为你项目的实际绝对路径
*
*/
private static String canonicalPath = "D:\\workspace\\mybatis-generator";
/**
* 基本包名
*/
private static String basePackage = "com.csvw.sx.ca.open.platform.administration";
/**
* 作者
*/
private static String authorName = "";
private static String[] tables = {"ss_apisdf"};
/**
* table前缀
*/
private static String[] prefix = {"ss_"};
/**
* 数据库类型
*/
private static DbType dbType = DbType.MYSQL;
/**
* 数据库配置四要素
*/
private static String driverName = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://ry_dev?useUnicode=true&characterEncoding=utf-8&useSSL=false";
private static String username = "s*v";
private static String password = "**";
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
/**
* 获取项目路径
*/
try {
canonicalPath = new File("").getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
// 全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(canonicalPath + "/src/main/java");
globalConfig.setFileOverride(true);
globalConfig.setActiveRecord(true);
globalConfig.setEnableCache(false);
globalConfig.setBaseResultMap(true);
globalConfig.setBaseColumnList(true);
globalConfig.setOpen(true);
globalConfig.setAuthor(authorName);
globalConfig.setMapperName("%sMapper");
globalConfig.setXmlName("%sMapper");
globalConfig.setServiceName("%sService");
globalConfig.setServiceImplName("%sServiceImpl");
globalConfig.setControllerName("%sController");
mpg.setGlobalConfig(globalConfig);
// 数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDriverName(driverName);
dataSourceConfig.setDbType(dbType);
dataSourceConfig.setUrl(url);
dataSourceConfig.setUsername(username);
dataSourceConfig.setPassword(password);
mpg.setDataSource(dataSourceConfig);
// 包配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent(basePackage);
packageConfig.setController("controller");
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
//packageConfig.setServiceImpl("service.impl");
mpg.setPackageInfo(packageConfig);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
/**
* 注入自定义配置
*/
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig abc = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
//自定义文件输出位置(非必须)
List<FileOutConfig> fileOutList = new ArrayList<FileOutConfig>();
fileOutList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return canonicalPath + "/src/main/resources/mappers/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
abc.setFileOutConfigList(fileOutList);
mpg.setCfg(abc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig.setEntity("templates/entity.java.vm");
templateConfig.setService("templates/service.java.vm");
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setTablePrefix(prefix);
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setInclude(tables);
strategyConfig.setEntityBuilderModel(true);
strategyConfig.setRestControllerStyle(true);
mpg.setStrategy(strategyConfig);
mpg.execute();
}
}
Entity.java.vm
package ${package.Entity};
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
import lombok.Data;
@Data
#if(${table.convert})
@TableName("${table.name}")
#end
public class ${entity} extends BaseEntity implements Serializable{
private static final long serialVersionUID = 1L;
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
/**
* ${field.comment}
*/
#end
#if(${field.keyFlag})
## 主键
#if(${field.keyIdentityFlag})
@TableId(value="${field.name}", type= IdType.AUTO)
#elseif(${field.convert})
@TableId("${field.name}")
#end
## 普通字段
#elseif(${field.fill})
## ----- 存在字段填充设置 -----
#if(${field.convert})
@TableField(value = "${field.name}", fill = FieldFill.${field.fill})
#else
@TableField(fill = FieldFill.${field.fill})
#end
#elseif(${field.convert})
@TableField("${field.name}")
#end
## 乐观锁注解
#if(${versionFieldName}==${field.name})
@Version
#end
## 逻辑删除注解
#if(${logicDeleteFieldName}==${field.name})
@TableLogic
#end
private ${field.propertyType} ${field.propertyName};
#end
}
BaseEntity.java
package model;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* Description
* <p>
* Copyright (C) HPE, All rights reserved.
*
* @author Ma, Yonglong
* @date 2019-03-23 16:08
*/
@Data
public class BaseEntity implements Serializable {
/**
* 创建时间
*/
@JSONField(serialize = false)
private LocalDateTime createAt;
private String createBy;
/**
* 修改时间
*/
@JSONField(serialize = false)
private LocalDateTime updateAt;
private String updateBy;
/**
* 逻辑删除标识
*/
@JSONField(serialize = false)
@TableLogic
private Boolean delFlag = false;
}
service.java.vm
package ${package.Service};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@Service
@Slf4j
public class ${table.serviceName} {
@Autowired
private ${table.mapperName} ${table.entityPath}Mapper;
}
mapper.xml.vm
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
<resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位
<id column="${field.name}" property="${field.propertyName}" />
#end
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段
<result column="${field.name}" property="${field.propertyName}" />
#end
#end
</resultMap>
</mapper>