package com.xxf.demo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.Scanner;
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
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");
gc.setOutputDir("D:\\FirstSpringBoot\\first\\src\\main\\java");//最好用java文件夹的绝对路径
gc.setAuthor("xxf");
gc.setOpen(false);
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setIdType(IdType.ID_WORKER_STR); //主键策略
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
gc.setSwagger2(true);//!!!!开启Swagger2模式,需要依赖,开启后,表里的comment注释,会进行对应生成
mpg.setGlobalConfig(gc);
// !!!!!数据源配置,记得改成自己数据库对应的
DataSourceConfig dsc = new DataSourceConfig();
//gu是数据库名字
dsc.setUrl("jdbc:mysql://localhost:3306/gu?serverTimezone=GMT%2B8&useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名,eg:输入demo,结构就会加上下面的com.xxf,所以就是com.xxf.demo"));
pc.setParent("com.xxf"); //!!!!可以改
//可以不进行设置,默认的,mapper设置后,如果名字不是mapper就会多一个,不知道为什么会多一个
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略,驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略,驼峰命名
strategy.setEntityLombokModel(true);// lombok 自动加上
strategy.setRestControllerStyle(true);//开启RestControoler注解
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true); //用于@RequestMapping("/demo_user/oneUser")生成的路径
strategy.setTablePrefix(pc.getModuleName() + "_");//生成实体时去掉表前缀,eg:表_one就会变成one
mpg.setStrategy(strategy);
mpg.execute();
}
}
package com.xxf.demo.config;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class MyConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(PathSelectors.any())
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-课程中心API文档")
.description("文档描述")
.version("1.0")
.contact(new Contact("java", "网站", "邮箱"))
.build();
}
}
访问swagger
http://localhost:8080/doc.html