代码生成器,极大简化你的工作

代码生成器,极大简化你的工作

        在web开发中,模块化开发非常普遍。在最近的毕业设计中,自己基于spring mvc + mybatics + liger ui开发了一个网站,里面也使用模块化来组织代码。于是每一个模块的操作,基本上都是大同小异,无非就是增删改查。下面先观察下代码的包组织结构,以及jsp文件组织结构:如下图所示。
代码生成器,极大简化你的工作_第1张图片代码生成器,极大简化你的工作_第2张图片
        从图中可以看出,一个模块通常情况下也会有一个相应的jsp文件夹。这样子给代码生成器的开发埋下了伏笔。
        继续观察代码组织结构,我们通常在模块下面又使用dao层,service层,controller层,entity层来组织代码。其中dao层用于访问数据库;service层属于dao层的门面设计模式,通常可以根据业务逻辑需要,调用多个dao层来完成任务;controller层则是controller类,通常由Add、delete、update、list四个controller类,每个类只有一个核心功能;entity层则是实体类,在这个工程里面,将mybatics的mapper类放在这里。下面我们来查看下一个模块的代码组织,如下图所示。
代码生成器,极大简化你的工作_第3张图片
        从图中可以看出,代码命名规则非常的明确,这样子的命名规则,可以很方便的使用代码生成器来生成这些代码。
        为了开发方便,于是花了点时间,写了个代码生成器。该代码生成器的主要目标就是用户给出实体类,则程序自动生成controller、dao、service、mapper、jsp等模板文件。开发者只需要根据业务逻辑需要,将sql放入到mapper中,就能快速完成新模块功能的开发。
        类的开发比较简单,主要就是得到工程所在路径,根据给出entity实体,自动生成dao、service、controller、mapper、jsp等文件。由于该类没什么技术难度,就直接贴代码了。
<span style="font-size:18px;">package net.itaem.autogeneratecode;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import net.itaem.department.entity.Department;

/**
 * 自动代码生成器
 * 1、代码生成器需要做的功能:
 * 给出Entity,生成Mybatics Mapper,生成成Dao层,service层,以及controller层,以及jsp文件
 * 
 * 生成的这些文件中,全部都是骨架代码,开发时需要根据具体要求添加业务逻辑代码即可
 * @author luohong [email protected] 15013336884
 * */
public class AutomaticGenerateCode {
    
	public static void main(String[] args) throws IOException{
		generateMybaticsMapper(Department.class);
		generateDao(Department.class);
		generateService(Department.class);
		generateController(Department.class);
		generateJsp(Department.class);	
	}

	/**
	 * 生成mybatics的mapper类
	 * @param clazz
	 * */
	public static void generateMybaticsMapper(Class<?> clazz) throws IOException{
		String path = path(clazz);
		String mapperDirectoryName = path.substring(0, path.lastIndexOf("\\")) + File.separator + "entity";
		File mapperDirectory = new File(mapperDirectoryName);
		if(!mapperDirectory.exists()){
			mapperDirectory.mkdir();
		}

		String mapperJavaFileName = mapperDirectory.getCanonicalPath() + File.separator + clazz.getSimpleName() + "Mapper.java";
		File mapperJavaFile = new File(mapperJavaFileName);
		StringBuilder contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import java.util.*;\n");
		contentBuilder.append("import org.apache.ibatis.annotations.Insert;\n");
		contentBuilder.append("import org.apache.ibatis.annotations.Select;\n");
		contentBuilder.append("import org.apache.ibatis.annotations.Results;\n");
		contentBuilder.append("import org.springframework.stereotype.Repository;\n");
		contentBuilder.append("import org.apache.ibatis.annotations.Update;\n");
		contentBuilder.append("\n\n");
		contentBuilder.append("");

		contentBuilder.append("@Repository(value = \"" + clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) + "Mapper\"" + ")\n");
		contentBuilder.append("public interface " + clazz.getSimpleName() + "Mapper"+"{\n\n");
		contentBuilder.append("    @Select(value = \"请替换成您需要的sql\")\n");
		contentBuilder.append("    @Results(value = {})\n");
		contentBuilder.append("    public List<"+clazz.getSimpleName()+"> listAll();");
		contentBuilder.append("\n\n");

