mybatis-plus代码生成工具的使用

代码生成工具的使用:

1、创建一个springboot的工程:

工程所需依赖:小辣椒、mysql server

plus生成工具官网上给的依赖:https://baomidou.com/guide/generator.html#%E5%AD%97%E6%AE%B5%E5%85%B6%E4%BB%96%E4%BF%A1%E6%81%AF%E6%9F%A5%E8%AF%A2%E6%B3%A8%E5%85%A5

        //生成工具的依赖
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        //生成器的依赖
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        //mybatis与springboot整合jar包
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        //工具里所需的freemarker模板依赖
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>

下面是生成工具代码,正常的话是手打需要生成的数据表名,经过我的改良直接生成当前指定数据库里的所有表的所需的代码:

package com;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
//    这些是一定要改的,下面main方法里的根据自己需求改
    public static String url = "jdbc:mysql://localhost:3306/vueblog?serverTimezone=UTC&useSLL=false";
    public static String driver = "com.mysql.cj.jdbc.Driver";
    public static String username = "root";
    public static String password = "root";
//   获取要生成的表
    public static String[] getTables(){
        List<String> list = new ArrayList<>();
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,username,password);
            String sql = "show tables";
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            while (rs.next()){
                list.add(rs.getString(1));
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return list.toArray(new String[list.size()]);
    }
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        //生成文件的输出目录
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        // Author设置作者
        gc.setAuthor("mybatis-plus");
        // 文件覆盖
        gc.setFileOverride(true);
        // 生成后打开文件
        gc.setOpen(false);
//         gc.setSwagger2(true); //实体属性 Swagger2 注解
        // 自定义文件名风格,%s自动填充表实体属性
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setEntityName("%s");
        gc.setControllerName("%sController");

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driver);
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        // 父包和子包名分开处理
        pc.setParent("com.blog");
        pc.setController("controller");
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setXml("mapper.mapper");
        mpg.setPackageInfo(pc);
        // 生成策略配置
        StrategyConfig strategy = new StrategyConfig();
        //设置命名格式
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // 实体是否为lombok模型,默认 false
        strategy.setEntityLombokModel(true);
        //生成 @RestController 控制器
        strategy.setRestControllerStyle(true);
        // 驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        //表和前缀处理
        strategy.setInclude(getTables());
        //去除数据库表前缀
        String[] tablePre = new String[]{"m_"};
        strategy.setTablePrefix(tablePre);
        mpg.setStrategy(strategy);
//        这里需要fremewok的模板
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        // 执行,以上相关参数可以基于动态输入获取
        mpg.execute();
    }
}

最终会生成Controller、Service、ServiceImpl、mapper、xmlmapper

image-20201223155920321

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值