parent
1907bfdd3f
commit
1b57ed4622
@ -0,0 +1,211 @@ |
|||||||
|
import com.company.project.core.ProjectConstant; |
||||||
|
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 abstract class CodeGenerator { |
||||||
|
//JDBC配置
|
||||||
|
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test"; |
||||||
|
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 BASE_PACKAGE_PATH = "\\com\\company\\project";//项目基础包路径
|
||||||
|
private static final String PACKAGE_PATH_SERVICE = BASE_PACKAGE_PATH + "\\service\\";//生成的Service存放路径
|
||||||
|
private static final String PACKAGE_PATH_SERVICE_IMPL = BASE_PACKAGE_PATH + "\\service\\impl\\";//生成的Service实现存放路径
|
||||||
|
private static final String PACKAGE_PATH_CONTROLLER = BASE_PACKAGE_PATH + "\\web\\";//生成的Controller实现存放路径
|
||||||
|
|
||||||
|
private static final String AUTHOR = "CodeGenerator";//@author
|
||||||
|
private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date
|
||||||
|
|
||||||
|
public static void main(String[] args) { |
||||||
|
genCode("输入表名"); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static void genCode(String... tableNames) { |
||||||
|
for (String tableName : tableNames) { |
||||||
|
//根据需求生成,不需要的注掉,模板有问题的话可以自己修改。
|
||||||
|
genModelAndMapper(tableName); |
||||||
|
genService(tableName); |
||||||
|
genController(tableName); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static void genModelAndMapper(String tableName) { |
||||||
|
try { |
||||||
|
List<String> warnings = new ArrayList<String>(); |
||||||
|
boolean overwrite = true; |
||||||
|
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", ProjectConstant.MAPPER_INTERFACE_REFERENCE); |
||||||
|
context.addPluginConfiguration(pluginConfiguration); |
||||||
|
|
||||||
|
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration(); |
||||||
|
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH); |
||||||
|
javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.MODEL_PACKAGE); |
||||||
|
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration); |
||||||
|
|
||||||
|
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration(); |
||||||
|
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH); |
||||||
|
sqlMapGeneratorConfiguration.setTargetPackage("mapper"); |
||||||
|
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration); |
||||||
|
|
||||||
|
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration(); |
||||||
|
javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH); |
||||||
|
javaClientGeneratorConfiguration.setTargetPackage(ProjectConstant.MAPPER_PACKAGE); |
||||||
|
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER"); |
||||||
|
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration); |
||||||
|
|
||||||
|
TableConfiguration tableConfiguration = new TableConfiguration(context); |
||||||
|
tableConfiguration.setTableName(tableName); |
||||||
|
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null)); |
||||||
|
context.addTableConfiguration(tableConfiguration); |
||||||
|
|
||||||
|
|
||||||
|
DefaultShellCallback callback = new DefaultShellCallback(overwrite); |
||||||
|
Configuration config = new Configuration(); |
||||||
|
config.addContext(context); |
||||||
|
config.validate(); |
||||||
|
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); |
||||||
|
myBatisGenerator.generate(null); |
||||||
|
String modelName = tableNameConvertUpperCamel(tableName); |
||||||
|
System.out.println(modelName + ".java 生成成功"); |
||||||
|
System.out.println(modelName + "Mapper.java 生成成功"); |
||||||
|
System.out.println(modelName + "Mapper.xml 生成成功"); |
||||||
|
} catch (Exception e) { |
||||||
|
throw new RuntimeException("生成Model和Mapper失败", e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void genService(String tableName) { |
||||||
|
try { |
||||||
|
freemarker.template.Configuration cfg = getConfiguration(); |
||||||
|
|
||||||
|
Map<String, Object> data = new HashMap<>(); |
||||||
|
data.put("date", DATE); |
||||||
|
data.put("author", AUTHOR); |
||||||
|
String modelNameUpperCamel = tableNameConvertUpperCamel(tableName); |
||||||
|
data.put("modelNameUpperCamel", modelNameUpperCamel); |
||||||
|
data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName)); |
||||||
|
data.put("basePackage", ProjectConstant.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) { |
||||||
|
try { |
||||||
|
freemarker.template.Configuration cfg = getConfiguration(); |
||||||
|
|
||||||
|
Map<String, Object> data = new HashMap<>(); |
||||||
|
data.put("date", DATE); |
||||||
|
data.put("author", AUTHOR); |
||||||
|
data.put("baseRequestMapping", tableNameConvertMappingPath(tableName)); |
||||||
|
String modelNameUpperCamel = tableNameConvertUpperCamel(tableName); |
||||||
|
data.put("modelNameUpperCamel", modelNameUpperCamel); |
||||||
|
data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName)); |
||||||
|
data.put("basePackage", ProjectConstant.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.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) { |
||||||
|
StringBuilder result = new StringBuilder(); |
||||||
|
if (tableName != null && tableName.length() > 0) { |
||||||
|
boolean flag = false; |
||||||
|
for (int i = 0; i < tableName.length(); i++) { |
||||||
|
char ch = tableName.charAt(i); |
||||||
|
if ("_".charAt(0) == ch) { |
||||||
|
flag = true; |
||||||
|
} else { |
||||||
|
if (flag) { |
||||||
|
result.append(Character.toUpperCase(ch)); |
||||||
|
flag = false; |
||||||
|
} else { |
||||||
|
result.append(ch); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return result.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
private static String tableNameConvertUpperCamel(String tableName) { |
||||||
|
String camel = tableNameConvertLowerCamel(tableName); |
||||||
|
return camel.substring(0, 1).toUpperCase() + camel.substring(1); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private static String tableNameConvertMappingPath(String tableName) { |
||||||
|
return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,21 @@ |
|||||||
|
package com.conpany.project; |
||||||
|
|
||||||
|
|
||||||
|
import com.company.project.Application; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||||
|
import org.springframework.test.annotation.Rollback; |
||||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||||
|
import org.springframework.transaction.annotation.Transactional; |
||||||
|
|
||||||
|
/** |
||||||
|
* 单元测试继承该类即可 |
||||||
|
*/ |
||||||
|
@RunWith(SpringRunner.class) |
||||||
|
@SpringBootTest(classes = Application.class) |
||||||
|
@Transactional |
||||||
|
@Rollback |
||||||
|
public abstract class Tester {} |
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,36 @@ |
|||||||
|
/* |
||||||
|
Navicat MySQL Data Transfer |
||||||
|
|
||||||
|
Source Server : Localhost |
||||||
|
Source Server Version : 50713 |
||||||
|
Source Host : localhost:3306 |
||||||
|
Source Database : test |
||||||
|
|
||||||
|
Target Server Type : MYSQL |
||||||
|
Target Server Version : 50713 |
||||||
|
File Encoding : 65001 |
||||||
|
|
||||||
|
Date: 2017-06-23 14:25:27 |
||||||
|
*/ |
||||||
|
|
||||||
|
SET FOREIGN_KEY_CHECKS=0; |
||||||
|
|
||||||
|
-- ---------------------------- |
||||||
|
-- Table structure for user |
||||||
|
-- ---------------------------- |
||||||
|
DROP TABLE IF EXISTS `user`; |
||||||
|
CREATE TABLE `user` ( |
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
||||||
|
`username` varchar(255) NOT NULL, |
||||||
|
`password` varchar(255) NOT NULL, |
||||||
|
`nick_name` varchar(255) DEFAULT NULL, |
||||||
|
`sex` int(1) DEFAULT NULL, |
||||||
|
`register_date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, |
||||||
|
PRIMARY KEY (`id`) |
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; |
||||||
|
|
||||||
|
-- ---------------------------- |
||||||
|
-- Records of user |
||||||
|
-- ---------------------------- |
||||||
|
INSERT INTO `user` VALUES ('1', '89921218@qq.com', '1ee04e0b1cb5af7367c80c22e42efd8b', '土豆', '1', '2017-06-23 14:24:23'); |
||||||
|
SET FOREIGN_KEY_CHECKS=1; |
@ -0,0 +1,43 @@ |
|||||||
|
package ${basePackage}.web; |
||||||
|
import ${basePackage}.core.Result; |
||||||
|
import ${basePackage}.core.ResultGenerator; |
||||||
|
import ${basePackage}.model.${modelNameUpperCamel}; |
||||||
|
import ${basePackage}.service.${modelNameUpperCamel}Service; |
||||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.RestController; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by ${author} on ${date}. |
||||||
|
*/ |
||||||
|
@RestController |
||||||
|
@RequestMapping("${baseRequestMapping}") |
||||||
|
public class ${modelNameUpperCamel}Controller { |
||||||
|
@Resource |
||||||
|
private ${modelNameUpperCamel}Service ${modelNameLowerCamel}Service; |
||||||
|
|
||||||
|
@PostMapping("/add") |
||||||
|
public Result add(${modelNameUpperCamel} ${modelNameLowerCamel}) { |
||||||
|
${modelNameLowerCamel}Service.save(${modelNameLowerCamel}); |
||||||
|
return ResultGenerator.genSuccessResult(); |
||||||
|
} |
||||||
|
|
||||||
|
@PostMapping("/delete") |
||||||
|
public Result delete(Integer id) { |
||||||
|
${modelNameLowerCamel}Service.deleteById(id); |
||||||
|
return ResultGenerator.genSuccessResult(); |
||||||
|
} |
||||||
|
|
||||||
|
@PostMapping("/update") |
||||||
|
public Result update(${modelNameUpperCamel} ${modelNameLowerCamel}) { |
||||||
|
${modelNameLowerCamel}Service.update(${modelNameLowerCamel}); |
||||||
|
return ResultGenerator.genSuccessResult(); |
||||||
|
} |
||||||
|
@PostMapping("/detail") |
||||||
|
public Result detail(Integer id) { |
||||||
|
${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id); |
||||||
|
return ResultGenerator.genSuccessResult(${modelNameLowerCamel}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
package ${basePackage}.service.impl; |
||||||
|
|
||||||
|
import ${basePackage}.dao.${modelNameUpperCamel}Mapper; |
||||||
|
import ${basePackage}.model.${modelNameUpperCamel}; |
||||||
|
import ${basePackage}.service.${modelNameUpperCamel}Service; |
||||||
|
import ${basePackage}.core.AbstractService; |
||||||
|
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 AbstractService<${modelNameUpperCamel}> implements ${modelNameUpperCamel}Service { |
||||||
|
@Resource |
||||||
|
private ${modelNameUpperCamel}Mapper ${modelNameLowerCamel}Mapper; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
package ${basePackage}.service; |
||||||
|
import ${basePackage}.model.${modelNameUpperCamel}; |
||||||
|
import ${basePackage}.core.Service; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Created by ${author} on ${date}. |
||||||
|
*/ |
||||||
|
public interface ${modelNameUpperCamel}Service extends Service<${modelNameUpperCamel}> { |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue