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

 更新时间:2020年03月25日 14:13:03   作者:sky豫  
这篇文章主要介绍了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<FileOutConfig> 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依赖

<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.0-RELEASE</version>
		</dependency>
		<!-- mp自动代码生成-->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.0.7.1</version>
		</dependency>
		<!-- velocity 模板引擎, 默认 -->
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity-engine-core</artifactId>
			<version>2.0</version>
		</dependency>
 
		<!-- freemarker 模板引擎 -->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.23</version>
		</dependency>
 
 
		<!-- beetl 模板引擎 -->
		<dependency>
			<groupId>com.ibeetl</groupId>
			<artifactId>beetl</artifactId>
			<version>2.2.5</version>
		</dependency>

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

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

相关文章

  • 实例讲解java定时任务

    实例讲解java定时任务

    这篇文章主要介绍了实例讲解java定时任务,感兴趣的的朋友可以参考下
    2015-08-08
  • Java中使用Filter过滤器的方法

    Java中使用Filter过滤器的方法

    Filter过滤器是javaWeb层面的,它跟Servlet类似,每次前端请求,首先进入的是过滤器,我们必须实现Filter接口,重写三个方法,才能使用Filter过滤器,需要的朋友可以参考下
    2021-06-06
  • springboot配置https安全连接的方法

    springboot配置https安全连接的方法

    这篇文章主要介绍了springboot配置https安全连接的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Java字母加数字组合比较大小

    Java字母加数字组合比较大小

    这篇文章主要通过实现Comarable接口来比较(如"a20"和"a9"这种)字符串的大小,希望能给大家做一个参考。
    2016-06-06
  • JUC三大辅助类CountDownLatch、CyclicBarrier和Semaphore详解

    JUC三大辅助类CountDownLatch、CyclicBarrier和Semaphore详解

    这篇文章主要介绍了JUC三大辅助类CountDownLatch、CyclicBarrier和Semaphore详解,CountDownLatch 类可以设置一个计数器,然后通过 countDown 方法来进行 减 1 的操作,使用 await 方法等待计数器不大于 0,然后继续执行 await 方法 之后的语句,需要的朋友可以参考下
    2024-01-01
  • Spring IOC容器FactoryBean工厂Bean实例

    Spring IOC容器FactoryBean工厂Bean实例

    这篇文章主要为大家介绍了Spring IOC容器FactoryBean工厂Bean实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • IntelliJ IDEA2020.3详细安装教程

    IntelliJ IDEA2020.3详细安装教程

    这篇文章主要介绍了IntelliJ IDEA2020.3详细安装教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 深入了解JAVA泛型

    深入了解JAVA泛型

    这篇文章主要介绍了JAVA泛型的相关知识,文中代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2020-06-06
  • JVM中-D、-X、-XX参数的区别

    JVM中-D、-X、-XX参数的区别

    本文主要介绍了JVM中-D、-X、-XX参数的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java创建多线程局域网聊天室实例

    Java创建多线程局域网聊天室实例

    这篇文章主要介绍了Java创建多线程局域网聊天室实例,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07

最新评论