---进行业务建表,这边根据个人业务分析,不具体操作
--加入mybatis plus pom依赖
<!-- mybatis-plus 3.0.5-->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version>
</dependency> --模板引擎
<!-- freemarker模板引擎用于自定义模板,生成代码-->
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.28</version>
</dependency><!-- apache模板引擎 -->
<dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.0</version>
</dependency>
--在项目配置yml写入数据源配置以及mapper对应实体entity的映射
# DataSource Config
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driver
#devurl: jdbc:mysql://your database ip and database name?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&useSSL=false&failOverReadOnly=false&tinyInt1isBit=false&serverTimezone=Asia/Shanghaiusername: your database userbamepassword: your database password#打印mybatis plus sql 日志输出
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpldefault-statement-timeout: 120mapper-locations: classpath*:/mapper/**Mapper.xmltype-aliases-package: your project entity package location
--代码生成工具类
/*** @Author: xxxx* @Description: ${description}* @Date: 2020/9/15 11:19*/import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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 com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
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(projectPath + "/src/main/java");gc.setAuthor("xxxx");//是否打开输出目录gc.setOpen(false);//service命名方式gc.setServiceName("%sService");//service impl命名方式gc.setServiceImplName("%sServiceImpl");//自定义文件命名,注意 %s 会自动填充表实体属性!gc.setMapperName("%sMapper");gc.setXmlName("%sMapper");gc.setFileOverride(true);gc.setActiveRecord(true);// XML 二级缓存gc.setEnableCache(false);// XML ResultMapgc.setBaseResultMap(true);// XML columnListgc.setBaseColumnList(false);//gc.setSwagger2(true); //实体属性 Swagger2 注解mpg.setGlobalConfig(gc);// 数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://your database ip /database name?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&useSSL=false&failOverReadOnly=false&tinyInt1isBit=false&serverTimezone=Asia/Shanghai");dsc.setDriverName("com.mysql.cj.jdbc.Driver");dsc.setUsername("/your database username");dsc.setPassword("/your database password");mpg.setDataSource(dsc);// 包配置PackageConfig pc = new PackageConfig();pc.setParent("com.stdl.chargingpile");mpg.setPackageInfo(pc);// 自定义配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}};// 如果模板引擎是 freemarkerString templatePath = "/templates/mapper.xml.ftl";// 如果模板引擎是 velocity// String templatePath = "/templates/mapper.xml.vm";// 自定义输出配置List<FileOutConfig> focList = new ArrayList<>();// 自定义配置会被优先输出focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!return projectPath + "/src/main/resources/mapper/"+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});cfg.setFileOutConfigList(focList);mpg.setCfg(cfg);// 配置模板TemplateConfig templateConfig = new TemplateConfig();// 配置自定义输出模板templateConfig.setXml(null);mpg.setTemplate(templateConfig);// 策略配置StrategyConfig strategy = new StrategyConfig();strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);strategy.setEntityLombokModel(true);strategy.setRestControllerStyle(true);// 公共父类//strategy.setSuperEntityColumns("id");strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));strategy.setControllerMappingHyphenStyle(true);strategy.setTablePrefix(pc.getModuleName() + "_");mpg.setStrategy(strategy);mpg.setTemplateEngine(new FreemarkerTemplateEngine());mpg.execute();}}
--启动类上加上mapper扫描的位置,否则回抛出找不到mapper的异常
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.annotation.EnableRetry;import java.util.Arrays;/*** @author xxx*/
@SpringBootApplication
@MapperScan("your mapper interface package location")
@EnableRetry
public class ChargingPileApplication {public static void main(String[] args) {SpringApplication.run(ChargingPileApplication.class, args);//查看当前运行的线程名称}@Beanpublic CommandLineRunner commandLineRunner(ApplicationContext ctx) {return args -> {String[] beanNames = ctx.getBeanDefinitionNames();Arrays.sort(beanNames);for (String beanName : args) {System.out.println(beanName);}};}
}