支持生成通用service、serviceMock和serviceImpl

This commit is contained in:
shuzheng 2017-03-20 23:32:14 +08:00
parent 1b3e01f325
commit 8a5efdee31
6 changed files with 88 additions and 58 deletions

View File

@ -23,7 +23,7 @@ public class Generator {
* 自动代码生成
* @param args
*/
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE, DATABASE, TABLE_PREFIX, PACKAGE_NAME);
}

View File

@ -8,10 +8,8 @@ import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.zheng.common.util.StringUtil.lineToHump;
@ -21,8 +19,14 @@ import static com.zheng.common.util.StringUtil.lineToHump;
*/
public class MybatisGeneratorUtil {
// 模板路径
private static String VM_PATH = "zheng-common/src/main/resources/template/generatorConfig.vm";
// generatorConfig模板路径
private static String generatorConfig_vm = "zheng-common/src/main/resources/template/generatorConfig.vm";
// Service模板路径
private static String service_vm = "zheng-common/src/main/resources/template/Service.vm";
// ServiceMock模板路径
private static String serviceMock_vm = "zheng-common/src/main/resources/template/ServiceMock.vm";
// ServiceImpl模板路径
private static String serviceImpl_vm = "zheng-common/src/main/resources/template/ServiceImpl.vm";
/**
* 根据模板生成generatorConfig.xml文件
@ -43,7 +47,7 @@ public class MybatisGeneratorUtil {
String module,
String database,
String table_prefix,
String package_name) {
String package_name) throws Exception{
String targetProject = module + "/" + module + "-dao";
String module_path = module + "/" + module + "-dao/src/main/resources/generatorConfig.xml";
@ -75,7 +79,7 @@ public class MybatisGeneratorUtil {
context.put("targetProject", targetProject);
context.put("targetProject_sqlMap", targetProject_sqlMap);
context.put("generator_jdbc_password", AESUtil.AESDecode(jdbc_password));
VelocityUtil.generate(VM_PATH, module_path, context);
VelocityUtil.generate(generatorConfig_vm, module_path, context);
// 删除旧代码
deleteDir(new File(targetProject + "/src/main/java/" + package_name.replaceAll("\\.", "/") + "/dao/model"));
deleteDir(new File(targetProject + "/src/main/java/" + package_name.replaceAll("\\.", "/") + "/dao/mapper"));
@ -86,32 +90,59 @@ public class MybatisGeneratorUtil {
System.out.println("========== 结束生成generatorConfig.xml文件 ==========");
System.out.println("========== 开始运行MybatisGenerator ==========");
try {
List<String> warnings = new ArrayList<>();
File configFile = new File(module_path);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
for (String warning : warnings) {
System.out.println(warning);
}
} catch (Exception e) {
e.printStackTrace();
List<String> warnings = new ArrayList<>();
File configFile = new File(module_path);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
for (String warning : warnings) {
System.out.println(warning);
}
System.out.println("========== 结束运行MybatisGenerator ==========");
String servicePath = module + "/" + module + "-rpc-api" + "/src/main/java/" + package_name.replaceAll(".", "/") + "/rpc/api";
String serviceImplPath = module + "/" + module + "-rpc-service" + "/src/main/java/" + package_name.replaceAll(".", "/") + "/rpc/service/impl";
System.out.println("========== 开始生成Service ==========");
String ctime = new SimpleDateFormat("yyyy/M/d").format(new Date());
String servicePath = module + "/" + module + "-rpc-api" + "/src/main/java/" + package_name.replaceAll("\\.", "/") + "/rpc/api";
String serviceImplPath = module + "/" + module + "-rpc-service" + "/src/main/java/" + package_name.replaceAll("\\.", "/") + "/rpc/service/impl";
for (int i = 0; i < tables.size(); i++) {
String model = StringUtil.lineToHump(ObjectUtils.toString(tables.get(i).get("table_name")));
String service = model + "Service";
String serviceMock = model + "ServiceMock";
String serviceImpl = model + "ServiceImpl";
System.out.println(service + " " + serviceMock + " " + serviceImpl);
String service = servicePath + "/" + model + "Service.java";
String serviceMock = servicePath + "/" + model + "ServiceMock.java";
String serviceImpl = serviceImplPath + "/" + model + "ServiceImpl.java";
// 生成service
File serviceFile = new File(service);
if (!serviceFile.exists()) {
VelocityContext context = new VelocityContext();
context.put("package_name", package_name);
context.put("model", model);
context.put("ctime", ctime);
VelocityUtil.generate(service_vm, service, context);
System.out.println(service);
}
// 生成serviceMock
File serviceMockFile = new File(serviceMock);
if (!serviceMockFile.exists()) {
VelocityContext context = new VelocityContext();
context.put("package_name", package_name);
context.put("model", model);
context.put("ctime", ctime);
VelocityUtil.generate(serviceMock_vm, serviceMock, context);
System.out.println(serviceMock);
}
// 生成serviceImpl
File serviceImplFile = new File(serviceImpl);
if (!serviceImplFile.exists()) {
VelocityContext context = new VelocityContext();
context.put("package_name", package_name);
context.put("model", model);
context.put("mapper", StringUtil.toLowerCaseFirstOne(model));
context.put("ctime", ctime);
VelocityUtil.generate(serviceImpl_vm, serviceImpl, context);
System.out.println(serviceImpl);
}
}
System.out.println("========== 开始生成Service ==========");
System.out.println("========== 结束生成Service ==========");
System.out.println("========== 开始生成Controller ==========");

View File

@ -1,11 +1,11 @@
package ${groupId}.${module}.rpc.api;
package ${package_name}.rpc.api;
import ${groupId}.${module}.dao.model.${model};
import ${groupId}.${module}.dao.model.${model}Example;
import ${groupId}.common.base.BaseService;
import com.zheng.common.base.BaseService;
import ${package_name}.dao.model.${model};
import ${package_name}.dao.model.${model}Example;
/**
* ${modelname}service接口
* ${model}Service接口
* Created by shuzheng on ${ctime}.
*/
public interface ${model}Service extends BaseService<${model}, ${model}Example> {

View File

@ -1,23 +1,29 @@
package ${groupId}.${module}.rpc.service.impl;
package com.zheng.upms.rpc.service.impl;
import ${groupId}.${module}.dao.mapper.${model}Mapper;
import ${groupId}.${module}.dao.model.${model};
import ${groupId}.${module}.dao.model.${model}Example;
import ${groupId}.${module}.rpc.api.${model}Service;
import ${groupId}.common.base.BaseServiceImpl;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import ${package_name}.dao.mapper.${model}Mapper;
import ${package_name}.dao.model.${model};
import ${package_name}.dao.model.${model}Example;
import ${package_name}.rpc.api.${model}Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* ${modelname}service实现
* ${model}Service实现
* Created by shuzheng on ${ctime}.
*/
@Service
@Transactional
@BaseService
public class ${model}ServiceImpl extends BaseServiceImpl<${model}Mapper, ${model}, ${model}Example> implements ${model}Service {
private static Logger _log = LoggerFactory.getLogger(${model}ServiceImpl.class);
private static Logger _log = LoggerFactory.getLogger(${model}ServiceImpl.class);
@Autowired
${model}Mapper ${mapper}Mapper;
}

View File

@ -1,21 +1,14 @@
package ${groupId}.${module}.rpc.service.impl;
package ${package_name}.rpc.api;
import ${groupId}.${module}.dao.mapper.${model}Mapper;
import ${groupId}.${module}.dao.model.${model};
import ${groupId}.${module}.dao.model.${model}Example;
import ${groupId}.${module}.rpc.api.${model}Service;
import ${groupId}.common.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zheng.common.base.BaseServiceMock;
import ${package_name}.dao.mapper.${model}Mapper;
import ${package_name}.dao.model.${model};
import ${package_name}.dao.model.${model}Example;
/**
* 降级实现${modelname}Service
* 降级实现${model}Service接口
* Created by shuzheng on ${ctime}.
*/
@Service
@Transactional
public class ${model}ServiceMock extends BaseServiceMock<${model}Mapper, ${model}, ${model}Example> implements ${model}Service {
}
}

View File

@ -23,7 +23,7 @@ public class Generator {
* 自动代码生成
* @param args
*/
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE, DATABASE, TABLE_PREFIX, PACKAGE_NAME);
}