mybatis-plus使用
一、项目结构
二、pom.xml
4.0.0
com.ykq
mybatis-plus-demo
0.0.1-SNAPSHOT
mybatis-plus-demo
Demo project for Spring Boot
1.8
UTF-8
UTF-8
2.3.7.RELEASE
com.baomidou
mybatis-plus-boot-starter
3.4.2
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.baomidou
mybatis-plus-generator
3.3.2
org.apache.velocity
velocity-engine-core
2.2
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
2.3.7.RELEASE
com.ykq.mybatisplusdemo.MybatisPlusDemoApplication
repackage
repackage
三、application.properties
# 应用名称
spring.application.name=mybatis-plus-demo
server.port=8000
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath*:com/ykq/mybatisplusdemo
public class CodeGenerator {
public static void main(String[] args) {
String parent = "com.ykq.mybatisplusdemo";
String parentPath = parent.replace(".", "/");
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// (1)全局配置
GlobalConfig gc = new GlobalConfig();
// todo 在这里写好项目路径
String projectPath = "D:\sts_workspacce\mybatis-plus-demo";
gc.setOutputDir(projectPath + "/src/main/java");
String author = "ykq";
gc.setAuthor(author);
gc.setBaseResultMap(true);// 设置 ResultMap
gc.setBaseColumnList(true);// 设置 columList
gc.setOpen(false);
// mapper命名格式
gc.setMapperName("%sMapper");
// xml命名格式
gc.setXmlName("%sMapper");
// service命名格式
gc.setServiceName("%sService");
// serviceImpl命名格式
gc.setServiceImplName("%sServiceImpl");
// controller命名格式
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// (2)数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
// todo 在这里写好db配置
dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus_demo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// (3)包配置
PackageConfig pc = new PackageConfig();
// todo 可以这里写好新增模块名
pc.setModuleName("tuser");
pc.setParent(parent);
// Entity包名
pc.setEntity("entity");
// Service包名
pc.setService("service");
// Service Impl包名
pc.setServiceImpl("service.impl");
// mapper包名
pc.setMapper("mapper");
// mapper xml包名
pc.setXml("mapper.xml");
// controller包名
pc.setController("controller");
mpg.setPackageInfo(pc);
// (4) 自定义配置
TemplateConfig tc = new TemplateConfig();
tc.setXml(null);
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
InjectionConfig cfg =new InjectionConfig(){
@Override
public void initMap(){
// to do nothing
}
};
List focList =new ArrayList<>();
// ⾃定义配置会被优先输出
focList.add(new FileOutConfig(templatePath){
@Override
public String outputFile(TableInfo tableInfo){
return projectPath + "/src/main/resources/"+ parentPath + "/" + pc.getModuleName() + "/mapper/" + tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(tc);
// (5)策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
// todo 设置对应表
strategy.setInclude("t_user");
// mapping中驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
// 带上@TableName以及@TableId
strategy.setEntityTableFieldAnnotationEnable(true);
// 表前缀
strategy.setTablePrefix(new String[] {});
mpg.setStrategy(strategy);
// (6)执行策略
mpg.execute();
}
}