【struts2+hibernate+spring项目实战】java读写实现代码生成器(ssh)
生活随笔
收集整理的這篇文章主要介紹了
【struts2+hibernate+spring项目实战】java读写实现代码生成器(ssh)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、概述
開發(fā)階段80%以上的模塊都具有基本增刪改查功能,反反復復的制作不僅效率低下,而且容易出錯,使用代碼生成器自動生成代碼,只需要精品的開發(fā)出代碼生成器,后期代碼開發(fā)效率將直線上升。
代碼生成器的思想就是通過執(zhí)行程序,自動生成出Java源代碼,核心原理是讀寫文件。
二、具體實現(xiàn)
2.1、數據初始化
這個方法主要是獲取包名、實體類名稱等信息。
//-1.數據初始化private void dataInit() {String className = clazz.getSimpleName(); //UserModelb = className.substring(0, className.length()-5); //UserString first = b.substring(0,1); //El = first.toLowerCase(); //es = l+b.substring(1); //userString rootPkg = clazz.getPackage().getName(); //org.sihai.qualitycontrol.auth.user.vopkg = rootPkg.substring(0,rootPkg.length()-3); //org.sihai.qualitycontrol.auth.userdir = pkg.replace(".","/"); //org/sihai/qualitycontrol/auth/user}2.2、創(chuàng)建目錄
生成具體的代碼的所在目錄
//0.創(chuàng)建目錄private void generatorDirectory() {//business/ebi// src+//org.sihai.qualitycontrol.auth.user+business/ebi .voFile f = new File("src/"+dir+"/business/ebi");f.mkdirs();//business/ebof = new File("src/"+dir+"/business/ebo");f.mkdirs();//dao/daof = new File("src/"+dir+"/dao/dao");f.mkdirs();//dao/implf = new File("src/"+dir+"/dao/impl");f.mkdirs();//webf = new File("src/"+dir+"/web");f.mkdirs();}2.3、創(chuàng)建查詢實體類
//1.QueryModelprivate void generatorQueryModel() throws Exception {//1.創(chuàng)建文件File f = new File("src/"+dir+"/vo/"+b+"QueryModel.java");//判斷:如果該文件存在,終止操作if(f.exists()){return;}f.createNewFile();//2.IO寫入內容BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("package "+pkg+".vo;");bw.newLine();bw.newLine();bw.write("import org.sihai.qualitycontrol.util.base.BaseQueryModel;");bw.newLine();bw.newLine();bw.write("public class "+b+"QueryModel extends "+b+"Model implements BaseQueryModel{");bw.newLine();bw.write(" // TODO 添加自定義查詢條件");bw.newLine();bw.write("}");bw.newLine();bw.flush();bw.close();}2.4、創(chuàng)建實體類的hibernate的配置文件
//2.Hbm.xmlprivate void generatorHbmXml() throws Exception {//1.創(chuàng)建文件File f = new File("src/"+dir+"/vo/"+b+"Model.hbm.xml");if(f.exists()){return;}f.createNewFile();//2.IO寫入內容BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");bw.newLine();bw.write("<!DOCTYPE hibernate-mapping PUBLIC");bw.newLine();bw.write(" '-//Hibernate/Hibernate Mapping DTD 3.0//EN'");bw.newLine();bw.write(" 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>");bw.newLine();bw.write("<hibernate-mapping>");bw.newLine();bw.write(" <class name=\""+pkg+".vo."+b+"Model\" table=\"tbl_"+s+"\">");bw.newLine();bw.write(" <id name=\"uuid\">");bw.newLine();bw.write(" <generator class=\"native\" />");bw.newLine();bw.write(" </id>");bw.newLine();//hibernate的映射配置文件中要對原始模型類中的屬性進行配置,反射獲取所有字段Field[] fds = clazz.getDeclaredFields();for(Field fd:fds) {//如果字段的修飾符是private,生成if(fd.getModifiers() == Modifier.PRIVATE && !fd.getName().equals("uuid")){//如果是關聯(lián)關系不生成,不是關聯(lián)關系(Long,Integer,Double,String)if( fd.getType().equals(String.class)||fd.getType().equals(Long.class)||fd.getType().equals(Integer.class)||fd.getType().equals(Double.class)){bw.write(" <property name=\""+fd.getName()+"\"/>");bw.newLine();}}}bw.write(" </class>");bw.newLine();bw.write("</hibernate-mapping>");bw.newLine();bw.flush();bw.close(); }2.5、創(chuàng)建dao接口
//3.Daoprivate void generatorDao() throws Exception {File f = new File("src/"+dir+"/dao/dao/"+b+"Dao.java");if(f.exists()){return;}f.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("package "+pkg+".dao.dao;");bw.newLine();bw.newLine();bw.write("import "+pkg+".vo."+b+"Model;");bw.newLine();bw.write("import org.sihai.qualitycontrol.util.base.BaseDao;");bw.newLine();bw.newLine();bw.write("public interface "+b+"Dao extends BaseDao<"+b+"Model> {");bw.newLine();bw.newLine();bw.write("}");bw.newLine();bw.flush();bw.close(); }2.6、創(chuàng)建dao實現(xiàn)類
//4.Implprivate void generatorImpl() throws Exception {File f = new File("src/"+dir+"/dao/impl/"+b+"Impl.java");if(f.exists()){return;}f.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("package "+pkg+".dao.impl;");bw.newLine();bw.newLine();bw.write("import org.hibernate.criterion.DetachedCriteria;");bw.newLine();bw.write("import org.hibernate.criterion.Restrictions;");bw.newLine();bw.newLine();bw.write("import "+pkg+".dao.dao."+b+"Dao;");bw.newLine();bw.write("import "+pkg+".vo."+b+"Model;");bw.newLine();bw.write("import "+pkg+".vo."+b+"QueryModel;");bw.newLine();bw.write("import org.sihai.qualitycontrol.util.base.BaseImpl;");bw.newLine();bw.write("import org.sihai.qualitycontrol.util.base.BaseQueryModel;");bw.newLine();bw.newLine();bw.write("public class "+b+"Impl extends BaseImpl<"+b+"Model> implements "+b+"Dao{");bw.newLine();bw.newLine();bw.write(" public void doQbc(DetachedCriteria dc,BaseQueryModel qm){");bw.newLine();bw.write(" "+b+"QueryModel "+l+"qm = ("+b+"QueryModel)qm;");bw.newLine();bw.write(" // TODO 添加自定義查詢條件");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write("}");bw.newLine();bw.flush();bw.close(); }2.7、創(chuàng)建service的接口
//5.Ebiprivate void generatorEbi() throws Exception {File f = new File("src/"+dir+"/business/ebi/"+b+"Ebi.java");if(f.exists()){return;}f.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("package "+pkg+".business.ebi;");bw.newLine();bw.newLine();bw.write("import org.springframework.transaction.annotation.Transactional;");bw.newLine();bw.newLine();bw.write("import "+pkg+".vo."+b+"Model;");bw.newLine();bw.write("import org.sihai.qualitycontrol.util.base.BaseEbi;");bw.newLine();bw.newLine();bw.write("@Transactional");bw.newLine();bw.write("public interface "+b+"Ebi extends BaseEbi<"+b+"Model>{");bw.newLine();bw.newLine();bw.write("}");bw.newLine();bw.flush();bw.close(); }2.8、創(chuàng)建service的實現(xiàn)類
//6.Eboprivate void generatorEbo() throws Exception {File f = new File("src/"+dir+"/business/ebo/"+b+"Ebo.java");if(f.exists()){return;}f.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("package "+pkg+".business.ebo;");bw.newLine();bw.newLine();bw.write("import java.io.Serializable;");bw.newLine();bw.write("import java.util.List;");bw.newLine();bw.newLine();bw.write("import "+pkg+".business.ebi."+b+"Ebi;");bw.newLine();bw.write("import "+pkg+".dao.dao."+b+"Dao;");bw.newLine();bw.write("import "+pkg+".vo."+b+"Model;");bw.newLine();bw.write("import org.sihai.qualitycontrol.util.base.BaseQueryModel;");bw.newLine();bw.newLine();bw.write("public class "+b+"Ebo implements "+b+"Ebi{");bw.newLine();bw.write(" private "+b+"Dao "+s+"Dao;");bw.newLine();bw.write(" public void set"+b+"Dao("+b+"Dao "+s+"Dao) {");bw.newLine();bw.write(" this."+s+"Dao = "+s+"Dao;");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" public void save("+b+"Model "+l+"m) {");bw.newLine();bw.write(" "+s+"Dao.save("+l+"m);");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" public void update("+b+"Model "+l+"m) {");bw.newLine();bw.write(" "+s+"Dao.update("+l+"m);");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" public void delete("+b+"Model "+l+"m) {");bw.newLine();bw.write(" "+s+"Dao.delete("+l+"m);");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" public "+b+"Model get(Serializable uuid) {");bw.newLine();bw.write(" return "+s+"Dao.get(uuid);");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" public List<"+b+"Model> getAll() {");bw.newLine();bw.write(" return "+s+"Dao.getAll();");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" public List<"+b+"Model> getAll(BaseQueryModel qm, Integer pageNum,Integer pageCount) {");bw.newLine();bw.write(" return "+s+"Dao.getAll(qm,pageNum,pageCount);");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" public Integer getCount(BaseQueryModel qm) {");bw.newLine();bw.write(" return "+s+"Dao.getCount(qm);");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write("}");bw.newLine();bw.flush();bw.close(); }2.9、創(chuàng)建action類
//7.Actionprivate void generatorAction() throws Exception {File f = new File("src/"+dir+"/web/"+b+"Action.java");if(f.exists()){return;}f.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("package "+pkg+".web;");bw.newLine();bw.newLine();bw.write("import java.util.List;");bw.newLine();bw.newLine();bw.write("import "+pkg+".business.ebi."+b+"Ebi;");bw.newLine();bw.write("import "+pkg+".vo."+b+"Model;");bw.newLine();bw.write("import "+pkg+".vo."+b+"QueryModel;");bw.newLine();bw.write("import org.sihai.qualitycontrol.util.base.BaseAction;");bw.newLine();bw.newLine();bw.write("public class "+b+"Action extends BaseAction{");bw.newLine();bw.write(" public "+b+"Model "+l+"m = new "+b+"Model();");bw.newLine();bw.write(" public "+b+"QueryModel "+l+"qm = new "+b+"QueryModel();");bw.newLine();bw.newLine();bw.write(" private "+b+"Ebi "+s+"Ebi;");bw.newLine();bw.write(" public void set"+b+"Ebi("+b+"Ebi "+s+"Ebi) {");bw.newLine();bw.write(" this."+s+"Ebi = "+s+"Ebi;");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" //列表");bw.newLine();bw.write(" public String list(){");bw.newLine();bw.write(" setDataTotal("+s+"Ebi.getCount("+l+"qm));");bw.newLine();bw.write(" List<"+b+"Model> "+s+"List = "+s+"Ebi.getAll("+l+"qm,pageNum,pageCount);");bw.newLine();bw.write(" put(\""+s+"List\", "+s+"List);");bw.newLine();bw.write(" return LIST;");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" //到添加");bw.newLine();bw.write(" public String input(){");bw.newLine();bw.write(" if("+l+"m.getUuid()!=null){");bw.newLine();bw.write(" "+l+"m = "+s+"Ebi.get("+l+"m.getUuid());");bw.newLine();bw.write(" }");bw.newLine();bw.write(" return INPUT;");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" //添加");bw.newLine();bw.write(" public String save(){");bw.newLine();bw.write(" if("+l+"m.getUuid() == null){");bw.newLine();bw.write(" "+s+"Ebi.save("+l+"m);");bw.newLine();bw.write(" }else{");bw.newLine();bw.write(" "+s+"Ebi.update("+l+"m);");bw.newLine();bw.write(" }");bw.newLine();bw.write(" return TO_LIST;");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write(" //刪除");bw.newLine();bw.write(" public String delete(){");bw.newLine();bw.write(" "+s+"Ebi.delete("+l+"m);");bw.newLine();bw.write(" return TO_LIST;");bw.newLine();bw.write(" }");bw.newLine();bw.newLine();bw.write("}");bw.newLine();bw.flush();bw.close(); }2.10、創(chuàng)建spring的配置文件
//8.applicationContext.xmlprivate void generatorApplicationContextXml() throws Exception {File f = new File("resources/applicationContext-"+s+".xml");if(f.exists()){return;}f.createNewFile();BufferedWriter bw = new BufferedWriter(new FileWriter(f));bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");bw.newLine();bw.write("<beans xmlns=\"http://www.springframework.org/schema/beans\"");bw.newLine();bw.write(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");bw.newLine();bw.write(" xsi:schemaLocation=\"");bw.newLine();bw.write(" http://www.springframework.org/schema/beans ");bw.newLine();bw.write(" http://www.springframework.org/schema/beans/spring-beans.xsd");bw.newLine();bw.write(" \"> ");bw.newLine();bw.write(" <!-- Action -->");bw.newLine();bw.write(" <bean id=\""+s+"Action\" class=\""+pkg+".web."+b+"Action\" scope=\"prototype\">");bw.newLine();bw.write(" <property name=\""+s+"Ebi\" ref=\""+s+"Ebi\"/>");bw.newLine();bw.write(" </bean>");bw.newLine();bw.write(" <!-- Ebi -->");bw.newLine();bw.write(" <bean id=\""+s+"Ebi\" class=\""+pkg+".business.ebo."+b+"Ebo\">");bw.newLine();bw.write(" <property name=\""+s+"Dao\" ref=\""+s+"Dao\"/>");bw.newLine();bw.write(" </bean>");bw.newLine();bw.write(" <!-- Dao -->");bw.newLine();bw.write(" <bean id=\""+s+"Dao\" class=\""+pkg+".dao.impl."+b+"Impl\">");bw.newLine();bw.write(" <property name=\"sessionFactory\" ref=\"sessionFactory\"/>");bw.newLine();bw.write(" </bean>");bw.newLine();bw.write("</beans>");bw.newLine();bw.flush();bw.close(); }2.11、對struts.xml配置文件修改
private void modifyStrutsXml() throws Exception {//1.讀取原始的內容//2.讀取到特定位置(package)添加指定內容//我們要讀的文件與寫的文件是同一個文件/*RandomAccessFile類讀寫文件時讀取,一共100,讀70,寫,寫的內容會覆蓋后30111222333444在333的后面寫5111222333544在333的后面寫5111222333555*///方案一:/*讀取原始文件,將內容寫入新文件寫之前判斷,讀取的內容是否是特定內容,特定內容寫之前,加入新的內容寫完畢之后生成了新的文件,刪除老的文件,使用新文件更名為老的文件*///方案二://1.讀取原始文件的文件大小,字節(jié)總數1000File f = new File("resources/struts.xml");long len = f.length();//2.創(chuàng)建一個字節(jié)數組,大小等于原始文件字節(jié)總數byte[] buf = new byte[(int)len];//3.將原始文件讀入該byte數組InputStream is = new FileInputStream(f);is.read(buf);is.close();//4.將buf轉化為字符串String all = new String(buf);//5.查找固定位置int idx = all.lastIndexOf(" </package>");//6.將要寫入的內容插入該位置String info = " <!-- "+b+" -->\r\n <action name=\""+s+"_*\" class=\""+s+"Action\" method=\"{1}\">\r\n </action>\r\n\r\n";//7.將info加入all的指定位置StringBuilder sbf = new StringBuilder(all);sbf.insert(idx, info);//8.將sbf中的組合最終內容寫入struts.xmlFileOutputStream fos = new FileOutputStream(f);fos.write(sbf.toString().getBytes());fos.close();}2.12、生成工具方法
private Class clazz; private String b ; //User private String l ; //e private String s ; //user private String pkg ; //org.sihai.qualitycontrol.auth.user private String dir ; //org/sihai/qualitycontrol/auth/user/vo public GeneratorUtil(Class clazz) throws Exception{this.clazz = clazz;//生成所有的內容//-1.數據初始化dataInit();//0.創(chuàng)建目錄generatorDirectory();//1.QueryModelgeneratorQueryModel();//2.Hbm.xmlgeneratorHbmXml();//3.DaogeneratorDao();//4.ImplgeneratorImpl();//5.EbigeneratorEbi();//6.EbogeneratorEbo();//7.ActiongeneratorAction();//8.applicationContext.xmlgeneratorApplicationContextXml();//9.struts.xml(選作)//modifyStrutsXml();}三、測試
public static void main(String[] args) throws Exception {new GeneratorUtil(ResModel.class); }總結
以上是生活随笔為你收集整理的【struts2+hibernate+spring项目实战】java读写实现代码生成器(ssh)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【struts2+hibernate+s
- 下一篇: svn教程----svn简介