大家好,今天有点空,和大家分享一下mybatisgenerator 和 mybatisPlusGenerator两个自动化代码生成器,mybatisgenerator是mybatis官方提供给我们的一款代码生成器,而mybatisplus是苞米豆贡献给开源社区的一个框架,它只对mybatis的功能做了一定的增加,完全兼容mybatis,大家想详细了解可以去官网看看,闲话不多说了,开整
1、pom.xml添加依赖
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
8.0.15
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.5
org.mybatis.generator
mybatis-generator-core
1.3.5
mysql
mysql-connector-java
8.0.15
true
false
src/main/resources/mybatis-generator.xml
2、创建mybatis-generator.xml
3、在pom.xml同一级目录使用maven命令生成文件
mvn mybatis-generator:generate
?,到此就用mybatisgenerator把实体类,mapper和dao生成好的
1、pom.xml添加依赖
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
8.0.15
com.baomidou
mybatis-plus-boot-starter
3.1.0
com.baomidou
mybatis-plus-generator
3.1.0
org.apache.velocity
velocity-engine-core
2.0
org.springframework.boot
spring-boot-maven-plugin
2、写MyBatisPlusGenerator类,生成代码
package cn.zilanxuan;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
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.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class MybatisPlusGenerator {
/**
* 项目路径 修改为你项目的实际绝对路径
*
*/
private static String canonicalPath = "D:\\Sxy_Document\\Workspace\\mybatis-plus";
/**
* 基本包名
*/
private static String basePackage = "cn.zilanxuan";
/**
* 作者
*/
private static String authorName = "sxy";
private static String[] tables = {"user_msg"};
/**
* table前缀
*/
private static String[] prefix = {"r_basic","t_basic","t_matrix","t_node"};
/**
* 数据库类型
*/
private static DbType dbType = DbType.MYSQL;
/**
* 数据库配置四要素
*/
private static String driverName = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/nmg_jyj?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai";
private static String username = "root";
private static String password = "zilanxuan";
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
/**
* 获取项目路径
*/
try {
canonicalPath = new File("").getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
// 全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(canonicalPath + "/src/main/java");
globalConfig.setFileOverride(true);
globalConfig.setActiveRecord(true);
globalConfig.setEnableCache(false);
globalConfig.setBaseResultMap(true);
globalConfig.setBaseColumnList(true);
globalConfig.setOpen(true);
globalConfig.setAuthor(authorName);
globalConfig.setMapperName("%sMapper");
globalConfig.setXmlName("%sMapper");
globalConfig.setServiceName("%sService");
globalConfig.setServiceImplName("%sServiceImpl");
globalConfig.setControllerName("%sController");
mpg.setGlobalConfig(globalConfig);
// 数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDriverName(driverName);
dataSourceConfig.setDbType(dbType);
dataSourceConfig.setUrl(url);
dataSourceConfig.setUsername(username);
dataSourceConfig.setPassword(password);
mpg.setDataSource(dataSourceConfig);
// 包配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent(basePackage);
packageConfig.setController("controller");
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
mpg.setPackageInfo(packageConfig);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
/**
* 注入自定义配置
*/
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig abc = new InjectionConfig() {
@Override
public void initMap() {
Map map = new HashMap();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
//自定义文件输出位置(非必须)
List fileOutList = new ArrayList();
fileOutList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return canonicalPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
abc.setFileOutConfigList(fileOutList);
mpg.setCfg(abc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setTablePrefix(prefix);
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setInclude(tables);
strategyConfig.setEntityBuilderModel(true);
strategyConfig.setRestControllerStyle(true);
mpg.setStrategy(strategyConfig);
mpg.execute();
}
}
3、至此就 可以右击Run As Java Application 就可以生成controller,service,dao,mapper,实体类了