项目结构:
这里要实现的功能是,当我们给出了bean,如:Admin,User,People等实体类后,
我想用代码自动生成我想要的代码,最后生成的效果:
也就是说为每一个bean都生成相应的Dao,DaoImpl,Service,ServiceImpl等类。
后台运行效果:
下面是列出自动生成User的相关文件:
UseDao
、
UserDaoImpl
UserService
UserServiceImpl
=================================================
代码部分:
=================================================
/UUUU_Web_Test/src/com/b510/base/bean/install/BeanUtils.java
1 /** 2 * 3 */ 4 package com.b510.base.bean.install; 5 6 import java.io.File; 7 import java.io.FileWriter; 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 10 11 12 /** 13 * @author hongten() 14 * @date 2013-2-24 15 */ 16 @SuppressWarnings("unchecked") 17 public class BeanUtils { 18 19 20 21 //公共部分 22 private static final String RT_1 = "\r\n"; 23 private static final String RT_2 = RT_1+RT_1; 24 private static final String BLANK_1 =" "; 25 private static final String BLANK_4 =" "; 26 private static final String BLANK_8 =BLANK_4 + BLANK_4; 27 28 29 30 //注释部分 31 private static final String ANNOTATION_AUTHOR_PARAMTER = "@author "; 32 private static final String ANNOTATION_AUTHOR_NAME = "hongten()"; 33 private static final String ANNOTATION_AUTHOR = ANNOTATION_AUTHOR_PARAMTER + ANNOTATION_AUTHOR_NAME; 34 private static final String ANNOTATION_DATE = "@date "; 35 private static final String ANNOTATION = "/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR +RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1; 36 37 38 //文件 地址 39 //private static final String BEAN_PATH = "com/b510/base/bean"; 40 private static final String DAO_PATH = "com/b510/base/dao"; 41 private static final String DAO_IMPL_PATH = "com/b510/base/dao/impl"; 42 private static final String SERVICE_PATH = "com/b510/base/service"; 43 private static final String SERVICE_IMPL_PATH = "com/b510/base/service/impl"; 44 45 46 47 //包名 48 private static final String BEAN_URL = "com.b510.base.bean"; 49 private static final String DAO_URL = "com.b510.base.dao"; 50 private static final String DAO_IMPL_URL = "com.b510.base.dao.impl"; 51 private static final String SERVICE_URL = "com.b510.base.service"; 52 private static final String SERVICE_IMPL_URL = "com.b510.base.service.impl"; 53 54 //基本类名称 55 private static final String BASE_DAO_NAME = DAO_URL + ".BaseDao"; 56 private static final String ABSTRACT_BASE_DAO_IMPL_NAME = DAO_IMPL_URL + ".AbstractBaseDaoImpl"; 57 private static final String BASE_SERVICE_NAME = SERVICE_URL + ".BaseService"; 58 private static final String ABSTRACT_BASE_SERVICE_IMPL_NAME = SERVICE_IMPL_URL + ".AbstractBaseServiceImpl"; 59 60 61 /** 62 * 创建bean的Dao<br> 63 * 64 * @param c 65 * @throws Exception 66 */ 67 public void createBeanDao(Class c) throws Exception { 68 String cName = c.getName(); 69 String fileName = System.getProperty("user.dir") + "/src/" + DAO_PATH 70 + "/" + getLastChar(cName) + "Dao.java"; 71 File f = new File(fileName); 72 FileWriter fw = new FileWriter(f); 73 fw.write("package "+DAO_URL+";"+RT_2+ANNOTATION+"public interface " + 74 getLastChar(cName) + "Dao extends "+BASE_DAO_NAME+" <" + cName + "> {"+RT_2+"}"); 75 fw.flush(); 76 fw.close(); 77 showInfo(fileName); 78 } 79 80 /** 81 * 创建bean的Dao的实现类 82 * @param c 83 * @throws Exception 84 */ 85 public void createBeanDaoImpl(Class c) throws Exception{ 86 String cName = c.getName(); 87 String fileName = System.getProperty("user.dir") + "/src/" + DAO_IMPL_PATH 88 + "/" + getLastChar(cName) + "DaoImpl.java"; 89 File f = new File(fileName); 90 FileWriter fw = new FileWriter(f); 91 fw.write("package "+DAO_IMPL_URL+";"+RT_2+ANNOTATION+"public class " + 92 getLastChar(cName) + "DaoImpl extends "+ABSTRACT_BASE_DAO_IMPL_NAME+"<" + 93 cName + "> implements "+DAO_URL+"."+getLastChar(cName)+"Dao{"+RT_2+"}"); 94 fw.flush(); 95 fw.close(); 96 showInfo(fileName); 97 } 98 99 100 101 /**102 * 创建bean的service103 * @param c104 * @throws Exception105 */106 public void createBeanService(Class c) throws Exception{ 107 String cName = c.getName();108 String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_PATH109 + "/" + getLastChar(cName) + "Service.java";110 File f = new File(fileName);111 FileWriter fw = new FileWriter(f);112 fw.write("package "+SERVICE_URL+";"+RT_2+ANNOTATION+"public interface " + 113 getLastChar(cName) + "Service extends "+BASE_SERVICE_NAME+"<"+ cName +">{"+RT_2+"}");114 fw.flush();115 fw.close();116 showInfo(fileName);117 }118 119 /**120 * 创建bean的service的实现类121 * @param c122 * @throws Exception123 */124 public void createBeanServiceImpl(Class c) throws Exception{ 125 String cName = c.getName();126 String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_IMPL_PATH 127 + "/" +getLastChar(cName)+"ServiceImpl.java";128 File f = new File(fileName);129 FileWriter fw = new FileWriter(f);130 fw.write("package "+SERVICE_IMPL_URL+";"+RT_2+ANNOTATION+"public class " 131 + getLastChar(cName) + "ServiceImpl extends "+ABSTRACT_BASE_SERVICE_IMPL_NAME+"<"+ cName 132 + "> implements "+SERVICE_URL+"."+getLastChar(cName)+"Service{"+RT_2+BLANK_4133 +"private "+DAO_URL+"."+getLastChar(cName)+"Dao "+getLowercaseChar(getLastChar(cName))134 +"Dao;"+RT_2+BLANK_4+"public void set"+getLastChar(cName)+"Dao("+DAO_URL+"."+getLastChar(cName)+"Dao "135 +getLowercaseChar(getLastChar(cName))+"Dao){"+RT_1+BLANK_8+"this."+getLowercaseChar(getLastChar(cName))+"Dao = "136 +getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+BLANK_4+"@Override"+RT_1+BLANK_4137 +"public "+DAO_URL+"."+"BaseDao<"+BEAN_URL+"."+getLastChar(cName)+"> getBaseDao(){"+RT_1+BLANK_8138 +"return "+getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+"}");139 fw.flush();140 fw.close();141 showInfo(fileName);142 }143 144 145 /**146 * 获取路径的最后面字符串<br>147 * 如:<br>148 * <code>str = "com.b510.base.bean.User"</code><br>149 * <code> return "User";<code>150 * @param str151 * @return152 */153 public String getLastChar(String str) { 154 if ((str != null) && (str.length() > 0)) { 155 int dot = str.lastIndexOf('.');156 if ((dot > -1) && (dot < (str.length() - 1))) { 157 return str.substring(dot + 1);158 }159 }160 return str;161 }162 163 /**164 * 把第一个字母变为小写<br>165 * 如:<br>166 * <code>str = "UserDao";</code><br>167 * <code>return "userDao";</code>168 * @param str169 * @return170 */171 public String getLowercaseChar(String str){ 172 return str.substring(0,1).toLowerCase()+str.substring(1);173 }174 175 /**176 * 显示信息177 * @param info178 */179 public void showInfo(String info){ 180 System.out.println("创建文件:"+ info+ "成功!");181 }182 183 /**184 * 获取系统时间185 * @return186 */187 public static String getDate(){ 188 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");189 return simpleDateFormat.format(new Date());190 }191 }/UUUU_Web_Test/src/com/b510/base/bean/install/BeanUtilTest.java 1 /** 2 * 3 */ 4 package com.b510.base.bean.install; 5 6 import com.b510.base.bean.Admin; 7 import com.b510.base.bean.People; 8 import com.b510.base.bean.User; 9 10 /**11 * @author hongten()12 * @date 2013-2-2413 */14 public class BeanUtilTest { 15 16 public static void main(String[] args) throws Exception{ 17 BeanUtilTest beanUtilTest = new BeanUtilTest();18 BeanUtils beanUtils = new BeanUtils();19 beanUtilTest.beanTool(beanUtils, User.class);20 beanUtilTest.beanTool(beanUtils, People.class);21 beanUtilTest.beanTool(beanUtils, Admin.class);22 }23 24 /**25 * 根据bean生成相应的文件26 * @param beanUtils27 * @param c28 * @throws Exception29 */30 @SuppressWarnings("unchecked")31 public void beanTool(BeanUtils beanUtils ,Class c)throws Exception{ 32 beanUtils.createBeanDao(c);33 beanUtils.createBeanDaoImpl(c);34 beanUtils.createBeanService(c);35 beanUtils.createBeanServiceImpl(c);36 }37 }
源码下载