代码生成器小例
Posted mr-dxin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码生成器小例相关的知识,希望对你有一定的参考价值。
1.创建模板文件(想要有代码生成器,就得有模板!)
在自己创建的包内创建file文件作为模板文件。
例如:生成Dao的模板文件:DemoDao.tlf(templatefile)
package cn.sxx.dao; import cn.sxx.model.Demo; import cn.sxx.query.DemoQuery; public interface DemoDao extends BaseDao<Demo, DemoQuery>
2.代码生成器的创建
在自己的定义的包内创建代码生成器。
例如:生成xxxDao.jave所用的代码生成器
/** * 生成Dao * @param ClassName * @throws Exception */ public static void generDao(String ClassName) throws Exception
//从模板文件所在的位置读取文件内容 BufferedReader br = new BufferedReader(new FileReader("config/cn/sxx/template/DemoDao.tlf"));
//把读取的文件内容写入到指定的xxxDao.jave中 BufferedWriter bw = new BufferedWriter(new FileWriter("src/cn/sxx/dao/"+ClassName+"Dao.java")); String line = null; String newLine= null; while((line = br.readLine()) != null) //把读取的模板里的Demo替换成类名 newLine = line.replace("Demo", ClassName); bw.write(newLine); bw.newLine(); bw.flush(); bw.close(); br.close();
最后,用于生成query,dao,service,以及applicationcontext-dao/service.xml文件的整体代码
package cn.sxx.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class SourceGenerator public static void main(String[] args) throws Exception generator("ProductType"); public static void generator(String className) throws Exception generQuery(className); generDao(className); generDaoImpl(className); generService(className); generServiceImpl(className); generDaoConfig(className); generServiceConfig(className); /** * 生成查询对象 * @param ClassName * @throws Exception */ public static void generQuery(String ClassName) throws Exception BufferedReader br = new BufferedReader(new FileReader("config/cn/sxx/template/DemoQuery.tlf")); BufferedWriter bw = new BufferedWriter(new FileWriter("src/cn/sxx/query/"+ClassName+"Query.java")); String line = null; String newLine= null; while((line = br.readLine()) != null) //把读取的模板里的Demo替换成类名 newLine = line.replace("Demo", ClassName); bw.write(newLine); bw.newLine(); bw.flush(); bw.close(); br.close(); /** * 生成Dao * @param ClassName * @throws Exception */ public static void generDao(String ClassName) throws Exception BufferedReader br = new BufferedReader(new FileReader("config/cn/sxx/template/DemoDao.tlf")); BufferedWriter bw = new BufferedWriter(new FileWriter("src/cn/sxx/dao/"+ClassName+"Dao.java")); String line = null; String newLine= null; while((line = br.readLine()) != null) //把读取的模板里的Demo替换成类名 newLine = line.replace("Demo", ClassName); bw.write(newLine); bw.newLine(); bw.flush(); bw.close(); br.close(); /** * 生成DaoImpl * @param ClassName * @throws Exception */ public static void generDaoImpl(String ClassName) throws Exception BufferedReader br = new BufferedReader(new FileReader("config/cn/sxx/template/DemoDaoImpl.tlf")); BufferedWriter bw = new BufferedWriter(new FileWriter("src/cn/sxx/dao/impl/"+ClassName+"DaoImpl.java")); String line = null; String newLine= null; while((line = br.readLine()) != null) //把读取的模板里的Demo替换成类名 newLine = line.replace("Demo", ClassName); bw.write(newLine); bw.newLine(); bw.flush(); bw.close(); br.close(); /** * 生成DaoImpl * @param ClassName * @throws Exception */ public static void generService(String ClassName) throws Exception BufferedReader br = new BufferedReader(new FileReader("config/cn/sxx/template/DemoService.tlf")); BufferedWriter bw = new BufferedWriter(new FileWriter("src/cn/sxx/service/"+ClassName+"Service.java")); String line = null; String newLine= null; while((line = br.readLine()) != null) //把读取的模板里的Demo替换成类名 newLine = line.replace("Demo", ClassName); bw.write(newLine); bw.newLine(); bw.flush(); bw.close(); br.close(); /** * 生成ServiceImpl:要考虑字母大小写的问题 * @param ClassName * @throws Exception */ public static void generServiceImpl(String ClassName) throws Exception //获得小写的类型 String lowerCaseClassName = ClassName.substring(0,1).toLowerCase() + ClassName.substring(1); BufferedReader br = new BufferedReader(new FileReader("config/cn/sxx/template/DemoServiceImpl.tlf")); BufferedWriter bw = new BufferedWriter(new FileWriter("src/cn/sxx/service/impl/"+ClassName+"ServiceImpl.java")); String line = null; String newLine= null; while((line = br.readLine()) != null) //把读取的模板里的Demo替换成类名 newLine = line.replace("Demo", ClassName).replace("demo", lowerCaseClassName); bw.write(newLine); bw.newLine(); bw.flush(); bw.close(); br.close(); /** * 生成DaoConfig:这里用dom4j * @param ClassName * @throws Exception */ public static void generDaoConfig(String ClassName) throws Exception //获得小写的类型 String lowerCaseClassName = ClassName.substring(0,1).toLowerCase() + ClassName.substring(1); SAXReader reader = new SAXReader(); Document doc = reader.read(new FileReader("config/ApplicationContext-dao.xml")); Element rootElement = doc.getRootElement(); Element newEle = rootElement.addElement("bean") .addAttribute("id", lowerCaseClassName+"Dao") .addAttribute("class", "cn.sxx.dao.impl."+ClassName+"DaoImpl"); newEle.addElement("property") .addAttribute("name", "sessionFactory") .addAttribute("ref", "sessionFactory"); XMLWriter writer = new XMLWriter(new FileWriter("config/ApplicationContext-dao.xml"),OutputFormat.createPrettyPrint()); writer.write(doc); writer.close(); /** * 生成ServiceConfig:这里用dom4j * @param ClassName * @throws Exception */ public static void generServiceConfig(String ClassName) throws Exception //获得小写的类型 String lowerCaseClassName = ClassName.substring(0,1).toLowerCase() + ClassName.substring(1); SAXReader reader = new SAXReader(); Document doc = reader.read(new FileReader("config/ApplicationContext-service.xml")); Element rootElement = doc.getRootElement(); Element newEle = rootElement.addElement("bean") .addAttribute("id", lowerCaseClassName+"Service") .addAttribute("class", "cn.sxx.service.impl."+ClassName+"ServiceImpl"); newEle.addElement("property") .addAttribute("name", lowerCaseClassName+"Dao") .addAttribute("ref", lowerCaseClassName+"Dao"); XMLWriter writer = new XMLWriter(new FileWriter("config/ApplicationContext-service.xml"),OutputFormat.createPrettyPrint()); writer.write(doc); writer.close();
以上是关于代码生成器小例的主要内容,如果未能解决你的问题,请参考以下文章