微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mybatis-Plus-AutoGenerator 最详细使用方法

这篇文章主要介绍了Mybatis-Plus-AutoGenerator 最详细使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。可以通过模版等一系列的方式来生成代码,⚠️这个比mybatis-generator的更加强大,纯java代码。。官方地址:https://mp.baomidou.com/guide/generator.html

package com.cikers.ps; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class MysqLGenerator { public static String scanner(String tip) { Scanner scanner = new Scanner(system.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = "/Users/syk/Documents/*/*/"; gc.setoutputDir(projectPath + "/src/main/java"); gc.setAuthor("syk"); gc.setopen(false); gc.setBaseResultMap(true); gc.setBaseColumnList(true); //gc.setControllerName("SSSSScontroller"); // 是否覆盖已有文件 gc.setFileOverride(false); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:MysqL://******/newstack_db?useUnicode=true&characterEncoding=UTF-8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.MysqL.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("password"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); //pc.setModuleName(scanner("模块名")); pc.setParent(null); // 这个地址是生成配置文件的包路径 pc.setEntity("com.cikers.ps.model.entity"); //pc.setController("com.cikers.ps.controller"); pc.setMapper("com.cikers.ps.mapper"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 veLocity //String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 List focList = new ArrayList(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义输出文件名 return projectPath + "/src/main/resources/mapper/entity" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // //配置自定义输出模板 // 不需要其他的类型时,直接设置为null就不会成对应的模版了 //templateConfig.setEntity("..."); templateConfig.setService(null); templateConfig.setController(null); templateConfig.setServiceImpl(null); // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改, // 放置自己项目的 src/main/resources/templates 目录下, 名称一下可以不配置,也 // 可以自定义模板名称 只要放到目录下,名字不变 就会采用这个模版 下面这句有没有无所谓 // 模版去github上看地址: /**https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-generator/src/main/resources/templates*/ //templateConfig.setEntity("/templates/entity.java"); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setSuperEntityClass("com.cikers.ps.model.BaseEntity"); strategy.setSuperMapperClass("com.cikers.ps.util.IMapper"); strategy.setEntityLombokModel(false); //strategy.setRestControllerStyle(false); //strategy.setSuperControllerClass("com.cikers.ps.controller.MysqLController"); strategy.setInclude(scanner("表名")); // 设置继承的父类字段 strategy.setSuperEntityColumns("id","modifiedBy","modifiedOn","createdBy","createdOn"); //strategy.setControllerMappingHyphenStyle(true); //strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

其中需要的maven依赖

com.baomidoumybatis-plus-boot-starter3.0-RELEASEcom.baomidoumybatis-plus-generator3.0.7.1org.apache.veLocityveLocity-engine-core2.0org.freemarkerfreemarker2.3.23com.ibeetlbeetl2.2.5

 运行输入表面就可以了!!!!

到此这篇关于Mybatis-Plus-AutoGenerator 最详细使用方法文章就介绍到这了,更多相关Mybatis Plus AutoGenerator内容搜索编程之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程之家!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