mybatis-plus代码自动生成

2022/1/3 6:15:21

本文主要是介绍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.setVersionFieldName("version");*/
        strategyConfig.setLogicDeleteFieldName("deleted");
        // 自动填充配置
        TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);

        ArrayList<TableFill> 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和上面代码相关的依赖

	<dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
		<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
	</dependency>



这篇关于mybatis-plus代码自动生成的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程