springboot 配置mybatis-plus

  • 添加pom依赖

            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.6
        
        
            org.apache.velocity
            velocity-engine-core
            2.0
        
        
        
            com.alibaba
            fastjson
            1.2.47
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            com.alibaba
            druid
            1.1.9
        
  • 2.配置generatorConfig.xml
package com.example.untils;

import com.baomidou.mybatisplus.annotation.DbType;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     :Wilson
 * @ Date       :Created in 17:01 2018/12/19
 * @ Description:${description}
 * @ Modified By:
 * @Version: $version$
 */
public class MpGenerator {
    private static String url = "jdbc:mysql://localhost:3306/boot?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
    private static String user = "root";
    private static String password = "root";
    private static String dirverName = "com.mysql.cj.jdbc.Driver";
    private static String author = "cj";
    private static String outputDir = "src/main/java"; //文件生成路径
    private static String packageName = "com.example";//文件生成所放的包名

    public static void main(String[] args) {
        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(url)
                .setUsername(user)
                .setPassword(password)
                .setDriverName(dirverName);
        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true) // 全局大写命名 ORACLE 注意
                //.setEntityLombokModel(false)
                .setNaming(NamingStrategy.underline_to_camel); // 表名生成策略
        //全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false)  // 不需要ActiveRecord特性的请改为false
                .setEnableCache(false) // XML 二级缓存
                .setAuthor(author)
                .setOutputDir(outputDir) //生成路径
                .setFileOverride(true)  //是否覆盖
                .setServiceName("%sService")
                .setEntityName("%Mo"); //自定义文件命名, %s 会自动填充表实体属性

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageName);

        // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };

        //调整xml 生成目录
       List focList= new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return "src/main/resources/mybatis/" + tableInfo.getEntityName() + ".xml";
            }
        });
        cfg.setFileOutConfigList(focList);

        // 关闭默认 xml 生成,调整生成 至 根目录
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);  // 上面调整xml 生成目录,这里一定要关闭才能生效

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo( pc
                ).setCfg(cfg)
                .setTemplate(tc)
                .execute();
    }

}

  • 运行之后就会生成对应的文件,

你可能感兴趣的:(springboot 配置mybatis-plus)