springboot+mybatisplus逆向工程生成代码

精选30+云产品,助力企业轻松上云!>>> hot3.png

1.引入pom依赖

		
            com.baomidou
            mybatis-plus-generator
            3.3.1.tmp
        
        
            org.apache.velocity
            velocity-engine-core
            2.2
        

2.Code

package com.wang.mybatisplus;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MybatisPlusGeneratorApplicationTests {

    @Test
    void codeGenerator () {

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

        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setActiveRecord(true);//支持AR模式
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setFileOverride(true);//文件覆盖
        gc.setIdType(IdType.AUTO);//主键自增
        gc.setServiceName("%sService");//设置接口名称是否有I
        gc.setAuthor("王一宁");
        gc.setBaseResultMap(true);//xml映射
        gc.setBaseColumnList(true);//sql片段
        mpg.setGlobalConfig(gc);

        // 2.数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mp?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8");
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 3.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true);//全局大小写命名
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setTablePrefix("tbl_");
        strategyConfig.setEntityLombokModel(true);//使用lombok
        strategyConfig.setInclude("tbl_user");//生成使用的表

        // 4.包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.wang.mybatisplus");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        pc.setEntity("domain");
        pc.setXml("mapper");
        mpg.setPackageInfo(pc);

        // 5.执行
        mpg.execute();
    }
}

你可能感兴趣的:(mybatis,generator,vue,spring,boot,mysql)