日常开发或接私活以后,有时候我们需要快速创建项目进行开发,除了需要项目框架模板(如SSM,SSH,Spring boot等框架模板),我们还需要快速的生成一些简易代码.
Mybatis generator代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
此版本mybatis gennerator无需配置任何配置文件,只需添加maven依赖后,执行test类就能生成,且生成的mapper接口简单易用.是目前配置较为简单,方法非常实用的,是目前较为优秀的mabatis自动生成工具.
****此种方发生成代码无需单独建立工程,而是将代码生成器放在项目的测试包下****
因此生产时,在建表之后,可以边开发边根据新的业务需求随时生成新的代码.如下图所示,test包下的CodeGenerator是生成代码的核心类.
以下是使用步骤:
- 添加maven依赖:
主要有3个依赖要添加: mybatis自动生成工具(org.mtbatis.generator),mybatis工具包(tk.mybatis),代码生成器依赖(org.freemarker)
<!--mybatis自动生成工具-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
<scope>test</scope>
</dependency>
<!-- mybatis 工具包-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.2</version>
</dependency>
<!--代码生成器依赖-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
<scope>test</scope>
</dependency>
2. 在工程的core包下放入2个接口(自动生成工具的核心继承接口,类名为Mapper和Service),这两个类的内容不能变,名称可以根据CodeGenerator类中的配置动态变化
代码如下:
package com.zt.core;
import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;
/**
* 定制版MyBatis Mapper插件接口,如需其他接口参考官方文档自行添加。
*/
public interface Mapper<T>
extends
BaseMapper<T>,
ConditionMapper<T>,
IdsMapper<T>,
InsertListMapper<T>{
}
package com.zt.core;
import tk.mybatis.mapper.entity.Condition;
import java.util.List;
public interface Service<T> {
long count(T model);
long countByCondition(Condition condition);
void insert(T model);
void insertSelective(T model);
void insertList(List<T> models);
void deleteByPrimaryKey(Object id);
void deleteByIds(String ids);
void deleteByCondition(Condition condition);
void updateByCondition(T model, Condition condition);
void updateByConditionSelective(T model, Condition condition);
void updateByPrimaryKey(T model);
void updateByPrimaryKeySelective(T model);
T selectByPrimaryKey(Object id);
T selectOne(T model);
List<T> selectAll();
T selectFirst(T model);
T selectFirstByCondition(Condition condition);
List<T> select(T model);
List<T> selectForStartPage(T model, Integer pageNum, Integer pageSize);
List<T> selectByIds(String ids);
List<T> selectByCondition(Condition condition);
List<T> selectByConditionForStartPage(Condition condition, Integer pageNum, Integer pageSize);
}
3.在test包的资源文件夹下(resources)添加模板.如下图
以下是模板资源内容(创建同名目录和文件,将代码复制进去即可)
controller.ftl:
package ${basePackage}.web;
import ${basePackage}.core.BasePageSearch;s
import ${basePackage}.dto.IDValid;
import ${basePackage}.result.PageResult;
import ${basePackage}.result.Result;
import ${basePackage}.result.ResultCode;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.service.${modelNameUpperCamel}Service;
import org.springframework.web.bind.annotation.*;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by ${author} on ${date}.
*/
@RestController
@RequestMapping("${baseRequestMapping}")
@Api(value = "xx", description = "xx")
public class ${modelNameUpperCamel}Controller {
@Resource
private ${modelNameUpperCamel}Service services;
@ApiOperation(value = "获取xx列表")
@RequestMapping(value = "/get_list", method = RequestMethod.POST)
@ResponseBody
public Object get_list(@RequestBody BasePageSearch<${modelNameUpperCamel}> modelValid) {
Condition condition = new Condition(${modelNameUpperCamel}.class);
Condition.Criteria criteria = condition.createCriteria();
// condition.setOrderByClause("ID DESC");
${modelNameUpperCamel} model = modelValid.getSearch() == null?new ${modelNameUpperCamel}():modelValid.getSearch();
//模型判断是否等于场景使用比如 ID = 10
// List<${modelNameUpperCamel}> lists = services.selectForStartPage(model,modelValid.getPage(),modelValid.getLimit());
// long total = services.count(model);
List<${modelNameUpperCamel}> lists = services.selectByConditionForStartPage(condition,modelValid.getPage(),modelValid.getLimit());
long total = services.countByCondition(condition);
return new Result(ResultCode.SUCCESS,lists,new PageResult(modelValid.getPage(),total));
}
@ApiOperation(value = "获取单条xx")
@RequestMapping(value = "/get_one", method = RequestMethod.POST)
@ResponseBody
public Object get_one(@RequestBody IDValid valid) {
${modelNameUpperCamel} model = services.selectByPrimaryKey(valid.getId());
return new Result(ResultCode.SUCCESS,model);
}
@ApiOperation(value = "新增单条xx")
@RequestMapping(value = "/add_one", method = RequestMethod.POST)
@ResponseBody
public Object add_one(@RequestBody ${modelNameUpperCamel} model) {
services.insertSelective(model);
return new Result(ResultCode.SUCCESS,model.getId());
}
@ApiOperation(value = "新增多条xx")
@RequestMapping(value = "/add_multiple", method = RequestMethod.POST)
@ResponseBody
public Object add_multiple(@RequestBody List<${modelNameUpperCamel}> model) {
services.insertList(model);
return new Result(ResultCode.SUCCESS);
}
@ApiOperation(value = "修改单条xx")
@RequestMapping(value = "/edit_one", method = RequestMethod.POST)
@ResponseBody
public Object edit_one(@RequestBody ${modelNameUpperCamel} model) {
services.updateByPrimaryKeySelective(model);
return new Result(ResultCode.SUCCESS);
}
@ApiOperation(value = "删除单条xx")
@RequestMapping(value = "/delete_one", method = RequestMethod.POST)
@ResponseBody
public Object delete_one(@RequestBody IDValid valid) {
services.deleteByPrimaryKey(valid.getId());
return new Result(ResultCode.SUCCESS);
}
@ApiOperation(value = "删除多条xx")
@RequestMapping(value = "/delete_multiple", method = RequestMethod.POST)
@ResponseBody
//ids 1,2,3,4
public Object delete_multiple(@RequestBody String ids) {
services.deleteByIds(ids);
return new Result(ResultCode.SUCCESS);
}
}
service.ftl:
package ${basePackage}.service;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.core.Service;
/**
* Created by ${author} on ${date}.
*/
public interface ${modelNameUpperCamel}Service extends Service<${modelNameUpperCamel}> {
}
service-impl.ftl
package ${basePackage}.service.impl;
import ${basePackage}.dao.${modelNameUpperCamel}Mapper;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.service.${modelNameUpperCamel}Service;
import ${basePackage}.core.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* Created by ${author} on ${date}.
*/
@Service
@Transactional
public class ${modelNameUpperCamel}ServiceImpl extends BaseService<${modelNameUpperCamel}> implements ${modelNameUpperCamel}Service {
@Resource
private ${modelNameUpperCamel}Mapper ${modelNameLowerCamel}Mapper;
}
4.在maven工程测试包下添加核心生成类CodeGenerator.java,执行此类的main方法,根据配置,就能生成数据表对应的Model,Mapper,Service,Controller等.代码如下(注:此类中有我的私有方类StringUtils,注意替换成您自己的),此类中的数据库和文件地址保证正确,否则代码生成失败!
import com.google.common.base.CaseFormat;
import com.zt.utils.StringUtils;
import freemarker.template.TemplateExceptionHandler;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
*/
public class CodeGenerator {
public static final String BASE_PACKAGE = "com.zt";//项目基础包名称,根据自己的项目修改
/*生成文件地址配置*/
public static final String MODEL_PACKAGE = BASE_PACKAGE + ".model";//生成的Model类所在包
public static final String MAPPER_PACKAGE = BASE_PACKAGE + ".dao";//生成的Mapper所在包
public static final String SERVICE_PACKAGE = BASE_PACKAGE + ".service";//生成的Service所在包
public static final String SERVICE_IMPL_PACKAGE = SERVICE_PACKAGE + ".impl";//生成的ServiceImpl所在包
public static final String CONTROLLER_PACKAGE = BASE_PACKAGE + ".controller";//生成的Controller所在包
public static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".core.Mapper";//Mapper插件基础接口的完全限定名(第二步提到的核心继承接口Mapper)
/*数据库配置*/
private static final String JDBC_URL = "jdbc:mysql://106.14.168.164:3306/zt";//数据库url
private static final String JDBC_USERNAME = "root";
private static final String JDBC_PASSWORD = "123456";
private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径
private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/test/resources/generator/template";//模板位置
private static final String JAVA_PATH = "/src/main/java"; //java文件路径
private static final String RESOURCES_PATH = "/src/main/resources";//资源文件路径
private static final String PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE);//生成的Service存放路径
private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径
private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径
private static final String AUTHOR = "KingZS";//@author
private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date
/*main函数入口,放入表名运行即可生成代码*/
public static void main(String[] args) {
genCode("program");
//genCodeByCustomModelName("输入表名","输入自定义Model名称");
}
/**
* 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
* 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
* @param tableNames 数据表名称...
*/
public static void genCode(String... tableNames) {
for (String tableName : tableNames) {
genCodeByCustomModelName(tableName, null);
}
}
/**
* 通过数据表名称,和自定义的 Model 名称生成代码
* 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
* @param tableName 数据表名称
* @param modelName 自定义的 Model 名称
*/
public static void genCodeByCustomModelName(String tableName, String modelName) {
genModelAndMapper(tableName, modelName);
// genService(tableName, modelName);
// genController(tableName, modelName);
}
public static void genModelAndMapper(String tableName, String modelName) {
Context context = new Context(ModelType.FLAT);
context.setId("Potato");
context.setTargetRuntime("MyBatis3Simple");
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
context.addPluginConfiguration(pluginConfiguration);
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
sqlMapGeneratorConfiguration.setTargetPackage("Mapping");
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration.setTableName(tableName);
if (StringUtils.isNotEmpty(modelName))tableConfiguration.setDomainObjectName(modelName);
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
context.addTableConfiguration(tableConfiguration);
List<String> warnings;
MyBatisGenerator generator;
try {
Configuration config = new Configuration();
config.addContext(context);
config.validate();
boolean overwrite = true;
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
warnings = new ArrayList<String>();
generator = new MyBatisGenerator(config, callback, warnings);
generator.generate(null);
} catch (Exception e) {
throw new RuntimeException("生成Model和Mapper失败", e);
}
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
throw new RuntimeException("生成Model和Mapper失败:" + warnings);
}
if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName);
System.out.println(modelName + ".java 生成成功");
System.out.println(modelName + "Mapper.java 生成成功");
System.out.println(modelName + "Mapper.xml 生成成功");
}
public static void genService(String tableName, String modelName) {
try {
freemarker.template.Configuration cfg = getConfiguration();
Map<String, Object> data = new HashMap<>();
data.put("date", DATE);
data.put("author", AUTHOR);
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data.put("modelNameUpperCamel", modelNameUpperCamel);
data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
data.put("basePackage", BASE_PACKAGE);
File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
cfg.getTemplate("service.ftl").process(data,
new FileWriter(file));
System.out.println(modelNameUpperCamel + "Service.java 生成成功");
File file1 = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java");
if (!file1.getParentFile().exists()) {
file1.getParentFile().mkdirs();
}
cfg.getTemplate("service-impl.ftl").process(data,
new FileWriter(file1));
System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功");
} catch (Exception e) {
throw new RuntimeException("生成Service失败", e);
}
}
public static void genController(String tableName, String modelName) {
try {
freemarker.template.Configuration cfg = getConfiguration();
Map<String, Object> data = new HashMap<>();
data.put("date", DATE);
data.put("author", AUTHOR);
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
data.put("modelNameUpperCamel", modelNameUpperCamel);
data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
data.put("basePackage", BASE_PACKAGE);
File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
//cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));
System.out.println(modelNameUpperCamel + "Controller.java 生成成功");
} catch (Exception e) {
throw new RuntimeException("生成Controller失败", e);
}
}
private static freemarker.template.Configuration getConfiguration() throws IOException {
freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
return cfg;
}
private static String tableNameConvertLowerCamel(String tableName) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
}
private static String tableNameConvertUpperCamel(String tableName) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());
}
private static String tableNameConvertMappingPath(String tableName) {
tableName = tableName.toLowerCase();//兼容使用大写的表名
return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
}
private static String modelNameConvertMappingPath(String modelName) {
String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
return tableNameConvertMappingPath(tableName);
}
private static String packageConvertPath(String packageName) {
return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
}
}
以下代码部分是配置生成的内容(Model,Mapper,Service,Controller)如下图配置,执行以后只生成Model类和xxxMapper.xml文件
此工具目前博主依旧在用,如有改进意见请君提出!