springboot 继承 mybatis-plus 根据数据库(postgresql)中的表反向生成代码

1、数据中先建表

springboot 继承 mybatis-plus 根据数据库(postgresql)中的表反向生成代码_第1张图片

2、新建springboot 项目

观察一下我们需要准备的东西,然后让我们一个一个的准备springboot 继承 mybatis-plus 根据数据库(postgresql)中的表反向生成代码_第2张图片

3、pom.xml中引入相关jar包

注意:引入后 maven 记得 package ,jar包最好不要少

	
        org.postgresql
        postgresql
        runtime
    
	
        com.baomidou
        mybatis-plus-boot-starter
        3.1.2
    
    
        com.baomidou
        mybatis-plus-generator
        3.1.2
    
    
        org.freemarker
        freemarker
        2.3.28
    
    
        org.apache.velocity
        velocity-engine-core
        2.1
    
    
        com.ibeetl
        beetl
        3.0.10.RELEASE
    

springboot 继承 mybatis-plus 根据数据库(postgresql)中的表反向生成代码_第3张图片
注意:不要引入 PageHelper 会冲突

4 、准备 application.xml 配置

数据源与mybatis-plus的配置

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: root
mybatis-plus:
   type-aliases-package: com.mine.manager.plan.mapper
   mapper-locations: classpath*:/mapper/**/*.xml

5、将mybatis-plus的配置文件 (mybatis-plus.properties)放置到resources下

数据库地址与yml配置的数据源地址一致,
mybatis-plus.properties 具体内容:

#此处为本项目src所在路径(代码生成器输出路径)
OutputDir=C:/Users/11147/Desktop/parent/plan/src/main/java
#mapper.xml的生成位置
OutputDirXml=C:/Users/11147/Desktop/parent/plan/src/main/resources
#设置作者
author=***
#自定义包路径
parent=com.mine.manager.plan
#数据库地址与yml配置的数据源地址一致
url=jdbc:postgresql://localhost:5432/postgres
userName=postgres
password=root
driver=org.postgresql.Driver

6、编写反向生成的类 CodeGenerator

注意:数据库类型与表名的修改,多个表名用逗号隔开
CodeGenerator 内容如下:

public class CodeGenerator {

    public static void main(String[] args) throws InterruptedException {

        //用来获取Mybatis-Plus.properties文件的配置信息
        final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus");

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(rb.getString("OutputDir"));
        gc.setOpen(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setAuthor(rb.getString("author"));
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.POSTGRE_SQL);
        dsc.setUrl(rb.getString("url"));
        dsc.setDriverName(rb.getString("driver"));
        dsc.setUsername(rb.getString("userName"));
        dsc.setPassword(rb.getString("password"));
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(rb.getString("parent"));
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setEntity("model");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return rb.getString("OutputDirXml") + "/mapper/" + tableInfo.getEntityName() + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setInclude(new String[]{"my_plan","my_plan_type"});
        strategy.setTablePrefix("my_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

7、执行 CodeGenerator的main()就可以反向生成代码了

框起来的新生成的代码
springboot 继承 mybatis-plus 根据数据库(postgresql)中的表反向生成代码_第4张图片

8、给启动类加注解

配置扫描地址
@MapperScan(value = “com.mine.manager.plan.mapper”

9、控制层(Controller)

将注解@Conroller 改成 @RestController

10、给持久层加注解(可以不加)

@Repository

完毕!

你可能感兴趣的:(框架,mybatisPlus,springboot)