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

mybatis-plus代码自动生成

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

mybatis-plus代码自动生成

备忘录:mybatis-plus代码自动生成 main方法
public static void main(String[] args) {
        // 需要构建一个 代码自动生成器 对象
        AutoGenerator autoGenerator = new AutoGenerator();
        // 配置策略

        // 1、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        //获取系统目录
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath + "/src/main/java");
        globalConfig.setAuthor("AbrahamCui");//作者
        globalConfig.setOpen(false);//生成后是否打开所在文件夹
        globalConfig.setFileOverride(false); // 是否覆盖
        globalConfig.setServiceName("%Service"); // 去Service的I前缀
        globalConfig.setIdType(IdType.ID_WORKER);//主键策略
        globalConfig.setDateType(DateType.ONLY_DATE);//日期配置
        globalConfig.setSwagger2(true);//自动配置swagger2
        autoGenerator.setGlobalConfig(globalConfig);//装入


        // 2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/music?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dsc);//装入

        // 3、包的设置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("music");
        pc.setParent("com.javaclimb");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        autoGenerator.setPackageInfo(pc);//装入

        // 4、策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        // 设置要映射的表名
        strategyConfig.setInclude("collect");
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true); // 自动lombok

        // 乐观锁
        
        strategyConfig.setLogicDeleteFieldName("deleted");
        // 自动填充配置
        TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);

        ArrayList tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);//装入
        tableFills.add(gmtModified);//装入
        strategyConfig.setTableFillList(tableFills);//装入


        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2
        autoGenerator.setStrategy(strategyConfig);//装入

        autoGenerator.execute();//执行
pom.xml和上面代码相关的依赖
	
            org.apache.velocity
            velocity-engine-core
            2.2
        
        
            com.baomidou
            mybatis-plus-generator
            3.0.5
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.5
        
		
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        
        
            org.projectlombok
            lombok
            1.18.6
	

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

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

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