注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

自写的简易生成代码工具  

2010-06-25 11:05:12|  分类: 项目开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

搜索相机

生成代码的主要工具类:GenCode

/**
 *
 */
package test;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.net163.framework.util.Function;

/**
 * @author wanggang
 *
 */
public class GenCode {

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
  List<String> modelNameList = new ArrayList<String>();
  String baseDir = "E:/program/newWorkspace/mytest/src/";
  for(File f:ReadWriteFile.readDir("E:/program/newWorkspace/mytest/src/com/net163/mytest/model")){
   String fileName = f.getName();
   
   if(fileName.endsWith(".java")){
    String capModel = fileName.substring(0, fileName.indexOf("."));
    
    
    
    
    String model = capLittleWord(capModel);
    writeDTO(baseDir+"com/net163/mytest/dto/","com.net163.mytest.dto",model,"com.net163.mytest.model."+capModel,Function.getDateTimeString(new Date()));
    System.out.println(model);
    writeService(baseDir+"com/net163/mytest/front/service/","com.net163.mytest.front.service",model,"com.net163.mytest.front.dao",Function.getDateTimeString(new Date()));
    writeDAO(baseDir+"com/net163/mytest/front/dao/","com.net163.mytest.front.dao","com.net163.mytest.model",model,Function.getDateTimeString(new Date()));
    
    writeService(baseDir+"com/net163/mytest/mgr/service/","com.net163.mytest.mgr.service",model,"com.net163.mytest.mgr.dao",Function.getDateTimeString(new Date()));
    writeDAO(baseDir+"com/net163/mytest/mgr/dao/","com.net163.mytest.mgr.dao","com.net163.mytest.model",model,Function.getDateTimeString(new Date()));
    
//    writeService(baseDir+"com/net163/mytest/service/","com.net163.mytest.service",model,"com.net163.mytest.dao",Function.getDateTimeString(new Date()));
    //writeDAO(baseDir+"com/net163/mytest/dao/","com.net163.mytest.dao","com.net163.mytest.model",model,Function.getDateTimeString(new Date()));
    modelNameList.add(model);
    
   }
//   /serviceTl.replace("", fileName);
  }
//  writeDeclarativeServices(baseDir+"../WebRoot/WEB-INF/context/","com.net163.mytest.dao","com.net163.mytest.service",modelNameList);
  writeDeclarativeServices(baseDir+"../WebRoot/WEB-INF/context/","com.net163.mytest.front.dao","com.net163.mytest.front.service",modelNameList);
  writeDeclarativeServices(baseDir+"../WebRoot/WEB-INF/context/","com.net163.mytest.mgr.dao","com.net163.mytest.mgr.service",modelNameList);
  
 }
 
 public static void writeDTO(String fileDir,String packageName,String model,String capModelFullName,String version) throws Exception{
  Class dtoClass = Class.forName(capModelFullName);
  Field[] fields =dtoClass.getDeclaredFields();
  
  StringBuilder methodTL = new StringBuilder();
  methodTL.append("\tpublic @type@ get@capfield@() {\n");
  methodTL.append("\t return this.@field@;\n");
  methodTL.append("\t}\n");
  methodTL.append("\t\n");
  methodTL.append("\tpublic void set@capfield@(@type@ @field@) {\n");
  methodTL.append("\t this.@field@ = @field@;\n");
  methodTL.append("\t}\n");
  String methodTemp = null;
  
  String fieldNameTL = "\tprivate @type@ @field@;";  
  String fieldTemp = null;
  
  StringBuilder filedStr = new StringBuilder();
  StringBuilder methodStr = new StringBuilder();
  for(Field field:fields){
   System.out.println(field.getType().getName() + ":"+field.getName());
   String type = field.getType().getName();
   String fieldName = field.getName();
   
   fieldTemp = fieldNameTL.replace("@type@", type);
   fieldTemp = fieldTemp.replace("@field@", fieldName);
   filedStr.append(fieldTemp).append("\n");
   //method
   methodTemp = methodTL.toString().replace("@type@", type);
   methodTemp = methodTemp.replace("@field@", fieldName);
   methodTemp = methodTemp.replace("@capfield@", capWord(fieldName));
   methodStr.append(methodTemp).append("\n");
  }
  
  String dtoTlDir = "E:/program/newWorkspace/mytest/src/test/dto.tl";
  String dtoTl = ReadWriteFile.readFile(dtoTlDir);
  dtoTl = dtoTl.replace("@fields@", filedStr.toString());
  dtoTl = dtoTl.replace("@methods@", methodStr.toString());
  
  String capmodel = capWord(model);
  dtoTl = dtoTl.replace("@capmodel@", capmodel);
  dtoTl = dtoTl.replace("@package@", packageName);
  dtoTl = dtoTl.replace("@version@", version);
  ReadWriteFile.writeFile(fileDir+capmodel+"DTO.java", dtoTl);
 }
 
 public static void writeService(String fileDir,String packageName,String model,String packageDAO,String version) throws Exception{
  String serviceTlDir = "E:/program/newWorkspace/mytest/src/test/service.tl";
  String serviceTl = ReadWriteFile.readFile(serviceTlDir);
  String temp = serviceTl.replace("@model@", model);
  String capmodel = capWord(model);//model.substring(0, 1).toUpperCase()+model.substring(1);
  temp = temp.replace("@capmodel@", capmodel);
  temp = temp.replace("@package@", packageName);
  temp = temp.replace("@version@", version);
  ReadWriteFile.writeFile(fileDir+capmodel+"Service.java", temp);
  System.out.println(temp);
  //impl
  
  String serviceImplTlDir = "E:/program/newWorkspace/mytest/src/test/serviceImpl.tl";
  String serviceImplTl = ReadWriteFile.readFile(serviceImplTlDir);
  String temp2 = serviceImplTl.replace("@model@", model);  
  temp2 = temp2.replace("@capmodel@", capmodel);
  temp2 = temp2.replace("@package@", packageName);
  temp2 = temp2.replace("@packageImpl@", packageName+".impl");
  temp2 = temp2.replace("@version@", version);
  temp2 = temp2.replace("@packageDAO@", packageDAO);
  ReadWriteFile.writeFile(fileDir+"impl/"+capmodel+"ServiceImpl.java", temp2);
  System.out.println("here:"+capmodel+" "+temp2);
 }
 public static String capWord(String str){
  String temp = str.substring(0, 1).toUpperCase()+str.substring(1);
  return temp;
 }
 public static String capLittleWord(String str){
  String capmodel = str.substring(0, 1).toLowerCase()+str.substring(1);
  return capmodel;
 }
 public static void writeDAO(String fileDir,String packageName,String packageModel,String model,String version) throws Exception{
  String serviceTlDir = "E:/program/newWorkspace/mytest/src/test/dao.tl";
  String serviceTl = ReadWriteFile.readFile(serviceTlDir);
  String temp = serviceTl.replace("@model@", model);
  String capmodel = capWord(model);;
  temp = temp.replace("@capmodel@", capmodel);
  temp = temp.replace("@package@", packageName);
  temp = temp.replace("@version@", version);
  temp = temp.replace("@packageModel@", packageModel);
  ReadWriteFile.writeFile(fileDir+capmodel+"DAO.java", temp);
  System.out.println(temp);
  //impl
  
  String serviceImplTlDir = "E:/program/newWorkspace/mytest/src/test/daoImpl.tl";
  String serviceImplTl = ReadWriteFile.readFile(serviceImplTlDir);
  String temp2 = serviceImplTl.replace("@model@", model);  
  temp2 = temp2.replace("@capmodel@", capmodel);
  temp2 = temp2.replace("@package@", packageName);
  temp2 = temp2.replace("@packageImpl@", packageName+".impl");
  temp2 = temp2.replace("@version@", version);
  temp2 = temp2.replace("@packageModel@", packageModel);
  ReadWriteFile.writeFile(fileDir+"impl/"+capmodel+"DAOImpl.java", temp2);
  System.out.println(temp2);
 }
 public static void writeDeclarativeServices(String fileDir,String packageDao,String packageService,List<String> modelNameList) throws Exception{
  StringBuilder sb = new StringBuilder();
  sb.append("\t<bean id=\"@model@DAO\" class=\"@packageDao@.impl.@capmodel@DAOImpl\">");
  sb.append("</bean>");
  sb.append("\n");
  sb.append("\t<bean id=\"@model@Service\" class=\"@packageService@.impl.@capmodel@ServiceImpl\">");
  sb.append("</bean>");
  String temple = sb.toString();
  String temp = null;
  StringBuilder result = new StringBuilder();
  for(String model :modelNameList){
   temp = temple.replace("@model@", model);
   temp = temp.replace("@capmodel@", capWord(model));
   temp = temp.replace("@packageDao@", packageDao);
   temp = temp.replace("@packageService@", packageService);
   
   result.append(temp).append("\n");
  }
  
  String declarativeServicesTlDir = "E:/program/newWorkspace/mytest/src/test/declarativeServices_xml.tl";
  String declarativeServicesTl = ReadWriteFile.readFile(declarativeServicesTlDir);
  
  declarativeServicesTl = declarativeServicesTl.replace("@declarativeServices@", result.toString());
  ReadWriteFile.writeFile(fileDir+"declarativeServices.xml", declarativeServicesTl);
 }

}