		contentBuilder.append("    @Select(value = \"请替换成您需要的sql\")\n");
		contentBuilder.append("    @Results(value = {})\n");
		contentBuilder.append("    public "+clazz.getSimpleName()+" listBy(String id);");
		contentBuilder.append("\n\n");

		contentBuilder.append("    @Insert(\"\")\n");
		contentBuilder.append("    public void add(" + clazz.getSimpleName().substring(0,1).toUpperCase() + clazz.getSimpleName().substring(1) + " " + clazz.getSimpleName() + ");");
		contentBuilder.append("\n\n");

		contentBuilder.append("    @Update(\"\")\n");
		contentBuilder.append("    public void delete(String id);");
		contentBuilder.append("\n\n");

		contentBuilder.append("    @Update(\"\")\n");
		contentBuilder.append("    public void update(" + clazz.getSimpleName().substring(0,1).toUpperCase() + clazz.getSimpleName().substring(1) + " " + clazz.getSimpleName() + ");");
		contentBuilder.append("\n}");
		println(mapperJavaFile, contentBuilder.toString());
	}

	/**
	 * 生成Dao代码
	 * @param clazz
	 * */
	public static void generateDao(Class<?> clazz) throws IOException{
		generateDaoInterface(clazz);
		generateDaoImplement(clazz);	
	}

	private static void generateDaoInterface(Class<?> clazz) throws IOException{
		String path = path(clazz);
		String daoDirectoryName = path.substring(0, path.lastIndexOf("\\")) + File.separator + "dao";
		File daoDirectory = new File(daoDirectoryName);
		if(!daoDirectory.exists()){
			daoDirectory.mkdir();
		}
		String daoJavaFileName = daoDirectory.getCanonicalPath() + File.separator + "I" + clazz.getSimpleName() + "Dao.java";
		File daoJavaFile = new File(daoJavaFileName);
		StringBuilder contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".dao;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import java.util.*;\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("\n\n");
		contentBuilder.append("");
		contentBuilder.append("public interface "+"I" + clazz.getSimpleName() + "Dao"+"{\n\n");
		contentBuilder.append("    public List<"+clazz.getSimpleName()+"> listAll();");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public "+clazz.getSimpleName()+" listBy(String id);");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public void add(" + clazz.getSimpleName().substring(0,1).toUpperCase() + clazz.getSimpleName().substring(1) + " " + clazz.getSimpleName() + ");");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public void delete(String id);");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public void update(" + clazz.getSimpleName().substring(0,1).toUpperCase() + clazz.getSimpleName().substring(1) + " " + clazz.getSimpleName() + ");");
		contentBuilder.append("\n}");
		println(daoJavaFile, contentBuilder.toString());
	}

	private static void generateDaoImplement(Class<?> clazz) throws IOException {
		String path = path(clazz);
		String daoDirectoryName = path.substring(0, path.lastIndexOf("\\")) + File.separator + "dao" + File.separator + "impl";

		File daoDirectory = new File(daoDirectoryName);
		if(!daoDirectory.exists()){
			daoDirectory.mkdir();
		}

		String daoJavaFileName = daoDirectory.getCanonicalPath() + File.separator + clazz.getSimpleName() + "DaoImpl.java";

		File daoJavaFile = new File(daoJavaFileName);
		StringBuilder contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".dao.impl;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import java.util.*;\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".dao.I" + clazz.getSimpleName() + "Dao" + ";\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + "Mapper" + ";\n");

		contentBuilder.append("import org.springframework.stereotype.Repository;\n");
		contentBuilder.append("import javax.annotation.Resource;\n");

		contentBuilder.append("\n\n");
		contentBuilder.append("");

		contentBuilder.append("@Repository\n");
		contentBuilder.append("public class "+ clazz.getSimpleName() + "DaoImpl implements I" + clazz.getSimpleName() + "Dao" +"{\n\n");

		String mapperName = clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) + "Mapper";
		contentBuilder.append("    @Resource(name = \"" + mapperName + "\")\n");
		contentBuilder.append("    private " + clazz.getSimpleName() + "Mapper " + mapperName + ";\n\n");
		contentBuilder.append("    public List<"+clazz.getSimpleName()+"> listAll(){\n");
		contentBuilder.append("        return " + mapperName+ ".listAll()" + ";");
		contentBuilder.append("\n    }\n\n");

		contentBuilder.append("    public "+clazz.getSimpleName()+" listBy(String id){\n");
		contentBuilder.append("        return " + mapperName + ".listBy(id)" + ";");
		contentBuilder.append("\n    }\n\n");

		contentBuilder.append("    public void add(" + clazz.getSimpleName() + " " + clazz.getSimpleName().substring(0,1).toLowerCase() + clazz.getSimpleName().substring(1) + "){\n");
		contentBuilder.append("        " + mapperName + ".add(" + clazz.getSimpleName().substring(0,1).toLowerCase() + clazz.getSimpleName().substring(1) + ");" + "");
		contentBuilder.append("\n    }\n\n");

		contentBuilder.append("    public void delete(String id){\n");
		contentBuilder.append("        " + mapperName + ".delete(id);");
		contentBuilder.append("\n    }\n\n");

		contentBuilder.append("    public void update(" + clazz.getSimpleName() + " " + clazz.getSimpleName().substring(0,1).toLowerCase() + clazz.getSimpleName().substring(1) + "){\n");
		contentBuilder.append("        " + mapperName + ".update(" + clazz.getSimpleName().substring(0,1).toLowerCase() + clazz.getSimpleName().substring(1) + ");" + "");
		contentBuilder.append("\n    }\n");
		contentBuilder.append("}");
		println(daoJavaFile, contentBuilder.toString());
	}

	/**
	 * 生成Service代码
	 * @param clazz
	 * */
	public static void generateService(Class<?> clazz) throws IOException{
		generateServiceInterface(clazz);
		generateServiceImplement(clazz);	
	}

	private static void generateServiceInterface(Class<?> clazz) throws IOException{
		String path = path(clazz);
		String serviceDirectoryName = path.substring(0, path.lastIndexOf("\\")) + File.separator + "service";
		File serviceDirectory = new File(serviceDirectoryName);
		if(!serviceDirectory.exists()){
			serviceDirectory.mkdir();
		}
		String serviceJavaFileName = serviceDirectory.getCanonicalPath() + File.separator + "I" + clazz.getSimpleName() + "Service.java";
		File serviceJavaFile = new File(serviceJavaFileName);
		StringBuilder contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".service;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import java.util.*;\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("\n\n");
		contentBuilder.append("");
		contentBuilder.append("public interface "+"I" + clazz.getSimpleName() + "Service"+"{\n\n");
		contentBuilder.append("    public List<"+clazz.getSimpleName()+"> listAll();");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public "+clazz.getSimpleName()+" listBy(String id);");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public void add(" + clazz.getSimpleName().substring(0,1).toUpperCase() + clazz.getSimpleName().substring(1) + " " + clazz.getSimpleName() + ");");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public void delete(String id);");
		contentBuilder.append("\n\n");
		contentBuilder.append("    public void update(" + clazz.getSimpleName().substring(0,1).toUpperCase() + clazz.getSimpleName().substring(1) + " " + clazz.getSimpleName() + ");");
		contentBuilder.append("\n}");
		println(serviceJavaFile, contentBuilder.toString());
	}

	private static void generateServiceImplement(Class<?> clazz) throws IOException {
		String path = path(clazz);
		String serviceDirectoryName = path.substring(0, path.lastIndexOf("\\")) + File.separator + "service" + File.separator + "impl";

		File serviceDirectory = new File(serviceDirectoryName);
		if(!serviceDirectory.exists()){
			serviceDirectory.mkdir();
		}

		String serviceJavaFileName = serviceDirectory.getCanonicalPath() + File.separator + clazz.getSimpleName() + "ServiceImpl.java";

		File daoJavaFile = new File(serviceJavaFileName);
		StringBuilder contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".service.impl;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import java.util.*;\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".service.I" + clazz.getSimpleName()+ "Service" + ";\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".dao.I" + clazz.getSimpleName()+ "Dao" + ";\n");
		contentBuilder.append("import org.springframework.stereotype.Service;\n");
		contentBuilder.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		contentBuilder.append("\n\n");

		contentBuilder.append("@Service\n");
		contentBuilder.append("public class "+ clazz.getSimpleName() + "ServiceImpl implements I" + clazz.getSimpleName() + "Service" + "{\n\n");
		String daoName = clazz.getSimpleName().substring(0,1).toLowerCase() + clazz.getSimpleName().substring(1) + "Dao";
		contentBuilder.append("    @Autowired\n");
		contentBuilder.append("    private I" + clazz.getSimpleName() + "Dao " + daoName + ";\n\n");

		contentBuilder.append("    public List<"+clazz.getSimpleName()+"> listAll(){\n");
		contentBuilder.append("        return " + daoName + ".listAll()" + ";");
		contentBuilder.append("\n    }\n\n");

		contentBuilder.append("    public "+clazz.getSimpleName()+" listBy(String id){\n");
		contentBuilder.append("        return " + daoName + ".listBy(id)" + ";");
		contentBuilder.append("\n    }\n\n");

		String arg = clazz.getSimpleName().substring(0,1).toLowerCase() + clazz.getSimpleName().substring(1);
		contentBuilder.append("    public void add(" + clazz.getSimpleName() + " " + arg + "){\n");
		contentBuilder.append("        " + daoName + ".add(" + arg + ");");
		contentBuilder.append("\n    }\n\n");
		contentBuilder.append("    public void delete(String id){\n");
		contentBuilder.append("       "+daoName +".delete(id);");
		contentBuilder.append("\n    }\n\n");
		contentBuilder.append("    public void update(" + clazz.getSimpleName() + " " + arg + "){\n");
		contentBuilder.append("        "+daoName+".update(" + arg + ");");
		contentBuilder.append("\n    }\n");
		contentBuilder.append("}");
		println(daoJavaFile, contentBuilder.toString());
	}

	/**
	 * 生成controller代码
	 * @param clazz
	 * */
	public static void generateController(Class<?> clazz) throws IOException{
		String path = path(clazz);
		String controllerDirectoryName = path.substring(0, path.lastIndexOf("\\")) + File.separator + "controller";
		File controllerDirectory = new File(controllerDirectoryName);
		if(!controllerDirectory.exists()){
			controllerDirectory.mkdir();
		}

		//generate add controller
		String controllerJavaFileName = controllerDirectory.getCanonicalPath() + File.separator + clazz.getSimpleName() + "AddController.java";
		File controllerJavaFile = new File(controllerJavaFileName);
		StringBuilder contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".controller;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("import net.itaem."+ clazz.getSimpleName().toLowerCase() + ".service.I" + clazz.getSimpleName() + "Service;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletRequest;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletResponse;\n");
		contentBuilder.append("import org.springframework.stereotype.Controller;");
		contentBuilder.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		contentBuilder.append("import org.springframework.web.bind.annotation.RequestMapping;\n");

		contentBuilder.append("\n\n");
		contentBuilder.append("");
		contentBuilder.append("@Controller\n");
		contentBuilder.append("public class "+ clazz.getSimpleName() + "AddController"+"{\n\n");
		String serviceName = clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) + "Service";
		contentBuilder.append("    @Autowired\n");
		contentBuilder.append("    private I" + clazz.getSimpleName() + "Service " + serviceName + ";\n\n");

		contentBuilder.append("    @RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "/add.do\")\n");
		contentBuilder.append("    public String add(HttpServletRequest req){\n");
		contentBuilder.append("        return \"" + clazz.getSimpleName().toLowerCase() + "/add\";\n");
		contentBuilder.append("    }\n\n");

		contentBuilder.append("    @RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "/addSubmit.do\")\n");
		contentBuilder.append("    public void addSubmit(" + clazz.getSimpleName() + " " + clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) + ", HttpServletRequest req, HttpServletResponse resp){\n");
		contentBuilder.append("    }\n");

		contentBuilder.append("\n}");
		println(controllerJavaFile, contentBuilder.toString());

		//clear content builder;
		contentBuilder = new StringBuilder();

		//generate delete controller
		controllerJavaFileName = controllerDirectory.getCanonicalPath() + File.separator + clazz.getSimpleName() + "DeleteController.java";
		controllerJavaFile = new File(controllerJavaFileName);
		contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".controller;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("import net.itaem."+ clazz.getSimpleName().toLowerCase() + ".service.I" + clazz.getSimpleName() + "Service;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletRequest;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletResponse;\n");
		contentBuilder.append("import org.springframework.stereotype.Controller;");
		contentBuilder.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		contentBuilder.append("import org.springframework.web.bind.annotation.RequestMapping;\n");

		contentBuilder.append("\n\n");
		contentBuilder.append("");
		contentBuilder.append("@Controller\n");
		contentBuilder.append("public class "+ clazz.getSimpleName() + "DeleteController"+"{\n\n");
		contentBuilder.append("    @Autowired\n");
		contentBuilder.append("    private I" + clazz.getSimpleName() + "Service " + serviceName + ";\n\n");

		contentBuilder.append("    @RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "/delete.do\")\n");
		contentBuilder.append("    public void delete(HttpServletResponse resp){\n");
		contentBuilder.append("    }\n\n");

		contentBuilder.append("\n}");
		println(controllerJavaFile, contentBuilder.toString());

		//clear content builder;
		contentBuilder = new StringBuilder();

		//generate update controller
		controllerJavaFileName = controllerDirectory.getCanonicalPath() + File.separator + clazz.getSimpleName() + "UpdateController.java";
		controllerJavaFile = new File(controllerJavaFileName);
		contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".controller;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("import net.itaem."+ clazz.getSimpleName().toLowerCase() + ".service.I" + clazz.getSimpleName() + "Service;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletRequest;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletResponse;\n");
		contentBuilder.append("import org.springframework.stereotype.Controller;");
		contentBuilder.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		contentBuilder.append("import org.springframework.web.bind.annotation.RequestMapping;\n");

		contentBuilder.append("\n\n");
		contentBuilder.append("");
		contentBuilder.append("@Controller\n");
		contentBuilder.append("public class "+ clazz.getSimpleName() + "UpdateController"+"{\n\n");
		contentBuilder.append("    @Autowired\n");
		contentBuilder.append("    private I" + clazz.getSimpleName() + "Service " + serviceName + ";\n\n");

		contentBuilder.append("    @RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "/update.do\")\n");
		contentBuilder.append("    public String update(HttpServletRequest req){\n");
		contentBuilder.append("        return \"" + clazz.getSimpleName().toLowerCase() + "/update\";\n");
		contentBuilder.append("    }\n\n");

		contentBuilder.append("    @RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "/updateSubmit.do\")\n");
		contentBuilder.append("    public void updateSubmit(" + clazz.getSimpleName() + " " + clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) + ", HttpServletRequest req, HttpServletResponse resp){\n");
		contentBuilder.append("    }\n");

		contentBuilder.append("\n}");
		println(controllerJavaFile, contentBuilder.toString());

		//clear content builder;
		contentBuilder = new StringBuilder();

		//generate list controller
		controllerJavaFileName = controllerDirectory.getCanonicalPath() + File.separator + clazz.getSimpleName() + "ListController.java";
		controllerJavaFile = new File(controllerJavaFileName);
		contentBuilder = new StringBuilder();
		contentBuilder.append("package net.itaem." + clazz.getSimpleName().toLowerCase() + ".controller;");
		contentBuilder.append("\n\n");
		contentBuilder.append("import net.itaem." + clazz.getSimpleName().toLowerCase() + ".entity." + clazz.getSimpleName() + ";\n");
		contentBuilder.append("import net.itaem."+ clazz.getSimpleName().toLowerCase() + ".service.I" + clazz.getSimpleName() + "Service;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletRequest;\n");
		contentBuilder.append("import javax.servlet.http.HttpServletResponse;\n");
		contentBuilder.append("import org.springframework.stereotype.Controller;");
		contentBuilder.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		contentBuilder.append("import org.springframework.web.bind.annotation.RequestMapping;\n");
		contentBuilder.append("\n\n");
		contentBuilder.append("");
		contentBuilder.append("@Controller\n");
		contentBuilder.append("public class "+ clazz.getSimpleName() + "ListController"+"{\n\n");
		contentBuilder.append("    @Autowired\n");
		contentBuilder.append("    private I" + clazz.getSimpleName() + "Service " + serviceName + ";\n\n");

		contentBuilder.append("    @RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "/list.do\")\n");
		contentBuilder.append("    public String list(HttpServletRequest req){\n");
		contentBuilder.append("        return \"" + clazz.getSimpleName().toLowerCase() + "/list\";\n");
		contentBuilder.append("    }\n\n");

		contentBuilder.append("    @RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "/listJson.do\")\n");
		contentBuilder.append("    public void listJson(HttpServletResponse resp){\n");
		contentBuilder.append("    }\n\n");

		contentBuilder.append("\n}");
		println(controllerJavaFile, contentBuilder.toString());
	}

	/**
	 * 生成jsp界面,这里面一共有三个jsp,分别是add.jsp, update.jsp, list.jsp
	 * */
	public static void generateJsp(Class<?> clazz) throws IOException{
		File directory = new File("");// 参数为空
		String courseFile = directory.getCanonicalPath();
		String jspRoot = courseFile + File.separator + "WebContent" + File.separator + "WEB-INF" + File.separator + "jsp";
		String newJspPath = jspRoot + File.separator + clazz.getSimpleName().toLowerCase();
		File newJspDirectory = new File(newJspPath);
		if(!newJspDirectory.exists()){
			newJspDirectory.mkdir();
		}

		String patternPath = jspRoot + File.separator + "pattern";

		//将pattern下面的模板jsp文件复制到这个新建立的文件夹中
		copyJsp(patternPath, newJspPath);
	}

	/**
	 * copy置顶文件夹下面的文件夹到新文件夹中
	 * @param src
	 * @param dest
	 * */
	private static void copyJsp(String src, String dest){
		File srcFile = new File(src);
		if(srcFile.exists()){
			File[] files = srcFile.listFiles();
			for(File from: files){
				try {   
					String to = dest + File.separator + from.getName();
					File toFile = new File(to);
					//如果该文件存在,不要进行复制
					if(toFile.exists()){
						continue;
					}
					
					InputStream in = new FileInputStream(from);   
					OutputStream out = new FileOutputStream(to);   

					byte[] buff = new byte[1024];   
					int len = 0;   
					while ((len = in.read(buff)) != -1) {   
						out.write(buff, 0, len);   
					}   
					in.close();   
					out.close();   
				} catch (FileNotFoundException e) {   
					e.printStackTrace();   
				} catch (IOException e) {   
					e.printStackTrace();   
				}   
			}
		}
	}
	/**
	 * 获取该类的文件夹所在位置
	 * @param clazz 
	 * @return 获取该类的文件夹所在位置 
	 * */
	public static String path(Class<?> clazz) throws IOException{
		File directory = new File("");// 参数为空
		String courseFile = directory.getCanonicalPath();		
		String packageName = clazz.getPackage().getName();
		String newPackageName = courseFile + File.separator + "src" + File.separator + packageName.replace(".", File.separator);
		return newPackageName;	
	}

	public static void println(File file, String content){
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(file);
			writer.write(content);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			writer.close();
		}
	}
}</span>

        
        下面我们观察下生成代码的样式。这里面给出一个实体Department,生成了如下图所示的代码以及jsp文件。
