SpringBoot-2.X 学习笔记11 整合 Mybatis-Plus 代码自动生成

SpringBoot-2.X 学习笔记10 整合 WebFlux

  • 1 添加依赖
  • 2 生成代码
  • 3 生成结果

1 添加依赖


<dependency>
	<groupId>com.baomidougroupId>
	<artifactId>mybatis-plusartifactId>
	<version>2.0.7version>
dependency>


<dependency>
	<groupId>org.apache.velocitygroupId>
	<artifactId>velocityartifactId>
	<version>1.7version>
dependency>

2 生成代码

package com.xu.springboot.utils;

import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
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 com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * Mybatis-Plus 代码自动生成器
 * @ClassName: MybatisPlusGenenator   
 * @Description: TODO   
 * @author: hyacinth
 * @date: 2019年7月22日 下午9:26:15     
 * @Copyright: hyacinth
 */
public class MybatisPlusGenenator {

	final static String  dirPath = "E://MpGenenator//";

	public static void main(String[] args) {
		AutoGenerator mpg = new AutoGenerator();
		// 选择 freemarker 引擎,默认 Veloctiy
		//mpg.setTemplateEngine(new FreemarkerTemplateEngine());

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir(dirPath);
		gc.setAuthor("hyacinth");
		gc.setFileOverride(true);// 是否覆盖
		gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
		gc.setEnableCache(false);// XML 二级缓存
		gc.setBaseResultMap(true);// XML ResultMap
		gc.setBaseColumnList(true);// XML columList

		// 自定义文件命名,注意 %s 会自动填充表实体属性!
		// gc.setMapperName("%sDao");
		gc.setXmlName("%sMapper");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setControllerName("%sController");
		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL);
		dsc.setTypeConvert(new MySqlTypeConvert(){
			// 自定义数据库表字段类型转换【可选】
			@Override
			public DbColumnType processTypeConvert(String fieldType) {
				System.out.println("转换类型:" + fieldType);
				// 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请自定义返回、非如下直接返回。
				return super.processTypeConvert(fieldType);
			}
		});
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("123456");
		dsc.setUrl("jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&characterEncoding=utf8");
		mpg.setDataSource(dsc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		// strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
		//strategy.setTablePrefix(new String[] { "tb_", "tsys_" });// 此处可以修改为您的表前缀
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		// strategy.setInclude(new String[] {"user"}); // 需要生成的表
		// strategy.setExclude(new String[] {"test"}); // 排除生成的表
		// 自定义实体父类
		// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
		// 自定义实体,公共字段
		// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
		// 自定义 mapper 父类
		// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
		// 自定义 service 父类
		// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
		// 自定义 service 实现类父类
		// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
		// 自定义 controller 父类
		// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
		// 【实体】是否生成字段常量(默认 false)
		// public static final String ID = "test_id";
		// strategy.setEntityColumnConstant(true);
		// 【实体】是否为构建者模型(默认 false)
		// public User setName(String name) {this.name = name; return this;}
		strategy.setEntityBuilderModel(true);
		mpg.setStrategy(strategy);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.xu");
		pc.setModuleName("springboot");
		pc.setController("controler");
		pc.setEntity("entity");
		pc.setMapper("mapper");
		pc.setService("service");
		pc.setServiceImpl("serviceimpl");
		pc.setXml("mapperxml");

		mpg.setPackageInfo(pc);

		// 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
				this.setMap(map);
			}
		};
		mpg.setCfg(cfg);
		
		// 自定义 表对应的 xxList.jsp 生成
		//List focList = new ArrayList();
		//focList.add(new FileOutConfig("/template/list.jsp.vm") {
		//	@Override
		//	public String outputFile(TableInfo tableInfo) {
		//		// 自定义输入文件名称
		//		return "D://my_" + tableInfo.getEntityName() + ".jsp";
		//	}
		//});
		//cfg.setFileOutConfigList(focList);
		//mpg.setCfg(cfg);

		// 调整 xml 生成目录演示
		//focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
		//	@Override
		//	public String outputFile(TableInfo tableInfo) {
		//		return dirPath + tableInfo.getEntityName() + "Mapper.xml";
		//	}
		//});
		//cfg.setFileOutConfigList(focList);
		//mpg.setCfg(cfg);

		// 关闭默认 xml 生成,调整生成 至 根目录
		//TemplateConfig tc = new TemplateConfig();
		//tc.setXml(null);
		//mpg.setTemplate(tc);

		// 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
		// 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称
		// TemplateConfig tc = new TemplateConfig();
		// tc.setController("...");
		// tc.setEntity("...");
		// tc.setMapper("...");
		// tc.setXml("...");
		// tc.setService("...");
		// tc.setServiceImpl("...");
		// 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
		// mpg.setTemplate(tc);

		// 执行生成
		mpg.execute();

		// 打印注入设置【可无】
		System.err.println(mpg.getCfg().getMap().get("abc"));
	}

}

3 生成结果

