Просмотр исходного кода

支持生成通用service、serviceMock和serviceImpl

shuzheng 9 лет назад
Родитель
Сommit
8a5efdee31

+ 1 - 1
zheng-cms/zheng-cms-dao/src/main/java/com/zheng/cms/dao/Generator.java

@@ -23,7 +23,7 @@ public class Generator {
 	 * 自动代码生成
 	 * 自动代码生成
 	 * @param args
 	 * @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);
 		MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE, DATABASE, TABLE_PREFIX, PACKAGE_NAME);
 	}
 	}
 
 

+ 60 - 29
zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java

@@ -8,10 +8,8 @@ import org.mybatis.generator.config.xml.ConfigurationParser;
 import org.mybatis.generator.internal.DefaultShellCallback;
 import org.mybatis.generator.internal.DefaultShellCallback;
 
 
 import java.io.File;
 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;
 import static com.zheng.common.util.StringUtil.lineToHump;
 
 
@@ -21,8 +19,14 @@ import static com.zheng.common.util.StringUtil.lineToHump;
  */
  */
 public class MybatisGeneratorUtil {
 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文件
 	 * 根据模板生成generatorConfig.xml文件
@@ -43,7 +47,7 @@ public class MybatisGeneratorUtil {
 			String module,
 			String module,
 			String database,
 			String database,
 			String table_prefix,
 			String table_prefix,
-			String package_name) {
+			String package_name) throws Exception{
 
 
 		String targetProject = module + "/" + module + "-dao";
 		String targetProject = module + "/" + module + "-dao";
 		String module_path = module + "/" + module + "-dao/src/main/resources/generatorConfig.xml";
 		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", targetProject);
 			context.put("targetProject_sqlMap", targetProject_sqlMap);
 			context.put("targetProject_sqlMap", targetProject_sqlMap);
 			context.put("generator_jdbc_password", AESUtil.AESDecode(jdbc_password));
 			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/model"));
 			deleteDir(new File(targetProject + "/src/main/java/" + package_name.replaceAll("\\.", "/") + "/dao/mapper"));
 			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("========== 结束生成generatorConfig.xml文件 ==========");
 
 
 		System.out.println("========== 开始运行MybatisGenerator ==========");
 		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 ==========");
 		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++) {
 		for (int i = 0; i < tables.size(); i++) {
 			String model = StringUtil.lineToHump(ObjectUtils.toString(tables.get(i).get("table_name")));
 			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("========== 结束生成Service ==========");
 
 
 		System.out.println("========== 开始生成Controller ==========");
 		System.out.println("========== 开始生成Controller ==========");

+ 5 - 5
zheng-common/src/main/resources/template/Service.vm

@@ -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}.
 * Created by shuzheng on ${ctime}.
 */
 */
 public interface ${model}Service extends BaseService<${model}, ${model}Example> {
 public interface ${model}Service extends BaseService<${model}, ${model}Example> {

+ 14 - 8
zheng-common/src/main/resources/template/ServiceImpl.vm

@@ -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.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 
 
 /**
 /**
-* ${modelname}service实现
+* ${model}Service实现
 * Created by shuzheng on ${ctime}.
 * Created by shuzheng on ${ctime}.
 */
 */
 @Service
 @Service
 @Transactional
 @Transactional
+@BaseService
 public class ${model}ServiceImpl extends BaseServiceImpl<${model}Mapper, ${model}, ${model}Example> implements ${model}Service {
 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;
 
 
 }
 }

+ 7 - 14
zheng-common/src/main/resources/template/ServiceMock.vm

@@ -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}.
 * Created by shuzheng on ${ctime}.
 */
 */
-@Service
-@Transactional
 public class ${model}ServiceMock extends BaseServiceMock<${model}Mapper, ${model}, ${model}Example> implements ${model}Service {
 public class ${model}ServiceMock extends BaseServiceMock<${model}Mapper, ${model}, ${model}Example> implements ${model}Service {
 
 
-}
+}

+ 1 - 1
zheng-upms/zheng-upms-dao/src/main/java/com/zheng/upms/dao/Generator.java

@@ -23,7 +23,7 @@ public class Generator {
 	 * 自动代码生成
 	 * 自动代码生成
 	 * @param args
 	 * @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);
 		MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE, DATABASE, TABLE_PREFIX, PACKAGE_NAME);
 	}
 	}