代码生成器,极大简化你的工作_第4张图片代码生成器,极大简化你的工作_第5张图片
        下面是生成后的代码样式,这里面生成的代码木有注释,如果大家觉得有必要,可以自己添加相关注释。
package net.itaem.department.controller;

import net.itaem.department.entity.Department;
import net.itaem.department.service.IDepartmentService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class DepartmentAddController{

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/department/add.do")
    public String add(HttpServletRequest req){
        return "department/add";
    }

    @RequestMapping("/department/addSubmit.do")
    public void addSubmit(Department department, HttpServletRequest req, HttpServletResponse resp){
    }

}
package net.itaem.department.service;

import java.util.*;
import net.itaem.department.entity.Department;


public interface IDepartmentService{

    public List<Department> listAll();

    public Department listBy(String id);

    public void add(Department Department);

    public void delete(String id);

    public void update(Department Department);
}

package net.itaem.department.dao;

import java.util.*;
import net.itaem.department.entity.Department;


public interface IDepartmentDao{

    public List<Department> listAll();

    public Department listBy(String id);

    public void add(Department Department);

    public void delete(String id);

    public void update(Department Department);
}
package net.itaem.department.entity;

import java.util.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Update;


@Repository(value = "departmentMapper")
public interface DepartmentMapper{

    @Select(value = "请替换成您需要的sql")
    @Results(value = {})
    public List<Department> listAll();

    @Select(value = "请替换成您需要的sql")
    @Results(value = {})
    public Department listBy(String id);

    @Insert("")
    public void add(Department Department);

    @Update("")
    public void delete(String id);

    @Update("")
    public void update(Department Department);
}

总结:开发一个代码生成器的难度并不大,关键在于工程的组织能够方便代码生成器生成代码。



你可能感兴趣的:(java,web开发,模块化,毕业设计)