需要在pom.xml引入以下文件
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<!--mybatis-plus组件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
<scope>compile</scope>
</dependency>
<!-- mybatis-plus生成代码组件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>mybatis-plus-extension</artifactId>
<groupId>com.baomidou</groupId>
</exclusion>
</exclusions>
</dependency>
<!--Velocity引擎模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
mybatis-plus代码生成类
package com.example.generator;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.example.webcore.framework.model.BaseModel;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* mybatis-plus代码生成类
*/
public class CodeGenerator {
/**
* 包含的表集合
*/
static String[] INCLUDE_TABLE_NAMES = {
"TB_TEMPLATE"
};
/**
* 要去除的表名前缀
*/
static String TABLE_PREFIX = "T";
/**
* 父包名。如果为空,下面子包名必须写全部, 否则就只需写子包名
*/
static String PARENT = "com.example.demo";
/**
* 开启 swagger2 模式
*/
static boolean USED_SWAGGER2 = true;
public static void main(String[] args) {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
mpg.setGlobalConfig(getGlobalConfig());
// 3、数据源配置
mpg.setDataSource(getDataSourceConfig());
// 4、包配置
mpg.setPackageInfo(getPackageConfig());
// 5、策略配置
mpg.setStrategy(getStrategyConfig());
// 6、执行
mpg.execute();
}
/**
* 生成包路径设置
* @return
*/
public static PackageConfig getPackageConfig(){
// 4、包配置
PackageConfig pc = new PackageConfig();
//子类包路径
pc.setParent(PARENT);
pc.setModuleName("sys"); //模块名
pc.setController("controller");
pc.setService("service");
pc.setMapper("mapper");
pc.setEntity("model.entity");
return pc;
}
/**
* 策略配置
* @return
*/
public static StrategyConfig getStrategyConfig(){
StrategyConfig strategy = new StrategyConfig();
//包含生成的表
strategy.setInclude(INCLUDE_TABLE_NAMES);
//数据库表映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//数据库表字段映射到实体的命名策略,下划线转驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 【实体】是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
//restful api风格控制器
strategy.setRestControllerStyle(true);
//自定义继承的Controller类全称
// strategy.setSuperControllerClass()
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
//字段填充字段
strategy.setTableFillList(getTableFills());
//去掉的表名前缀
strategy.setTablePrefix(TABLE_PREFIX);
//自定义实体父类
strategy.setSuperEntityClass(BaseModel.class);
// Boolean类型字段是否移除is前缀处理
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
// 是否生成实体时,生成字段注解
strategy.setEntityTableFieldAnnotationEnable(true);
return strategy;
}
/**
* 获取TableFill策略
*/
private static List<TableFill> getTableFills() {
// 自定义需要填充的字段
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("CREATED_AT", FieldFill.INSERT));
tableFillList.add(new TableFill("UPDATED_AT", FieldFill.INSERT_UPDATE));
return tableFillList;
}
/**
* 设置链接的数据库信息
* @return
*/
public static DataSourceConfig getDataSourceConfig(){
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDriverName("oracle.jdbc.OracleDriver");
dsc.setUrl("jdbc:oracle:thin:@//10.99.74.110:1521/zkjhr");
dsc.setUsername("msjtdbdev");
dsc.setPassword("zc_sczh20");
dsc.setDbType(DbType.ORACLE);
return dsc;
}
/**
* 数据全局配置
* @return
*/
public static GlobalConfig getGlobalConfig(){
// 全局配置
GlobalConfig gc = new GlobalConfig();
//设置输出路径
gc.setOutputDir(getJavaPath());
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setAuthor("system");
gc.setOpen(false);
gc.setFileOverride(true);
gc.setSwagger2(USED_SWAGGER2);
//开启 通用查询结果列
gc.setBaseColumnList(true);
//开启默认返回
gc.setBaseResultMap(true);
return gc;
}
/**
* 获取JAVA目录
*/
private static String getJavaPath() {
String javaPath = new CodeGenerator().getRootPath()+ "/src/main/java";
System.err.println(" Generator Java Path:【 " + javaPath + " 】");
return javaPath;
}
/**
* 获取根目录
*/
private String getRootPath() {
String file = Objects.requireNonNull(this.getClass().getClassLoader().getResource(""))
.getFile();
return new File(file).getParentFile().getParent();
}
}
实体基类
package com.example.webcore.framework.model;
import com.example.webcore.framework.converter.Convert;
/**
* 实体基类
*/
public class BaseModel extends Convert {
protected Long id;
public Long getId() {
return this.id;
}
public void setId(final Long id) {
this.id = id;
}
public String toString() {
return "BaseModel(id=" + this.getId() + ")";
}
public BaseModel() {
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof BaseModel)) {
return false;
} else {
BaseModel other = (BaseModel)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof BaseModel;
}
public int hashCode() {
int result = 1;
Object $id = this.getId();
result = result * 59 + ($id == null ? 43 : $id.hashCode());
return result;
}
}
convert类,可实现类之间的转换
package com.example.webcore.framework.converter;
import com.example.webcore.framework.converter.BeanConverter;
import java.io.Serializable;
public class Convert implements Serializable {
public Convert() {
}
public <T> T convert(Class<T> clazz) {
return BeanConverter.convert(clazz, this);
}
}
BeanConverter,利用modemapper实现类和类之间的转换,使用ModelMapper需要导入依赖
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.4</version>
</dependency>
package com.example.webcore.framework.converter;
import cn.hutool.core.collection.CollUtil;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.objenesis.instantiator.util.ClassUtils;
import java.util.*;
import java.util.stream.Collectors;
public class BeanConverter {
private static final ModelMapper MODEL_MAPPER;
static {
MODEL_MAPPER = new ModelMapper();
// Jsr310ModuleConfig config = Jsr310ModuleConfig.builder()
// .dateTimePattern("yyyy-MM-dd HH:mm:ss.SSS")
// .datePattern("yyyy-MM-dd")
// .zoneId(ZoneOffset.UTC)
// .build();
// MODEL_MAPPER.registerModule(new Jsr310Module(config)).registerModule(new Jdk8Module());
MODEL_MAPPER.getConfiguration().setFullTypeMatchingRequired(true);
MODEL_MAPPER.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
public BeanConverter() {
}
public static <T> Map<String, Object> beanToMap(T bean) {
Map<String, Object> map = Collections.emptyMap();
if (null != bean) {
BeanMap beanMap = BeanMap.create(bean);
map = new HashMap(beanMap.keySet().size());
Iterator var3 = beanMap.keySet().iterator();
while(var3.hasNext()) {
Object key = var3.next();
((Map)map).put(String.valueOf(key), beanMap.get(key));
}
}
return (Map)map;
}
public static <T> List<Map<String, Object>> beansToMap(List<T> objList) {
List<Map<String, Object>> list = Collections.emptyList();
if (CollUtil.isNotEmpty(objList)) {
list = new ArrayList(objList.size());
Iterator var4 = objList.iterator();
while(var4.hasNext()) {
T anObjList = (T) var4.next();
Map<String, Object> map = beanToMap(anObjList);
((List)list).add(map);
}
}
return (List)list;
}
public static <T> List<T> mapToBean(List<Map<String, Object>> mapList, Class<T> beanClass) {
List<T> list = Collections.emptyList();
if (CollUtil.isNotEmpty(mapList)) {
list = new ArrayList(mapList.size());
Iterator var5 = mapList.iterator();
while(var5.hasNext()) {
Map<String, Object> map1 = (Map)var5.next();
T bean = mapToBean(map1, beanClass);
((List)list).add(bean);
}
}
return (List)list;
}
public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) {
T entity = ClassUtils.newInstance(beanClass);
BeanMap beanMap = BeanMap.create(entity);
beanMap.putAll(map);
return entity;
}
public static <T> List<T> convert(Class<T> clazz, List<?> list) {
return CollUtil.isEmpty(list) ? Collections.emptyList() : (List)list.stream().map((e) -> {
return convert(clazz, e);
}).collect(Collectors.toList());
}
public static <T> T convert(Class<T> targetClass, Object source) {
return getModelMapper().map(source, targetClass);
}
public static ModelMapper getModelMapper() {
return MODEL_MAPPER;
}
}
以上配置完成后,就可以执行生成mapper,service,controller文件