读写工具类:

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;

public class ReadWriteFile {
 
 public static File[]  readDir(String dirname)throws IOException{
  File dir = new File(dirname);
  return dir.listFiles();
 }
 public static String readFile(String filename) throws IOException{
  File f = new File(filename);
  if(!f.exists()){
   File dir = f.getParentFile();
   if(!dir.exists()){
    dir.mkdirs();
   }
  }
  f.createNewFile();
  FileInputStream fis = new FileInputStream(filename);
  BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  StringBuilder sb = new StringBuilder();
  for(String text = br.readLine();text != null;text = br.readLine()){
   sb.append(text);
   sb.append("\n");
  }
  br.close();
  fis.close();
  return sb.toString();
 }
 
 public static void writeFile(String filename,String result) throws IOException{
  File f = new File(filename);
  if(!f.exists()){
   File dir = f.getParentFile();
   if(!dir.exists()){
    dir.mkdirs();
   }
  }
  if(result == null) return;
  FileOutputStream fos = new FileOutputStream(filename);
  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
  bw.write(result);
  bw.close();
  fos.close();
 }
 
 public static void deleteFile(String filename){
  File f = new File(filename);
  if(f.exists() && f.isFile())
   f.delete();
 }
}

 

 


//模板

daoImpl.tl 文件

