Springboot + MyBatis Plus自动生成代码工具类,简单好用。

想要的效果:

一键生成数据库表对应的Controller、Entity、Service、Mapper、Mapper.xml

数据库表字段如有加备注会自动加到Entity类中,所以建议表都加上备注。

前置依赖:

数据库配置:

spring.datasource.name=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

pom.xml依赖:


            mysql
            mysql-connector-java
            5.1.42
        

        
            com.baomidou
            mybatis-plus-boot-starter
            2.3.3
        

        
            org.apache.velocity
            velocity-engine-core
            2.0
        

1、创建一个MpHelper工具类,用来配置自动生成代码参数

2、右键执行main方法执行生成代码

执行参数:
1.输出文件的路径
2.数据库用户名
3.数据库密码
4.该代码所属的包名
5.表名称,多个表用逗号隔开。

3、MpHelper工具类代码如下:


import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.util.StringUtils;

public class MpHelper {
 private static final Logger logger = LoggerFactory.getLogger(MpHelper.class);
 private String outPutDir = "D://";
 private String dbUserName;
 private String dbPassword;
 private String dbUrl;
 private String moduleName = "test";
 private boolean fileOverride = true;
 private String[] includeTable;
 private String[] excludeTable;

 public void setFileOverride(boolean fileOverride) {
     this.fileOverride = fileOverride;
 }

 public void setIncludeTable(String[] includeTable) {
     this.includeTable = includeTable;
 }

 public void setExcludeTable(String[] excludeTable) {
     this.excludeTable = excludeTable;
 }

    /** * 引用用参数类 * @param outPutDir 生成代码的位置 * @param dbUserName 数据库用户名 * @param dbPassword 数据库密码 * @param dbUrl 数据库URL * @param moduleName 生成代码的所属包的名称 */
 public MpHelper(String outPutDir, String dbUserName, String dbPassword, String dbUrl, String moduleName ) {
     this.outPutDir = outPutDir;
     this.dbUserName = dbUserName;
     this.dbPassword = dbPassword;
     this.dbUrl = dbUrl;
     this.moduleName = moduleName;
 }

 public void execute() {
     if (StringUtils.isEmpty(this.dbUserName)) {
         logger.error("数据库连接信息不能为空");
     } else {
         AutoGenerator mpg = new AutoGenerator();
         GlobalConfig gc = new GlobalConfig();
         gc.setOutputDir(this.outPutDir);
         gc.setFileOverride(true);
         gc.setActiveRecord(true);
         gc.setEnableCache(false);
         gc.setBaseResultMap(false);
         gc.setBaseColumnList(false);
         gc.setAuthor("jack");
         mpg.setGlobalConfig(gc);
         DataSourceConfig dsc = new DataSourceConfig();
         dsc.setDbType(DbType.MYSQL);
         dsc.setTypeConvert(new MySqlTypeConvert() {
             public DbColumnType processTypeConvert(String fieldType) {
                 return super.processTypeConvert(fieldType);
             }
         });
// dsc.setDriverName("com.mysql.cj.jdbc.Driver"); //mysql 8.0以上版本 需要用这个驱动
         dsc.setDriverName("com.mysql.jdbc.Driver");
         dsc.setUsername(this.dbUserName);
         dsc.setPassword(this.dbPassword);
         dsc.setUrl(this.dbUrl);
         mpg.setDataSource(dsc);
         StrategyConfig strategy = new StrategyConfig();
         strategy.setNaming(NamingStrategy.underline_to_camel);
         strategy.setInclude(this.includeTable);
         strategy.setExclude(this.excludeTable);
         mpg.setStrategy(strategy);
         PackageConfig pc = new PackageConfig();
         pc.setParent("com.test");
         pc.setModuleName(this.moduleName);
         mpg.setPackageInfo(pc);
         mpg.execute();
     }

    }

    /** * 测试 * @param args */
    public static void main(String[] args) {
        MpHelper mh = new MpHelper("D:\\workspace\\test\\src\\main\\java",
                "username",
                "password",
                "jdbc:mysql://192.192.192.192:3306/demo?useSSL=false",
                "test");
        mh.setIncludeTable(new String []{"t_table1","t_table2"});// 需要自动生成的table 列表
        mh.execute();
    }
}

生成效果:

Springboot + MyBatis Plus自动生成代码工具类,简单好用。_第1张图片

你可能感兴趣的:(springboot,JAVA,mybatis)