去掉getter方法里的参数 package test; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * 增加自动写入application-Context.xml功能,把生成的dao与service对应的spring配置文件写入里面 * @Description: ibator补丁工具,生成Manager类,替换xml,java里的Date数据类型 * @author: 尹滨 * @E-mail: yinbinhome@163.com * @date 2011-3-7 上午09:55:26 * @version 4.0 * */ public class CopyOfIbatorConfig2 { /** * @param args */ public static void main(String[] args) { String path_dao="D://workspace//rcportalTEST//src//com//rc//portal//dao//impl"; String path_sqlmap="D://workspace//rcportalTEST//src//com//rc//portal//vo//sqlmap"; String path_vo="D://workspace//rcportalTEST//src//com//rc//portal//vo"; String path_service="D://workspace//rcportalTEST//src//com//rc//portal//service//impl"; String path_app_dao="D://workspace//rcportalTEST//conf_spring//applicationContext-dao.xml"; String path_app_service="D://workspace//rcportalTEST//conf_spring//applicationContext-service.xml"; new CopyOfIbatorConfig2().readyGO(path_dao,path_sqlmap,path_vo,path_service,path_app_dao,path_app_service); } public void readyGO(String daopath,String sqlpath,String vopath,String serivepath,String app_dao,String app_service){ start(daopath,sqlpath,vopath,serivepath,app_dao,app_service); moveDAO(daopath); } public void start(String daopath,String sqlpath,String vopath,String serivepath,String app_dao,String app_service){ System.out.println("处理dao......"); startDo(daopath); System.out.println("处理sqlmap......"); startDo(sqlpath); System.out.println("处理vo......"); startDo(vopath); System.out.println("移动dao......"); startDo2(daopath,serivepath,app_dao,app_service); } /** * 按目录查找文件, * 1.换文件中的指定字符串 * 2.生成Manager接口 * 3.生成ManagerImpl类 */ public void startDo(String path){ File file=new File(path); File[] files = file.listFiles(); int i=0; //System.out.println("文件路径:"+path); if(files!=null && files.length>0){ for(File f:files){ //System.out.print("/n第 "+(++i)+" 个文件"); String filename=f.getName(); if(filename.toLowerCase().indexOf("_sqlmap.xml")>0){ System.out.println("替换sqlMap.xml里时间类型......"); String fileALLname=f.getPath(); readFileByLines(fileALLname,"DATE","TIMESTAMP"); }else if(filename.toLowerCase().indexOf("example.java")>0){ System.out.println("替换Example.java文件里的java.sql.Date时间类型为java.util.Date......"); String fileALLname=f.getPath(); readFileByLines(fileALLname,"java.sql.Date","Date"); }else if(filename.toLowerCase().indexOf("daoimpl.java")>0){ System.out.println("增加daoImpl里的sqlMapClient的setter,getter方法......"); String fileALLname=f.getPath(); chgDAOImpl(fileALLname); }else if(filename.toLowerCase().indexOf("dao.java")>0){ System.out.println("生成Manager.java接口,并移动到ManagerImpl目录......"); addManager(f); System.out.println("生成ManagerImple.java......"); File f1=new File(f.getPath()); newManagerImpl(f1); } } } } /** * 往application.xml里追加dao与service的bean * @param dao_path dao的文件夹 * @param service_path sevice文件夹 * @param app_dao_path application-dao.xml 全路径 * @param app_service_path application-service.xml 全路径 */ public void startDo2(String dao_path,String service_path,String app_dao_path,String app_service_path){ File file=new File(dao_path); File[] files = file.listFiles(); int i=0; //System.out.println("文件路径:"+dao_path); if(files!=null && files.length>0){ for(File f:files){ //System.out.print("/n第 "+(++i)+" 个文件"); String filename=f.getName(); if(filename.toLowerCase().indexOf("daoimpl.java")>0){ //System.out.println("开始生成dao的bean......"); String fileALLname=f.getPath(); String daobean=getDaoBean(fileALLname); daobean+="/n"; daobean+="</beans>"; readFileByLines(app_dao_path,"</beans>",daobean); } } } File file2=new File(service_path); File[] files2 = file2.listFiles(); int i2=0; //System.out.println("文件路径:"+service_path); if(files2!=null && files2.length>0){ for(File f:files2){ //System.out.print("/n第 "+(++i2)+" 个文件"); String filename=f.getName(); if(filename.toLowerCase().indexOf("managerimpl.java")>0){ //System.out.println("开始生成service的bean......"); String fileALLname=f.getPath(); String servicebean=getServiceBean(fileALLname); servicebean+="/n"; servicebean+="</beans>"; readFileByLines(app_service_path,"</beans>",servicebean); } } } } /** * 按行读取,替换指定字符串 */ public void readFileByLines(String fileName,String oldstring,String newstring) { File file = new File(fileName); BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; String temp=tempString.replace(oldstring, newstring); sb.append(temp+"/n"); } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } System.out.println(sb); appendMethodB(fileName,sb.toString()); } /** * 按给定内容,更新文件内容 */ public void appendMethodB(String fileName, String content) { try { //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, false); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 更新DAO为SqlMapClient增加setter getter * @param fileName */ public void chgDAOImpl(String fileName){ File file = new File(fileName); BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); String daoname=file.getName(); int s1=daoname.indexOf("."); daoname=daoname.substring(0,s1); try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; if(tempString.toLowerCase().indexOf("private sqlmapclient sqlmapclient;")>0){ sb.append(tempString+"/n/n"); sb.append(" public void setSqlMapClient(SqlMapClient sqlMapClient) {/n"+ " this.sqlMapClient = sqlMapClient;/n"+ " }/n/n"+ " public SqlMapClient getSqlMapClient() {/n"+ " return sqlMapClient;/n"+ " }/n"); sb.append(" public "+daoname+"() {/n"); sb.append(" super();/n"); sb.append(" }/n"); }else{ sb.append(tempString+"/n"); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } System.out.println(sb); appendMethodB(fileName,sb.toString()); } /** * 增加Manager接口 * @param fileIn */ public void addManager(File fileIn){ BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); try { reader = new BufferedReader(new FileReader(fileIn)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; if(tempString.indexOf("interface")>0 && tempString.indexOf("DAO")>0){ String temp=tempString.replace("DAO", "Manager"); sb.append(temp+"/n"); }else if(tempString.indexOf("package")!=-1){ String temp=tempString.replace("dao.impl", "service"); sb.append(temp+"/n"); }else{ sb.append(tempString+"/n"); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } System.out.println(sb); String name=fileIn.getName().replace("DAO", "Manager"); String newFilename=fileIn.getParent().replace("dao//impl", "service")+"//"+name; appendMethodB(newFilename,sb.toString()); } /** * 更改DAO的名字 * @param name * @return */ public String chgName(String name){ // String firstC=name.substring(0, 1).toLowerCase(); // String otherC=name.substring(1); return name.toLowerCase(); } /** * 第一个字母大写 * @param word * @return */ private String firstWordToUpper(String word) { String first = word.substring(0, 1); return first.toUpperCase() + word.substring(1); } /** * 在manager里增加dao的setter和getter * setter getter * @param name */ public String getset(String name){ StringBuffer sb=new StringBuffer(""); sb.append(" public void set"+firstWordToUpper(chgName(name))+"("+name+" "+chgName(name)+"){/n"); sb.append(" this."+chgName(name)+"="+chgName(name)+";/n"); sb.append(" }/n"); sb.append(" public "+name+" get"+firstWordToUpper(chgName(name))+"(){/n"); sb.append(" return this."+chgName(name)+";/n"); sb.append(" }/n"); return sb.toString(); } /** * 取方法名 * @param ss * @return */ public String getMethodName(String ss){ int s1=ss.indexOf("("); String temp=ss.substring(0,s1); int s2=temp.lastIndexOf(" "); return (ss.substring(s2,s1)); } /** * 得到变量名字的串 * @param line * @return */ public String getParam(String line){ int s1=line.indexOf("("); int s2=line.indexOf(")"); String params=line.substring(s1+1,s2); String p1=""; String p2=""; String paramStr=""; if(params.indexOf(",")>0){ int s3=params.indexOf(" "); int s4=params.indexOf(","); int s5=params.lastIndexOf(" "); p1=params.substring(s3,s4); p2=params.substring(s5); paramStr="("+p1+","+p2+")"; }else{ int s6=params.lastIndexOf(" "); p1=params.substring(s6); paramStr="("+p1+")"; } return paramStr; } /** * 更具接口方法声明生成方法体 * @param line * @param daoName * @return */ public String getMethod(String line,String daoName){ String methodname=getMethodName(line); StringBuffer sb=new StringBuffer(""); if(line.indexOf("SQLException;")>0 && line.indexOf("import")<0){ if(line.indexOf("void")>0 && line.indexOf(";")>0){ String temp=line.replace(";", "{/n"); sb.append(" public "+temp); sb.append(" "+chgName(daoName)+"."+methodname+getParam(line)+";/n"); sb.append(" }/n/r"); }else{ String temp=line.replace(";", "{/n"); sb.append(" public "+temp); sb.append(" return "+chgName(daoName)+"."+methodname+getParam(line)+";/n"); sb.append(" }/n/r"); } } return sb.toString(); } /** * 传入DAO接口的File,生成ManagerImpl * @param file */ public void newManagerImpl(File file){ String oldname=file.getName(); int s1=oldname.indexOf("."); oldname=oldname.substring(0,s1); String name=oldname.replace("DAO", "ManagerImpl"); String filename=file.getParent().replace("dao", "service")+"//"+name+".java"; file.getName(); BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; if(tempString.indexOf("interface")>0){ sb.append(""); String temp=tempString.replace("interface", "class"); temp=temp.replace(oldname, name); temp=temp.replace("{", ""); temp=temp+" implements "+oldname.replace("DAO", "Manager")+" {/n"; sb.append(temp); sb.append("/n"); sb.append(" private "+oldname+" "+chgName(oldname)+";/n/n"); sb.append(" public "+name+"() {/n"); sb.append(" super();/n"); sb.append(" }/n"); sb.append(getset(oldname)); }else if(tempString.indexOf("SQLException;")>0 && tempString.indexOf("import")<0){ String methodBody=getMethod(tempString,oldname); sb.append(methodBody); }else if(tempString.indexOf("package")!=-1){ String tempIMP=tempString.replace("package", "import"); tempIMP=tempIMP.replace("dao.impl", "dao"); tempIMP=tempIMP.replace(";", ".")+oldname+";/n"; String tempmanager=tempString.replace("package", "import"); tempmanager=tempmanager.replace("dao", "service"); String managername=name.substring(0,name.indexOf("Impl")); tempmanager=tempmanager.replace("impl", managername); String temp=tempString.replace("dao", "service"); sb.append(temp+"/n/n"); sb.append(tempIMP); sb.append(tempmanager); }else{ sb.append(tempString+"/n"); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } appendMethodB(filename,sb.toString()); } public void addSpringDao(String filename){ } public String getDaoModel(){ String daoM= " <bean id=/"#id#/" class=/"#classname#/">/n"+ " <property name=/"sqlMapClient/" ref=/"sqlMapClient/" />/n"+ " </bean>/n"; return daoM; } public String getServiceModel(){ String serviceM= " <bean id=/"#id#/" class=/"#classname#/">/n"+ " <property name=/"#daoname#/" ref=/"#daoref#/" />/n"+ " </bean>/n"; return serviceM; } /** * 取类得全路径名,dao属性名 * @param fileName * @param oldstring * @param newstring */ public Map getDaoAllName(String fileName) { String daoname=""; //String name=""; Map<String,String> map=new HashMap<String,String>(); File file = new File(fileName); BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; if(tempString.indexOf("package")!=-1){ daoname=tempString.substring(tempString.indexOf("package")+7,tempString.indexOf(";")).trim()+"."; }else if(tempString.indexOf("public class")!=-1){ daoname+=tempString.substring(tempString.indexOf("public class")+12,tempString.indexOf("implements")).trim(); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } String oldname=file.getName(); int s1=oldname.indexOf("."); oldname=oldname.substring(0,s1); String daoAttrname=this.chgName(oldname); daoAttrname=daoAttrname.substring(0,daoAttrname.indexOf("impl")); map.put("daoname", daoname); map.put("daoAttrname", daoAttrname); return map; } /** * 取service类全路径名和dao属性的名字 * @param fileName * @return */ public Map getServiceAllName(String fileName) { String servicename=""; String daoname=""; Map<String,String> map=new HashMap<String,String>(); File file = new File(fileName); BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; if(tempString.indexOf("package")!=-1){ servicename=tempString.substring(tempString.indexOf("package")+7,tempString.indexOf(";")).trim()+"."; }else if(tempString.indexOf("public class")!=-1){ servicename+=tempString.substring(tempString.indexOf("public class")+12,tempString.indexOf("implements")).trim(); }else if(tempString.indexOf("private")>0 && tempString.indexOf("DAO")>0){ daoname=tempString.substring(tempString.indexOf("DAO ")+4,tempString.indexOf(";")).trim(); } String temp=tempString; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } String oldname=file.getName(); int s1=oldname.indexOf("."); oldname=oldname.substring(0,s1); String serviceAttrname=this.chgName(oldname); serviceAttrname=serviceAttrname.substring(0,serviceAttrname.indexOf("impl")); map.put("servicename", servicename); map.put("daoname", daoname); map.put("serviceAttrname", serviceAttrname); return map; } /** * 生成dao bean * @param filename * @return */ public String getDaoBean(String filename){ Map<String,String> map=getDaoAllName(filename); String daoname=map.get("daoname"); String daoAttrname=map.get("daoAttrname"); String daobean=getDaoModel(); daobean=daobean.replace("#classname#", daoname); daobean=daobean.replace("#id#", chgName(daoAttrname)); return daobean; } /** * 生成service bean * @param filename * @return */ public String getServiceBean(String filename){ Map<String,String> map=getServiceAllName(filename); String servicename=map.get("servicename"); String daoname=map.get("daoname"); String serviceAttrname=map.get("serviceAttrname"); String servicebean=getServiceModel(); servicebean=servicebean.replace("#classname#", servicename); servicebean=servicebean.replace("#id#", serviceAttrname); servicebean=servicebean.replace("#daoname#", chgName(daoname)); servicebean=servicebean.replace("#daoref#", chgName(daoname)); return servicebean; } public void moveDAO(String path){ System.out.println("移动DAO接口到DAOImpl目录......"); File file=new File(path); File[] files = file.listFiles(); int i=0; //System.out.println("文件路径:"+path); if(files!=null && files.length>0){ for(File f:files){ String filename=f.getName(); if(filename.toLowerCase().indexOf("daoimpl.java")>0){ String newDaoname=filename.replace("Impl.java", ".java"); BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); try { reader = new BufferedReader(new FileReader(f.getPath())); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; if(tempString.indexOf("package")!=-1){ String temp=tempString.replace("package", "import"); temp=temp.replace("dao.impl;", "dao.")+newDaoname.replace(".java", ";"); sb.append(tempString+"/n/n"); sb.append(temp); }else{ sb.append(tempString+"/n"); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } appendMethodB(f.getPath(),sb.toString()); }else if(filename.toLowerCase().indexOf("dao.java")>0){ BufferedReader reader = null; StringBuffer sb=new StringBuffer(""); try { reader = new BufferedReader(new FileReader(f.getPath())); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; if(tempString.indexOf("package")!=-1){ tempString=tempString.replace("dao.impl", "dao"); sb.append(tempString); }else{ sb.append(tempString+"/n"); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } appendMethodB(f.getPath().replace("dao//impl", "dao"),sb.toString()); f.delete(); } } } } }