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("<?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=\""+small+"Action\" class=\""+pkg+".web."+big+"Action\" scope=\"prototype\">");
		bw.newLine();

		bw.write("		<property name=\""+small+"Ebi\" ref=\""+small+"Ebi\"/>");
		bw.newLine();

		bw.write("	</bean>");
		bw.newLine();

		bw.newLine();

		bw.write("	<!-- Ebi -->");
		bw.newLine();

		bw.write("	<bean id=\""+small+"Ebi\" class=\""+pkg+".business.ebo."+big+"Ebo\">");
		bw.newLine();

		bw.write("		<property name=\""+small+"Dao\" ref=\""+small+"Dao\"/>");
		bw.newLine();

		bw.write("	</bean>");
		bw.newLine();

		bw.newLine();

		bw.write("	<!-- Dao -->");
		bw.newLine();

		bw.write("	<bean id=\""+small+"Dao\" class=\""+pkg+".dao.impl."+big+"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();
	}

	// 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("<?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."+big+"Model\" table=\"tbl_"+small+"\">");
		bw.newLine();

		bw.write("        <id name=\"uuid\">");
		bw.newLine();

		bw.write("            <generator class=\"native\" />");
		bw.newLine();

		bw.write("        </id>");
		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("        <property name=\""+fd.getName()+"\"/>");
						bw.newLine();
					}
				}
			}
		}

		bw.newLine();

		bw.write("        <!-- 关系 -->");
		bw.newLine();

		bw.newLine();

		bw.write("    </class>");
		bw.newLine();

		bw.write("</hibernate-mapping>");
		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(".", "/");
	}

}

  

[需再总结]SSH整合代码生成器的更多相关文章

  1. SSH框架整合配置所需JAR包(SSH整合)

    转载于:http://www.cnblogs.com/kaige123/p/5719662.html Hibernate Jar: 1.hibernate3.jar,这个是hibernate3.0的核 ...

  2. SSH整合框架+mysql简单的实现

    SSH整合框架+mysql简单的实现 1. 框架整合原理: struts2整合Spring 两种: 一种struts2自己创建Action,自动装配Service : 一种 将Action交给Spri ...

  3. 三大框架SSH整合

    三大框架SSH整合 -------------------------------Spring整合Hibernate------------------------------- 一.为什么要整合Hi ...

  4. 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】

    一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...

  5. SSH整合(1)异常

    信息: No Spring WebApplicationInitializer types detected on classpath 十二月 01, 2016 10:06:12 下午 org.apa ...

  6. 【转载】SSH整合使用步骤

    SSH整合使用步骤 由于刚开始学习SSH,其中的配置比较多,为了下次能够快速的进行配置,将SSH整合的过程记录下来,以便下次查阅. 软件环境:MyEclipse 9.0.Struts2.2.Sprin ...

  7. SSH整合方案2

    [案例3]SSH整合_方案2 **  案例描述  两个知识点的演示  其一,SSH整合的第二个方案  其二,Spring+JDBC+Struts2  参考代码  31) 使用工程spring4  32 ...

  8. mysql+ssh整合样例,附源代码下载

    项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源代码下载地址:http://download.csdn.net/detail/ ...

  9. SSH整合总结(xml与注解)

    本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 一.使用XML配置: SSH版本 Struts-2.3.31 Spring-4.3.5 Hibernate-4.2.21 引入ja ...

随机推荐

  1. DelphiXE7中创建WebService(服务端+客户端)

    相关资料: http://www.2ccc.com/news/Html/?1507.html http://www.dfwlt.com/forum.php?mod=viewthread&tid ...

  2. ACM OJ Collection

    浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 同济大学(TJU):http://acm.t ...

  3. 随便看看My97DatePicker源码J方法

    如果有一个路径是写错的并且这个路径是写在前面,那么相关的css文件你就别想引进来了 <script language="javascript" type="text ...

  4. [iOS 多线程 & 网络 - 4.0] - AFN框架简单使用

    A.AFN基本知识 1.概念 AFNetworking 是对NSURLConnection的封装 运行效率没有ASI高(因为ASI基于CFNetwork),但是使用简单 AFN支持ARC     B. ...

  5. linux系统中如何查看日志 (常用命令)

    cat tail -f 日 志 文 件 说 明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 /var/log/secure 与安全相关 ...

  6. HDU 3687 National Day Parade (暴力)

    题意:给定 n 个人,在 n 列,问你移动最少的距离,使得他们形成一个n*n的矩阵. 析:这个题本来是要找中位数的,但是有特殊情况,所以改成暴力了,时间也很短,就是从第一个能够放左角的位置开始找,取最 ...

  7. Ubuntu12.04 使用中遇到的问题

    这个随笔回记录使用Ubuntu遇到的一些问题   不定期进行整理和分类 1.Question:   ubuntu 无法检测包或者源码包 Description:Ubuntu软件中心打开时报错  无法检 ...

  8. ecshop以幻灯版调用首页主广告显示

    默认的是index_ad.lbi模板有一个$flash变量了,但在搜索搜索没发现 <!--{foreach from=$flash name=no item=flash}--> <l ...

  9. c++地址对齐

    在一些计算机上,由于性能方面的原因,2个字节的变量,必须放在2的倍数的地址中,4个字节的,就必须放在4的倍数中的地址中,以此类推. 也就是说,如果有3个占用两个字节的变量,分别为:A,B,C,如下图: ...

  10. MFC拆分窗口及它们之间的数据交换(转)

    转自:http://blog.csdn.net/nuptboyzhb/article/details/7455471 源代码:http://download.csdn.net/detail/nuptb ...