博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[需再总结]SSH整合代码生成器
阅读量:6479 次
发布时间:2019-06-23

本文共 13127 字,大约阅读时间需要 43 分钟。

package cn.itcast.invoice.util.generator;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import cn.itcast.invoice.invoice.storeoper.vo.StoreOperModel;//生成器public class GeneratorUtil {	private Class clazz;	private String all;					// EmpModel	private String small;				// emp	private String big;					// Emp	private String little;				// e	private String pkg;					// cn.itcast.invoice.auth	private String dir;					// cn\itcast\invoice\auth		public static void main(String[] args) throws Exception {		// 只要你提供Model我就可以给你生成你要的所有文件		//EmpModel,RoleModel,ResModel,MenuModel		//SupplierModel,GoodsTypeModel,GoodsModel		//OrderModel,OrderDetailModel		//StoreModel,StoreDetailModel,StoreOperModel		new GeneratorUtil(StoreOperModel.class);		System.out.println("请添加Action在struts.xml中的定义");		System.out.println("请添加关联关系,位置hbm.xml");		System.out.println("请添加自定义查询条件数据层实现,位置Impl");		System.out.println("请添加自定义查询条件,位置QueryModel");		System.out.println("运行完毕!");	}		public GeneratorUtil(Class clazz) throws Exception {		this.clazz = clazz;		// -1.数据初始化		dataInit();		// 0.生成目录(包)		generatorDirectory();		// 1.QueryModel		generatorQueryModel();		// 2.hbm.xml		generatorHbmXml();		// 3.Dao		generatorDao();		// 4.Impl		generatorImpl();		// 5.Ebi		generatorEbi();		// 6.Ebo		generatorEbo();		// 7.Action		generatorAction();		// 8.applicationContext-**.xml		generatorApplicationContextXml();	}	// 8.applicationContext-**.xml	private void generatorApplicationContextXml() throws Exception {		File f = new File("resources/applicationContext-"+small+".xml");		if(f.exists()){			return;		}		f.createNewFile();				BufferedWriter bw = new BufferedWriter(new FileWriter(f));		bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.flush(); bw.close(); } // 7.Action private void generatorAction() throws Exception { File f = new File("src/"+dir+"/web/"+big+"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."+big+"Ebi;"); bw.newLine(); bw.write("import "+pkg+".vo."+big+"Model;"); bw.newLine(); bw.write("import "+pkg+".vo."+big+"QueryModel;"); bw.newLine(); bw.write("import cn.itcast.invoice.util.base.BaseAction;"); bw.newLine(); bw.newLine(); bw.write("public class "+big+"Action extends BaseAction{"); bw.newLine(); bw.write(" public "+big+"Model "+little+"m = new "+big+"Model();"); bw.newLine(); bw.write(" public "+big+"QueryModel "+little+"qm = new "+big+"QueryModel();"); bw.newLine(); bw.newLine(); bw.write(" private "+big+"Ebi "+small+"Ebi;"); bw.newLine(); bw.write(" public void set"+big+"Ebi("+big+"Ebi "+small+"Ebi) {"); bw.newLine(); bw.write(" this."+small+"Ebi = "+small+"Ebi;"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public String list(){"); bw.newLine(); bw.write(" setDataTotal("+small+"Ebi.getCount("+little+"qm));"); bw.newLine(); bw.write(" List<"+big+"Model> "+small+"List = "+small+"Ebi.getAll("+little+"qm,pageNum,pageCount);"); bw.newLine(); bw.write(" put(\""+small+"List\", "+small+"List);"); bw.newLine(); bw.write(" return LIST;"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public String input(){"); bw.newLine(); bw.write(" if("+little+"m.getUuid() != null){"); bw.newLine(); bw.write(" "+little+"m = "+small+"Ebi.get("+little+"m.getUuid());"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.write(" return INPUT;"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public String save(){"); bw.newLine(); bw.write(" if("+little+"m.getUuid() == null){"); bw.newLine(); bw.write(" "+small+"Ebi.save("+little+"m);"); bw.newLine(); bw.write(" }else{"); bw.newLine(); bw.write(" "+small+"Ebi.update("+little+"m);"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.write(" return TO_LIST;"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public String delete(){"); bw.newLine(); bw.write(" "+small+"Ebi.delete("+little+"m);"); bw.newLine(); bw.write(" return TO_LIST;"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.write("}"); bw.newLine(); bw.flush(); bw.close(); } // 6.Ebo private void generatorEbo() throws Exception { File f = new File("src/"+dir+"/business/ebo/"+big+"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."+big+"Ebi;"); bw.newLine(); bw.write("import "+pkg+".dao.dao."+big+"Dao;"); bw.newLine(); bw.write("import "+pkg+".vo."+big+"Model;"); bw.newLine(); bw.write("import cn.itcast.invoice.util.base.BaseQueryModel;"); bw.newLine(); bw.newLine(); bw.write("public class "+big+"Ebo implements "+big+"Ebi{"); bw.newLine(); bw.write(" private "+big+"Dao "+small+"Dao;"); bw.newLine(); bw.write(" public void set"+big+"Dao("+big+"Dao "+small+"Dao) {"); bw.newLine(); bw.write(" this."+small+"Dao = "+small+"Dao;"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public void save("+big+"Model "+little+"m) {"); bw.newLine(); bw.write(" "+small+"Dao.save("+little+"m);"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public void update("+big+"Model "+little+"m) {"); bw.newLine(); bw.write(" "+small+"Dao.update("+little+"m);"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public void delete("+big+"Model "+little+"m) {"); bw.newLine(); bw.write(" "+small+"Dao.delete("+little+"m);"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public "+big+"Model get(Serializable uuid) {"); bw.newLine(); bw.write(" return "+small+"Dao.get(uuid);"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public List<"+big+"Model> getAll() {"); bw.newLine(); bw.write(" return "+small+"Dao.getAll();"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public Integer getCount(BaseQueryModel qm) {"); bw.newLine(); bw.write(" return "+small+"Dao.getCount(qm);"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write(" public List<"+big+"Model> getAll(BaseQueryModel qm, Integer pageNum,Integer pageCount) {"); bw.newLine(); bw.write(" return "+small+"Dao.getAll(qm,pageNum,pageCount);"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write("}"); bw.newLine(); bw.flush(); bw.close(); } // 5.Ebi private void generatorEbi() throws Exception { File f = new File("src/"+dir+"/business/ebi/"+big+"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."+big+"Model;"); bw.newLine(); bw.write("import cn.itcast.invoice.util.base.BaseEbi;"); bw.newLine(); bw.write("@Transactional"); bw.newLine(); bw.write("public interface "+big+"Ebi extends BaseEbi<"+big+"Model>{"); bw.newLine(); bw.newLine(); bw.write("}"); bw.newLine(); bw.flush(); bw.close(); } // 4.Impl private void generatorImpl() throws Exception { File f = new File("src/"+dir+"/dao/impl/"+big+"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."+big+"Dao;"); bw.newLine(); bw.write("import "+pkg+".vo."+big+"Model;"); bw.newLine(); bw.write("import "+pkg+".vo."+big+"QueryModel;"); bw.newLine(); bw.write("import cn.itcast.invoice.util.base.BaseDaoImpl;"); bw.newLine(); bw.write("import cn.itcast.invoice.util.base.BaseQueryModel; "); bw.newLine(); bw.newLine(); bw.write("public class "+big+"Impl extends BaseDaoImpl<"+big+"Model> implements "+big+"Dao{"); bw.newLine(); bw.newLine(); bw.write(" public void doQbc(BaseQueryModel qm,DetachedCriteria dc){"); bw.newLine(); bw.write(" "+big+"QueryModel "+little+"qm = ("+big+"QueryModel)qm;"); bw.newLine(); bw.write(" //TODO 添加自定义查询条件"); bw.newLine(); bw.write(" }"); bw.newLine(); bw.newLine(); bw.write("}"); bw.newLine(); bw.flush(); bw.close(); } // 3.Dao private void generatorDao() throws Exception { File f = new File("src/"+dir+"/dao/dao/"+big+"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."+big+"Model;"); bw.newLine(); bw.write("import cn.itcast.invoice.util.base.BaseDao;"); bw.newLine(); bw.newLine(); bw.write("public interface "+big+"Dao extends BaseDao<"+big+"Model>{"); bw.newLine(); bw.newLine(); bw.write("}"); bw.newLine(); bw.flush(); bw.close(); } // 2.hbm.xml private void generatorHbmXml() throws Exception { File f = new File("src/"+dir+"/vo/"+big+"Model.hbm.xml"); if(f.exists()){ return; } f.createNewFile(); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); bw.write("
"); bw.newLine(); bw.write(""); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); //迭代生成所有的属性 //使用反射获取所有的字段 Field[] fds = clazz.getDeclaredFields(); for (Field fd :fds) { //如果是私有的生成 if(Modifier.PRIVATE == fd.getModifiers() && !"uuid".equals(fd.getName())){ //String,Long,Double,Integer if( fd.getType().equals(String.class) || fd.getType().equals(Long.class) || fd.getType().equals(Integer.class) || fd.getType().equals(Double.class) ){ if(!fd.getName().endsWith("View")){ bw.write("
"); bw.newLine(); } } } } bw.newLine(); bw.write("
"); bw.newLine(); bw.newLine(); bw.write("
"); bw.newLine(); bw.write("
"); bw.newLine(); bw.flush(); bw.close(); } // 1.QueryModel private void generatorQueryModel() throws Exception { //1.创建文件 //2.IO写数据 //3.关闭流 File f = new File("src/"+dir+"/vo/"+big+"QueryModel.java"); //如果该文件存在,则不做任何操作 if(f.exists()){ return; } f.createNewFile(); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); bw.write("package "+pkg+".vo;"); bw.newLine(); bw.newLine(); bw.write("import cn.itcast.invoice.util.base.BaseQueryModel;"); bw.newLine(); bw.newLine(); bw.write("public class "+big+"QueryModel extends "+big+"Model implements BaseQueryModel{"); bw.newLine(); bw.write(" //TODO 添加查询范围字段 "); bw.newLine(); bw.write("}"); bw.newLine(); bw.flush(); bw.close(); } // 0.生成目录(包) private void generatorDirectory() { File f = new File("src/"+dir+"/web"); f.mkdirs(); f = new File("src/"+dir+"/business/ebi"); f.mkdirs(); f = new File("src/"+dir+"/business/ebo"); f.mkdirs(); f = new File("src/"+dir+"/dao/dao"); f.mkdirs(); f = new File("src/"+dir+"/dao/impl"); f.mkdirs(); } // -1.数据初始化 private void dataInit() { // EmpModel all = clazz.getSimpleName(); // Emp big = all.substring(0,all.length()-5); // e little = all.substring(0,1).toLowerCase(); // emp small = little + big.substring(1); // cn.itcast.invoice.auth.emp String pkgTemp = clazz.getPackage().getName(); pkg = pkgTemp.substring(0,pkgTemp.length()-3); // cn\itcast\invoice\auth\emp dir = pkg.replace(".", "/"); } }

  

转载地址:http://gywuo.baihongyu.com/

你可能感兴趣的文章
python 生成器
查看>>
HTTPS(SSL)详解以及PHP调用方法
查看>>
突发小事件,USB接口问题
查看>>
Nginx负载均衡配置实例详解
查看>>
L1-009. N个数求和
查看>>
实参传递不当导致的运行时错误
查看>>
PHP生成静态html文件 的三种方法
查看>>
sqlserver 批量删除存储过程(转)
查看>>
自建型呼叫中心
查看>>
Inno setup中定制安装路径
查看>>
要懂得对你的老板好一点!
查看>>
HDU5139:Formula(找规律+离线处理)
查看>>
visio如何让动态连接线的单箭头变成双箭头?
查看>>
poj 1273 Drainage Ditches 网络流最大流基础
查看>>
Bash: how to check if a process id (PID) exists
查看>>
Mirantis Fuel fundations
查看>>
启动Tomcat一闪而过——分析及解决过程
查看>>
Android intent action大全
查看>>
使用 Flash Builder 的 Apple iOS 开发过程
查看>>
Android OpenGL ES(一)OpenGL ES介绍
查看>>