逆向工程这个东西就是为了减少程序员的工作量,说白了就是偷懒的,下面原代码可以直接复制粘贴使用
直接开始
主要的类是CodeGenerator.java
其次是core里面的一些常数
最后是test/java/resources/generator/template里面的模板
其中me.springboot.mybatis.core.WebResponse是响应类,这个可以直接复制到项目中使用
生成的类在对应的controller、dao、pojo、service、resources/mapper,各个文件夹里面
没必要读代码,只是个工具
只需要在CodeGenerator.java里面配置好数据源
在main方法中写上需要生成的表名即可
整体目录
pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.springboot-mybatis</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- Inherit defaults from Spring Boot -->
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--Spring Boot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
<!--MySQL JDBC驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
<!--代码生成器依赖 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>aliyun-repos</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
CodeGenerator类
package me.springboot.mybatis;
import com.google.common.base.CaseFormat;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.CommentGenerator;
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.*;
import static me.springboot.mybatis.core.ProjectConstant.*;
/**
* @ClassName: CodeGenerator
* @Description: 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
* @author mjz
* @date 2017年8月10日 下午4:22:55
*
*/
public class CodeGenerator {
// JDBC配置,请修改为你项目的实际配置
private static final String JDBC_URL = "jdbc:mysql://192.168.1.48:3308/model_base";
private static final String JDBC_USERNAME = "root";
private static final String JDBC_PASSWORD = "clear!@#123";
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 = "mjz";// @author
private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());// @date
public static void main(String[] args) {
//可以只输入表名
genCode("feature");
//也可以自定义类名
// genCode("输入表名","输入自定义Model名称");
}
/**
* 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。 如输入表名称 "t_user_detail" 将生成
* TUserDetail、TUserDetailMapper、TUserDetailService ...
*
* @param tableNames
* 数据表名称...
*/
public static void genCode(String... tableNames) {
for (String tableName : tableNames) {
genCode(tableName, null);
}
}
/**
* 通过数据表名称,和自定义的 Model 名称生成代码 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User"
* 将生成 User、UserMapper、UserService ...
*
* @param tableName
* 数据表名称
* @param modelName
* 自定义的 Model 名称
*/
public static void genCode(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);
//实体类
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
//生成xml文件
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
sqlMapGeneratorConfiguration.setTargetPackage("mapper");
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
//生成mapper
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);
tableConfiguration.setDomainObjectName(modelName);
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
context.addTableConfiguration(tableConfiguration);
//去除自动生成的注释
Properties properties = new Properties();
properties.setProperty("suppressAllComments","true");
CommentGenerator commentGenerator = context.getCommentGenerator();
commentGenerator.addConfigurationProperties(properties);
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", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
data.put("basePackage", BASE_PACKAGE);
data.put("basePackageService", SERVICE_PACKAGE);
data.put("basePackageServiceImpl", SERVICE_IMPL_PACKAGE);
data.put("basePackageModel", MODEL_PACKAGE);
data.put("basePackageDao", MAPPER_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);
data.put("basePackageController", CONTROLLER_PACKAGE);
data.put("basePackageService", SERVICE_PACKAGE);
data.put("basePackageModel", MODEL_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.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);
}
}
ProjectConstant类(常数)
package me.springboot.mybatis.core;
/**
* @ClassName: ProjectConstant
* @Description: TODO
* @author mjz
* @date 2017年8月10日 下午4:07:14
*
*/
public class ProjectConstant {
// 项目基础包名称,根据自己公司的项目修改
public static final String BASE_PACKAGE = "me.springboot.mybatis";
// Model所在包
public static final String MODEL_PACKAGE = BASE_PACKAGE + ".pojo";
// Mapper所在包
public static final String MAPPER_PACKAGE = BASE_PACKAGE + ".dao";
// Service所在包
public static final String SERVICE_PACKAGE = BASE_PACKAGE + ".service";
// ServiceImpl所在包
public static final String SERVICE_IMPL_PACKAGE = SERVICE_PACKAGE + ".impl";
// Controller所在包
public static final String CONTROLLER_PACKAGE = BASE_PACKAGE + ".controller";
// Mapper插件基础接口的完全限定名
public static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".core.Mapper";
}
WebResponse类(响应)
package me.springboot.mybatis.core;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* @Author mjz
* @Date 2022/3/23 14:30
* @Version 1.0
*/
public class WebResponse {
/**
* 成功
*/
public static final int CODE_SUCCESS=0;
/**
* 失败
*/
public static final int CODE_ERROE=-1;
/**
* 警告
*/
public static final int WARN_ERROE=-2;
private int code;
private String msg="success";
private Object data;
public WebResponse(){ code= CODE_SUCCESS; }
public void setThrowable(Throwable t){
code=-1;
msg=t.getMessage();
}
public static WebResponse success(){return success(null);}
/**
* 带参数返回成功
* @param data
* @return
*/
public static WebResponse success(Object data){
WebResponse webResponse = new WebResponse();
webResponse.setCode(CODE_SUCCESS);
webResponse.setData(data);
return webResponse;
}
/**
* 失败返回
* @param code
* @param msg
* @return
*/
public static WebResponse error(int code, String msg){
WebResponse webResponse = new WebResponse();
webResponse.setCode(code);
webResponse.setMsg(msg);
return webResponse;
}
@JsonIgnore
public boolean isSuccess(){return this.code==CODE_SUCCESS;}
@JsonIgnore
public boolean isError(){return this.code==CODE_ERROE;}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
controller.ftl(controller模板)
package ${basePackageController};
import ${basePackage}.core.WebResponse;
import ${basePackageModel}.${modelNameUpperCamel};
import ${basePackageService}.${modelNameUpperCamel}Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @ClassName: ${modelNameUpperCamel}Controller
* @Description: TODO
* @author ${author}
* @date ${date}
*/
@RestController
public class ${modelNameUpperCamel}Controller {
@Resource
private ${modelNameUpperCamel}Service ${modelNameLowerCamel}Service;
/**
* @Title: add
* @Description: 添加
* @param ${modelNameLowerCamel}
* @Reutrn WebResponse
*/
@PostMapping("/add")
public WebResponse add(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
${modelNameLowerCamel}Service.save(${modelNameLowerCamel});
return WebResponse.success();
}
/**
* @Title: delete
* @Description: 删除
* @param id
* @Reutrn WebResponse<Object>
*/
@PostMapping("/delete")
public WebResponse delete(@RequestParam Integer id) {
${modelNameLowerCamel}Service.deleteById(id);
return WebResponse.success();
}
/**
* @Title: update
* @Description: 更新
* @param ${modelNameLowerCamel}
* @Reutrn WebResponse
*/
@PostMapping("/update")
public WebResponse update(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
return WebResponse.success();
}
/**
* @Title: detail
* @Description: 根据ID查询详情
* @param id
* @Reutrn WebResponse
*/
@PostMapping("/detail")
public WebResponse detail(@RequestParam Integer id) {
${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id);
return WebResponse.success(${modelNameLowerCamel});
}
/**
* @Title: list
* @Description: 分页查询
* @param page 页码
* @param size 每页条数
* @Reutrn WebResponse
*/
@PostMapping("/list")
public WebResponse list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
PageHelper.startPage(page, size);
List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll();
PageInfo<${modelNameUpperCamel}> pageInfo = new PageInfo<${modelNameUpperCamel}>(list);
return WebResponse.success(pageInfo);
}
}
service.ftl(service模板)
package ${basePackageService};
import ${basePackageModel}.${modelNameUpperCamel};
<#--import ${basePackage}.core.Service;-->
import java.util.List;
/**
* @ClassName: ${modelNameUpperCamel}Service
* @Description: TODO
* @author ${author}
* @date ${date}
*/
public interface ${modelNameUpperCamel}Service {
/**
* @Title: add
* @Description: 添加
* @param ${modelNameLowerCamel}
*/
public void save(${modelNameUpperCamel} ${modelNameLowerCamel});
/**
* @Title: delete
* @Description: 删除
* @param id
*/
public void deleteById(Integer id);
/**
* @Title: update
* @Description: 更新
* @param ${modelNameLowerCamel}
*/
public void update(${modelNameUpperCamel} ${modelNameLowerCamel});
/**
* @Title: detail
* @Description: 根据ID查询详情
* @param id
* @Reutrn ${modelNameUpperCamel}
*/
public ${modelNameUpperCamel} findById(Integer id);
/**
* @Title: list
* @Description: 分页查询
* @Reutrn List<${modelNameUpperCamel}>
*/
public List<${modelNameUpperCamel}> findAll();
}
service-impl.ftl(service实现类模板)
package ${basePackageServiceImpl};
import ${basePackageDao}.${modelNameUpperCamel}Mapper;
import ${basePackageModel}.${modelNameUpperCamel};
import ${basePackageService}.${modelNameUpperCamel}Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import javax.annotation.Resource;
/**
* @ClassName: ${modelNameUpperCamel}Service
* @Description: TODO
* @author ${author}
* @date ${date}
*/
@Service
@Transactional
public class ${modelNameUpperCamel}ServiceImpl implements ${modelNameUpperCamel}Service {
@Resource
private ${modelNameUpperCamel}Mapper ${modelNameLowerCamel}Mapper;
/**
* @Title: add
* @Description: 添加
* @param ${modelNameLowerCamel}
*/
@Override
public void save(${modelNameUpperCamel} ${modelNameLowerCamel}){
${modelNameLowerCamel}Mapper.insert(${modelNameLowerCamel});
}
/**
* @Title: delete
* @Description: 删除
* @param id
*/
@Override
public void deleteById(Integer id){
${modelNameLowerCamel}Mapper.deleteByPrimaryKey(id);
}
/**
* @Title: update
* @Description: 更新
* @param ${modelNameLowerCamel}
*/
@Override
public void update(${modelNameUpperCamel} ${modelNameLowerCamel}){
${modelNameLowerCamel}Mapper.updateByPrimaryKey(${modelNameLowerCamel});
}
/**
* @Title: detail
* @Description: 根据ID查询详情
* @param id
* @Reutrn ${modelNameUpperCamel}
*/
@Override
public ${modelNameUpperCamel} findById(Integer id){
return ${modelNameLowerCamel}Mapper.selectByPrimaryKey(id);
}
/**
* @Title: list
* @Description: 分页查询
* @Reutrn List<${modelNameUpperCamel}>
*/
public List<${modelNameUpperCamel}> findAll(){
return ${modelNameLowerCamel}Mapper.selectAll();
}
}