栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus自动生成代码(简洁)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus自动生成代码(简洁)

mybatis-plus自动生成代码(简洁版本)
package com.netft;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;



public class FengCode {
    public static void main(String[] args) {
        // 构建一个代码生成器对象
        AutoGenerator generator = new AutoGenerator();
        // 配置策略
        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("ft"); //设置作者
        gc.setOpen(false); // 是否打开资源管理器
        gc.setFileOverride(false); // 是否覆盖
        gc.setServiceName("%sService"); // 去Service的I前缀
        gc.setIdType(IdType.ID_WORKER_STR); // string类型的主键id   雪花算法
        gc.setDateType(DateType.ONLY_DATE); // 日期的类型
        gc.setSwagger2(false);
        generator.setGlobalConfig(gc);
        // 2. 设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mp?serverTimezone=UTC&characterEncoding=utf8&useSSL=false");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        generator.setDataSource(dsc);
        // 3. 包的配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.netft");
        pc.setEntity("pojo");   // 实体包名
        pc.setMapper("mapper"); // 持久层包名
        pc.setService("service"); // 服务层包名
        pc.setController("controller");
        generator.setPackageInfo(pc);

        // 4. 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix("tb_");  // 去掉表明前缀
        strategy.setNaming(NamingStrategy.underline_to_camel); // 包下划线转驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);  // 列名下划线转驼峰
        strategy.setEntityLombokModel(true);  // 是否使用lombok
        strategy.setLogicDeleteFieldName("deleted"); // 逻辑删除
//        // 自动填充配置
//        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
//        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
//        ArrayList tableFills = new ArrayList<>();
//        tableFills.add(createTime);
//        tableFills.add(updateTime);
//        strategy.setTableFillList(tableFills);
//        // 乐观锁的配置
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true); // 开启restful风格
//        strategy.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2
        strategy.setInclude("tb_user");   // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
        generator.setStrategy(strategy);
        // 执行
        generator.execute();

    }
}

生成效果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号