mybatisPlus代码生成器构造和使用

紧接着最简单的MAVEN+Spingboot+mybatisplus的多模块配置,这篇我们来研究mybatisPlus代码生成器是如何构造和使用的

构造mybatisPlus代码生成器

在pom文件中添加以下代码

 		<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency> 

<build>
		<finalName>child</finalName>
		<plugins>  
	        <plugin>
	           <groupId>org.mybatis.generator</groupId>
	           <artifactId>mybatis-generator-maven-plugin</artifactId>
	           <version>1.3.2</version>
	           <executions>
	              <execution>
	                 <id>Generate MyBatis Files</id>
	                 <goals>
	                    <goal>generate</goal>
	                 </goals>
	                 <phase>generate</phase>
	                 <configuration>
	                    <verbose>true</verbose>
	                    <overwrite>true</overwrite>
	                 </configuration>
	              </execution>
	              </executions>
  </plugin>
  </plugins>
  </build>

创建CodeGeneration.java文件

package org.child.generate;

import com.baomidou.mybatisplus.annotation.DbType;
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;

/**
 *  *   * @ClassName: CodeGeneration  * @Description: 代码生成器  * @date 2019/1/8  
 */
public class CodeGeneration {
	public static void main(String[] args) {
		AutoGenerator mpg = new AutoGenerator();

		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir("E://myproject/mybatisPlus/mybatisPlusChild/src/main/java");
		gc.setFileOverride(true);
		gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
		gc.setEnableCache(false);// XML 二级缓存
		gc.setBaseResultMap(true);// XML ResultMap
		gc.setBaseColumnList(false);// XML columList
		gc.setAuthor("Walle");// 作者

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

		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL);
		dsc.setDriverName("com.mysql.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("123456");
		dsc.setUrl("jdbc:mysql://localhost:3306/test");
		mpg.setDataSource(dsc);

		StrategyConfig strategy = new StrategyConfig();
		strategy.setTablePrefix(new String[] { "itcast_" });// 此处可以修改为您的表前缀
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		strategy.setInclude(new String[] { "u_test" }); // 需要生成的表

		strategy.setSuperServiceClass(null);
		strategy.setSuperServiceImplClass(null);
		strategy.setSuperMapperClass(null);

		mpg.setStrategy(strategy);

		PackageConfig pc = new PackageConfig();
		pc.setParent("org.child");
		pc.setController("controller");
		pc.setService("service");
		pc.setServiceImpl("serviceImpl");
		pc.setMapper("mapper");
		pc.setEntity("entity");
		pc.setXml("xml");
		mpg.setPackageInfo(pc);
		mpg.execute();
	}
}

使用方法

运行CodeGeneration.java
自动生成controller、service、mapper等文件
在这里插入图片描述
对应test表
在这里插入图片描述

最后给出springboot+mybatisPlus+代码生成器完整项目地址:https://github.com/TheMingH/CodeGeneration

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值