mybatis-plus-generator代碼生成器體驗

  • 介紹

    mybatis-generator是一款在使用mybatis框架時,自動生成model,dao和mapper的工具,很大程度上減少了業務開發人員的手動編碼時間,今天自己研究了一下,也分享一下使用心得供大家簡單使用。
  • 配置

  1. 添加maven依賴
    本人使用的是maven構建,首先需要在pom.xml文件添加mybatis-generator依賴包以及插件,配置如下:
    dependencies中添加:
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
    </dependency>
  1. 添加配置文件信息 mybatis-plus.properties
OutputDir=C:/Users/Administrator/Desktop/SpringBoot項目/mybatis-plus-AutoGenerator
#mapper.xml\u7684\u751F\u6210\u4F4D\u7F6E
OutputDirXml=C:/Users/Administrator/Desktop/SpringBoot項目/mybatis-plus-AutoGenerator
#\u6570\u636E\u5E93\u8868\u540D(\u6B64\u5904\u5207\u4E0D\u53EF\u4E3A\u7A7A\uFF0C\u5982\u679C\u4E3A\u7A7A\uFF0C\u5219\u9ED8\u8BA4\u8BFB\u53D6\u6570\u636E\u5E93\u7684\u6240\u6709\u8868\u540D)
tableName=t_user,t_file,t_directory,t_file_label,t_file_product,t_picture_library
#\u88C5\u4EE3\u7801\u7684\u6587\u4EF6\u5939\u540D
className=mapper
#\u8BBE\u7F6E\u4F5C\u8005
author=caoyn
#\u6B63\u5E38\u60C5\u51B5\u4E0B\uFF0C\u4E0B\u9762\u7684\u4EE3\u7801\u65E0\u9700\u4FEE\u6539\uFF01\uFF01\uFF01\uFF01\uFF01\uFF01\uFF01\uFF01\uFF01\uFF01
#\u81EA\u5B9A\u4E49\u5305\u8DEF\u5F84
parent=com.wego.clouddisk
#\u6570\u636E\u5E93\u5730\u5740
url=jdbc:mysql://localhost:3306/bestclouddisk?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
userName=root
password=199420
  1. 運行代碼
/**
 * mybatis-plus代碼生成器
 *
 * @author: caoyn
 * @create: 2020-04-08 13:22
 */
public class MpGenerator {
    public static void main(String[] args) {

        //用來獲取Mybatis-Plus.properties文件的配置信息
        final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus");

        // 代碼生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(rb.getString("OutputDir"));
        gc.setOpen(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setAuthor(rb.getString("author"));
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 數據源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl(rb.getString("url"));
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(rb.getString("userName"));
        dsc.setPassword(rb.getString("password"));
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(rb.getString("parent"));
        pc.setController("controller." + rb.getString("className"));
        pc.setService("service." + rb.getString("className"));
        pc.setServiceImpl("service." + rb.getString("className") + ".impl");
        pc.setEntity("domain." + rb.getString("className"));
        pc.setMapper("mapper." + rb.getString("className"));
        mpg.setPackageInfo(pc);

        // 自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        /*focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定義輸入文件名稱
                return rb.getString("OutputDirXml") + "/mapper/" + rb.getString("className") + "/" + tableInfo.getEntityName() + StringPool.DOT_XML;
            }
        });*/
        /*cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));*/

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setInclude(rb.getString("tableName").split(","));
        strategy.setTablePrefix("t_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章