
1、pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.6.RELEASE com.example mybatis-plus0.0.1-SNAPSHOT mybatis-plus Demo project for Spring Boot 1.8 2.1.3 3.4.0 3.4.0 1.1.9 8.0.21 1.18.12 2.0.3 2.0.1.Final 2.8.9 2.3.30 com.baomidou mybatis-plus-generator${generator.version} org.freemarker freemarker${freemarker.version} com.baomidou mybatis-plus-boot-starter${mybatis-plus.version} org.mybatis.spring.boot mybatis-spring-boot-starter${mybatis.version} com.fasterxml.jackson.core jackson-annotations${jackson.version} javax.validation validation-api${validation.version} com.github.xiaoymin knife4j-spring-boot-starter${knife4j.version} org.projectlombok lomboktrue ${lombok.version} com.alibaba druid-spring-boot-starter${druid.version} org.springframework.boot spring-boot-starter-webmysql mysql-connector-javaruntime ${mysql.version} org.springframework.boot spring-boot-starter-tomcatprovided org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engineorg.springframework.boot spring-boot-maven-plugin
2、sql脚本
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for gateway_route -- ---------------------------- DROP TABLE IF EXISTS `gateway_route`; CREATE TABLE `gateway_route` ( `id` bigint(0) NOT NULL AUTO_INCREMENT, `service_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `predicates` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `filters` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `order` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0', `create_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `create_date` datetime(0) NULL DEFAULT NULL, `update_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `update_date` datetime(0) NULL DEFAULT NULL, `remarks` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `del_flag` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
3、CodeGenerator自动生成器
package com.example.mybatisplus;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator {
public static String scanner(String tip) {
// 获取控制台输入值
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
// 项目根目录
String projectPath = System.getProperty("user.dir");
// 用于多个模块下生成到精确的目录下(我设置在桌面)
// String projectPath = "C:/Users/xie/Desktop";
// 代码生成目录
gc.setOutputDir(projectPath + "/src/main/java");
// 开发人员
gc.setAuthor("code");
// 是否打开输出目录(默认值:null)
gc.setOpen(false);
//实体属性 Swagger2 注解
gc.setSwagger2(true);
// 是否覆盖已有文件(默认值:false)
gc.setFileOverride(true);
//去掉接口上的I
//gc.setServiceName("%Service");
// 配置时间类型策略(date类型),如果不配置会生成LocalDate类型
gc.setDateType(DateType.ONLY_DATE);
//把全局配置添加到代码生成器主类
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
//数据库连接
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
// 数据库 schema name
//dsc.setSchemaName("public");
// 数据库类型
dsc.setDbType(DbType.MYSQL);
// 驱动名称
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
// 用户名
dsc.setUsername("root");
// 密码
dsc.setPassword("123456");
// 把数据源配置添加到代码生成器主类
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 添加这个后 会以一个实体为一个模块 比如user实体会生成user模块 每个模块下都会生成三层
// pc.setModuleName(scanner("模块名"));
// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.example.mybatisplus");
// Service包名
pc.setService("service");
// Entity包名
pc.setEntity("model");
// ServiceImpl包名
pc.setServiceImpl("service.impl");
// Mapper包名
pc.setMapper("mapper");
// Controller包名
pc.setController("controller");
// Mapper.xml包名
pc.setXml("mapper");
// 把包配置添加到代码生成器主类
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 数据库表配置
StrategyConfig strategy = new StrategyConfig();
// 数据库表映射到实体的命名策略:下划线转驼峰
strategy.setNaming(NamingStrategy.underline_to_camel);
// 数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 实体是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
// 生成 @RestController 控制器
strategy.setRestControllerStyle(true);
// 实体类主键名称设置
strategy.setSuperEntityColumns("id");
// 需要包含的表名,允许正则表达式
// 这里做了输入设置
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
// 需要排除的表名,允许正则表达式
//strategy.setExclude("***");
// 是否生成实体时,生成字段注解 默认false;
strategy.setEntityTableFieldAnnotationEnable(true);
// 驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
// 表前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
// 把数据库配置添加到代码生成器主类
mpg.setStrategy(strategy);
// 在代码生成器主类上配置模板引擎
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 生成
mpg.execute();
}
}
执行以后,在控制台输入表名
4、properties
server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #mybatis-plus mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
5、test
@Autowired
private IGatewayRouteService gatewayRouteService;
@RequestMapping("/get")
public List get(){
List list = gatewayRouteService.list();
return list;
}
6、bug报错
@TableField("`order`")
private String order;
实体类有sql关键字时,修改**@TableField("order")**
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)