21:43:40.101 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
21:43:40.108 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
转换类型:int(11)
转换类型:varchar(40)
转换类型:int(11)
转换类型:varchar(4)
转换类型:varchar(40)
转换类型:varchar(40)
转换类型:int(11) unsigned
转换类型:varchar(128)
转换类型:varchar(16)
转换类型:datetime
转换类型:int(4)
21:43:40.855 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 创建目录: [E://MpGenenator//\com\xu\springboot\serviceimpl]
21:43:40.855 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 创建目录: [E://MpGenenator//\com\xu\springboot\entity]
21:43:40.856 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 创建目录: [E://MpGenenator//\com\xu\springboot\controler]
21:43:40.856 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 创建目录: [E://MpGenenator//\com\xu\springboot\mapperxml]
21:43:40.856 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 创建目录: [E://MpGenenator//\com\xu\springboot\mapper]
21:43:40.857 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 创建目录: [E://MpGenenator//\com\xu\springboot\service]
21:43:40.882 [main] DEBUG org.apache.velocity - CommonsLogLogChute name is 'org.apache.velocity'
21:43:40.882 [main] DEBUG org.apache.velocity - Initializing Velocity, Calling init()...
21:43:40.882 [main] DEBUG org.apache.velocity - Starting Apache Velocity v1.7 (compiled: 2010-11-19 12:14:37)
21:43:40.882 [main] DEBUG org.apache.velocity - Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties
21:43:40.882 [main] DEBUG org.apache.velocity - Trying to use logger class org.apache.velocity.runtime.log.AvalonLogChute
21:43:40.882 [main] DEBUG org.apache.velocity - Target log system for org.apache.velocity.runtime.log.AvalonLogChute is not available (java.lang.NoClassDefFoundError: org/apache/log/format/Formatter).  Falling back to next log system...
21:43:40.882 [main] DEBUG org.apache.velocity - Trying to use logger class org.apache.velocity.runtime.log.Log4JLogChute
21:43:40.882 [main] DEBUG org.apache.velocity - Target log system for org.apache.velocity.runtime.log.Log4JLogChute is not available (java.lang.NoClassDefFoundError: org/apache/log4j/Layout).  Falling back to next log system...
21:43:40.882 [main] DEBUG org.apache.velocity - Trying to use logger class org.apache.velocity.runtime.log.CommonsLogLogChute
21:43:40.882 [main] DEBUG org.apache.velocity - Using logger class org.apache.velocity.runtime.log.CommonsLogLogChute
21:43:40.885 [main] DEBUG org.apache.velocity - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.893 [main] DEBUG org.apache.velocity - ResourceCache: initialized (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class java.util.Collections$SynchronizedMap cache map.
21:43:40.894 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
21:43:40.895 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Define
21:43:40.895 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Break
21:43:40.896 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
21:43:40.896 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Literal
21:43:40.897 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
21:43:40.897 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
21:43:40.898 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Include
21:43:40.899 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
21:43:40.920 [main] DEBUG org.apache.velocity - Created '20' parsers.
21:43:40.924 [main] DEBUG org.apache.velocity - Velocimacro : "velocimacro.library" is not set.  Trying default library: VM_global_library.vm
21:43:40.924 [main] DEBUG org.apache.velocity - Could not load resource 'VM_global_library.vm' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader: ClasspathResourceLoader Error: cannot find resource VM_global_library.vm
21:43:40.924 [main] DEBUG org.apache.velocity - Velocimacro : Default library not found.
21:43:40.924 [main] DEBUG org.apache.velocity - Velocimacro : allowInline = true : VMs can be defined inline in templates
21:43:40.924 [main] DEBUG org.apache.velocity - Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
21:43:40.924 [main] DEBUG org.apache.velocity - Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed.
21:43:40.924 [main] DEBUG org.apache.velocity - Velocimacro : autoload off : VM system will not automatically reload global library macros
21:43:40.940 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.950 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/entity.java.vm;  文件:E://MpGenenator//\com\xu\springboot\entity\User.java
21:43:40.951 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.952 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/mapper.java.vm;  文件:E://MpGenenator//\com\xu\springboot\mapper\UserMapper.java
21:43:40.954 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.955 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/mapper.xml.vm;  文件:E://MpGenenator//\com\xu\springboot\mapperxml\UserMapper.xml
21:43:40.956 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/service.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.957 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/service.java.vm;  文件:E://MpGenenator//\com\xu\springboot\service\UserService.java
21:43:40.957 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/serviceImpl.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.958 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/serviceImpl.java.vm;  文件:E://MpGenenator//\com\xu\springboot\serviceimpl\UserServiceImpl.java
21:43:40.959 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.960 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/controller.java.vm;  文件:E://MpGenenator//\com\xu\springboot\controler\UserController.java
21:43:40.963 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.965 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/entity.java.vm;  文件:E://MpGenenator//\com\xu\springboot\entity\Student.java
21:43:40.966 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.967 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/mapper.java.vm;  文件:E://MpGenenator//\com\xu\springboot\mapper\StudentMapper.java
21:43:40.968 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.969 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/mapper.xml.vm;  文件:E://MpGenenator//\com\xu\springboot\mapperxml\StudentMapper.xml
21:43:40.969 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/service.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.970 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/service.java.vm;  文件:E://MpGenenator//\com\xu\springboot\service\StudentService.java
21:43:40.971 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/serviceImpl.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.972 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/serviceImpl.java.vm;  文件:E://MpGenenator//\com\xu\springboot\serviceimpl\StudentServiceImpl.java
21:43:40.973 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
21:43:40.973 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - 模板:/templates/controller.java.vm;  文件:E://MpGenenator//\com\xu\springboot\controler\StudentController.java
21:43:40.998 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================
hyacinth-mp

SpringBoot-2.X 学习笔记11 整合 Mybatis-Plus 代码自动生成_第1张图片

你可能感兴趣的:(Java,SprinBoot-2.X,学习笔记)