温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatis-plus怎么自动生成类

发布时间:2021-06-30 16:51:27 来源:亿速云 阅读:125 作者:chen 栏目:大数据

本篇内容介绍了“mybatis-plus怎么自动生成类”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

目录

pom 依赖
执行sql
demo

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.1.2</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
</dependency>

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
</dependency>

执行sql

CREATE TABLE if not exists `tbl_product_category` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `category_pic_code` varchar(100) NOT NULL comment '图片地址id',
  `name` varchar(255) NOT NULL comment '产品类别名字', 
  `status` bit(1) NOT NULL  default 1 comment '0 无效 1有效',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)  
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 comment='产品类别信息';

执行代码

public static void main(String[] args) {

        String projectPath = System.getProperty("user.dir");
        String outputDir = projectPath + "/src/main/java"; //生成文件输出目录
        String author = "duanlsh"; //注释作者
        String entity = "ProductCategory"; //生成文件实体名称
        String entityPrefix = "tbl_"; //前缀
        String[] tableName = "tbl_product_category".split(","); //表名多个以 , 分割
        String packageName = "com.apple"; //生成文件的 包名

        //数据库
        String dbUrl = "jdbc:mysql://localhost:3306/grainfull?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("root")
                .setDriverName("com.mysql.cj.jdbc.Driver");


        GlobalConfig config = new GlobalConfig();
        config.setAuthor(author);
        config.setOutputDir(outputDir);
        config.setFileOverride(true);
        config.setOpen(false); //文件生成完是否打开目录
        config.setEntityName("%sModel");//实体类添加Model
        config.setServiceName("%sService");
        config.setControllerName("%sController");
        config.setServiceImplName("%sServiceImpl");
        config.setMapperName("%sMapper");
        config.setXmlName("%sMapper");

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true) //设置是否使用 lombook
//                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setRestControllerStyle(true)
                .setControllerMappingHyphenStyle(true)
//                .setSuperEntityClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl")
//                .setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper")
                .setInclude(tableName)//修改替换成你需要的表名,多个表名传数组
                .setTablePrefix(entityPrefix)
                ;

        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                //do nothing
            }
        };

        PackageConfig packageConfig = new PackageConfig();
//        packageConfig.setModuleName("ProductCategory"); //在parent包下面添加的包名 eg: com.apple.ProductCategory
        packageConfig.setParent(packageName);
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setEntity("model");


//        TemplateConfig templateConfig  = new TemplateConfig();
//        templateConfig.setEntity("/templates/entity2.java");
//        templateConfig.setXml(null);

        AutoGenerator mpg = new AutoGenerator();
//        mpg.setTemplate(templateConfig);
        mpg.setGlobalConfig(config);
        mpg.setDataSource(dataSourceConfig);
        mpg.setStrategy(strategyConfig);
        mpg.setCfg(cfg);
        mpg.setPackageInfo(packageConfig);
        mpg.execute();
    }

“mybatis-plus怎么自动生成类”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI