Mybatis-plus自动生成代码

依赖引入


            com.baomidou
            mybatis-plus-generator
            3.3.0
        

        
            org.apache.velocity
            velocity-engine-core
            2.1
        

编写自动配置类

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;

public class MysqlGenerator {
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        // 输入表名称
        String tableName = "student";
        String packetName = "";

        AutoGenerator mpg2 = new AutoGenerator();
        //1、全局配置
        GlobalConfig gc2 = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
        //TODO 输出地址填写全路径
        gc2.setOutputDir("D:\\workspace\\xiaobai-middle\\xiaobai-component\\xiaobai-cloud\\xiaobai-cloud-server\\src\\main\\java");
        //生成路径(一般都是生成在此项目的src/main/java下面)
        gc2.setAuthor("xiaobai"); //设置作者
        gc2.setOpen(false);
        gc2.setFileOverride(true); //第二次生成会把第一次生成的覆盖掉
        //gc2.setServiceName("%sIService"); //生成的service接口名字首字母是否为I,这样设置就没有。不配置默认servcie类名称前面带I,比如Iservice。
        gc2.setBaseResultMap(true); //生成resultMap
        mpg2.setGlobalConfig(gc2);

        //2、数据源配置
        DataSourceConfig dsc2 = new DataSourceConfig();
        dsc2.setUrl("jdbc:mysql://127.0.0.1:3306/xiaobai_cloud_test?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
        dsc2.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc2.setUsername("root");
        dsc2.setPassword("123456");
        mpg2.setDataSource(dsc2);

        // 3、包配置
        PackageConfig pc2 = new PackageConfig();
        pc2.setParent("");
        //pc2.setController("com.xiaobai.centralization.provide.controller");
        pc2.setEntity("com.xiaobai.server.domain.generator.student.entity");
        pc2.setService("com.xiaobai.server.domain.generator.student.service");
        pc2.setServiceImpl("com.xiaobai.server.domain.generator.student.service.Impl");
        pc2.setMapper("com.xiaobai.server.domain.generator.student.mapper");
        pc2.setXml("com.xiaobai.server.domain.generator.student.mapper");
        mpg2.setPackageInfo(pc2);
        // 4、策略配置
        StrategyConfig strategy2 = new StrategyConfig();
        strategy2.setNaming(NamingStrategy.underline_to_camel);
        strategy2.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy2.setSuperControllerClass("com.lcy.demo.sys.controller.BaseController");
//        strategy2.setSuperEntityClass("com.lcy.demo.sys.entity.BaseEntity");
        // strategy.setTablePrefix("t_"); // 表名前缀
        strategy2.setEntityLombokModel(true); //使用lombok
        strategy2.setInclude(tableName);  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
        mpg2.setStrategy(strategy2);

        //5、执行
        mpg2.execute();
    }
}

你可能感兴趣的:(Java,mybatis,java,mysql)