MyBatis plus自动生成代码工具类

mybatis plus 自动生成代码 工具类。

生成数据库表对应的Controller、Entity、Service、Mapper、Mapper.xml
参数:
1.输出文件的路径
2.数据库用户名
3.数据库密码
4.该代码所属的包名
5.表名称,多个表用逗号隔开。

        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();


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.bdxh");
         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();
    }
}

猜你喜欢

转载自blog.csdn.net/fangchao2011/article/details/88785783