////////////////////////

package @package@.impl;

import @packageModel@.@capmodel@;
import @package@.@capmodel@DAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.net263.framework.dao.impl.BaseDAOImpl;
/**
 * @author wanggang
 * @version @version@
 */
public class @capmodel@DAOImpl extends BaseDAOImpl<@capmodel@> implements @capmodel@DAO {
 private static final Log log = LogFactory.getLog(@capmodel@DAOImpl.class);

 
}

------

declarativeServices_xml.tl 文件

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
 default-autowire="byName">
 
 <!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 -->
 <!-- 该类实现PlatformTransactionManager接口,是针对Hibernate的特定实现-->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <!-- 配置HibernateTransactionManager时需要依注入SessionFactory的引用 -->
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <!-- 配置事务切面Bean,指定事务管理器 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <!-- 用于配置详细的事务语义 -->
  <tx:attributes>
   <tx:method name="create*" propagation="REQUIRED" />
   <tx:method name="save*" propagation="REQUIRED" />
   <tx:method name="update*" propagation="REQUIRED" />
   <tx:method name="delete*" propagation="REQUIRED" />
   <tx:method name="confirm*" propagation="REQUIRED" />
   <tx:method name="refuse*" propagation="REQUIRED" />
   <tx:method name="approval*" propagation="REQUIRED" />
   <tx:method name="submit*" propagation="REQUIRED" />
   <tx:method name="hang*" propagation="REQUIRED" />
   <tx:method name="hide*" propagation="REQUIRED" />
   <tx:method name="resume*" propagation="REQUIRED" />
   <tx:method name="reject*" propagation="REQUIRED" />
   <tx:method name="stop*" propagation="REQUIRED" />
   <tx:method name="finished*" propagation="REQUIRED" />
   <tx:method name="valid*" propagation="REQUIRED" />
   <tx:method name="modify*" propagation="REQUIRED" />
   <tx:method name="frozen*" propagation="REQUIRED" />
   <tx:method name="all*" propagation="REQUIRED" />
   <tx:method name="copy*" propagation="REQUIRED" />
   <tx:method name="drop*" propagation="REQUIRED" />
   <tx:method name="assign*" propagation="REQUIRED" />
   <tx:method name="*" propagation="SUPPORTS" read-only="true" />
   <tx:method name="create*" rollback-for="Exception" />
   <tx:method name="update*" rollback-for="Exception" />
   <tx:method name="delete*" rollback-for="Exception" />
   <tx:method name="confirm*" rollback-for="Exception" />
   <tx:method name="refuse*" rollback-for="Exception" />
   <tx:method name="approval*" rollback-for="Exception" />
   <tx:method name="submit*" rollback-for="Exception" />
   <tx:method name="hang*" rollback-for="Exception" />
   <tx:method name="hide*" rollback-for="Exception" />
   <tx:method name="resume*" rollback-for="Exception" />
   <tx:method name="reject*" rollback-for="Exception" />
   <tx:method name="stop*" rollback-for="Exception" />
   <tx:method name="finished*" rollback-for="Exception" />
   <tx:method name="valid*" rollback-for="Exception" />
   <tx:method name="modify*" rollback-for="Exception" />
   <tx:method name="frozen*" rollback-for="Exception" />
   <tx:method name="all*" rollback-for="Exception" />
   <tx:method name="copy*" rollback-for="Exception" />
   <tx:method name="drop*" rollback-for="Exception" />
   <tx:method name="assign*" rollback-for="Exception" />
  </tx:attributes>
 </tx:advice>
 <aop:config>
 <!-- 配置切入点,匹配指定包下所有以Impl结尾的类执行的所有方法 -->
  <aop:pointcut id="naspServiceFrameworkCut"
   expression="execution(* com.net263.framework.service.impl.*Impl.*(..))" />
  <aop:pointcut id="naspServiceFrameworkUMCut"
   expression="execution(* com.net263.framework.um.service.impl.*Impl.*(..))" />
  <aop:pointcut id="naspServiceCut"
   expression="execution(* com.net263.nasp.service.impl.*Impl.*(..))" />
   
  <!-- 指定在leeService切入点应用txAdvice事务切面 -->
  <aop:advisor advice-ref="txAdvice" pointcut-ref="naspServiceFrameworkCut" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="naspServiceCut" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="naspServiceFrameworkUMCut" />
 </aop:config>

 
 <!--配置业务逻辑组件====================================--> 

@declarativeServices@ 
</beans>


搜索相机

  评论这张
 
阅读(908)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016