pom依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>commons-lang3</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
</exclusions>
</dependency>
java代码
package com.wy;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class MpGenerator {
@Test
public void testGenerator() {
String projectPath= "D:\\ideaWork\\wy\\server";
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(true)
.setAuthor("ws")
.setOutputDir(projectPath+"\\src\\main\\java")
.setFileOverride(true)
.setIdType(IdType.ID_WORKER)
.setServiceName("I%sService")
.setBaseResultMap(true)
.setBaseColumnList(true).setOpen(false).setSwagger2(true);
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUrl("jdbc:mysql://url:3306/数据库名称?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true")
.setUsername("a_admin")
.setPassword("Initial-1");
StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(true)
.setTablePrefix("")
.setNaming(NamingStrategy.underline_to_camel)
.setInclude("mdm_processinst_edit_log");
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("com.goldwind.mdm")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("model");
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
}
};
String templatePath = "/templates/mapper.xml.vm";
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(config)
.setDataSource(dsConfig)
.setStrategy(stConfig)
.setPackageInfo(pkConfig).setCfg(cfg);
ag.execute();
}
}
idea安装插件
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/58a8705c1855409d9271bb145e2b4876.png)
yml 包扫描,可配合多数据一起配置https://blog.csdn.net/wyazyf/article/details/141420807
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.goldwind.wy.model
global-config:
db-config:
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
db-column-underline: true
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志打印