1 /**
2 *3 */
4 packagecom.b510.base.bean.install;5
6 importjava.io.File;7 importjava.io.FileWriter;8 importjava.text.SimpleDateFormat;9 importjava.util.Date;10
11
12 /**
13 *@authorhongten(hongtenzone@foxmail.com)14 * @date 2013-2-2415 */
16 @SuppressWarnings("unchecked")17 public classBeanUtils {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(hongtenzone@foxmail.com)";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
63 *64 *@paramc65 *@throwsException66 */
67 public void createBeanDao(Class c) throwsException {68 String cName =c.getName();69 String fileName = System.getProperty("user.dir") + "/src/" +DAO_PATH70 + "/" + getLastChar(cName) + "Dao.java";71 File f = newFile(fileName);72 FileWriter fw = newFileWriter(f);73 fw.write("package "+DAO_URL+";"+RT_2+ANNOTATION+"public interface " +
74 getLastChar(cName) + "Dao extends "+BASE_DAO_NAME+" {"+RT_2+"}");75 fw.flush();76 fw.close();77 showInfo(fileName);78 }79
80 /**
81 * 创建bean的Dao的实现类82 *@paramc83 *@throwsException84 */
85 public void createBeanDaoImpl(Class c) throwsException{86 String cName =c.getName();87 String fileName = System.getProperty("user.dir") + "/src/" +DAO_IMPL_PATH88 + "/" + getLastChar(cName) + "DaoImpl.java";89 File f = newFile(fileName);90 FileWriter fw = newFileWriter(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 *@paramc104 *@throwsException105 */
106 public void createBeanService(Class c) throwsException{107 String cName =c.getName();108 String fileName = System.getProperty("user.dir") + "/src/" +SERVICE_PATH109 + "/" + getLastChar(cName) + "Service.java";110 File f = newFile(fileName);111 FileWriter fw = newFileWriter(f);112 fw.write("package "+SERVICE_URL+";"+RT_2+ANNOTATION+"public interface " +
113 getLastChar(cName) + "Service extends "+BASE_SERVICE_NAME+"{"+RT_2+"}");114 fw.flush();115 fw.close();116 showInfo(fileName);117 }118
119 /**
120 * 创建bean的service的实现类121 *@paramc122 *@throwsException123 */
124 public void createBeanServiceImpl(Class c) throwsException{125 String cName =c.getName();126 String fileName = System.getProperty("user.dir") + "/src/" +SERVICE_IMPL_PATH127 + "/" +getLastChar(cName)+"ServiceImpl.java";128 File f = newFile(fileName);129 FileWriter fw = newFileWriter(f);130 fw.write("package "+SERVICE_IMPL_URL+";"+RT_2+ANNOTATION+"public class "
131 + getLastChar(cName) + "ServiceImpl extends "+ABSTRACT_BASE_SERVICE_IMPL_NAME+" 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 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 * 获取路径的最后面字符串
147 * 如:
148 * str = "com.b510.base.bean.User"
149 * return "User";
150 *@paramstr151 *@return
152 */
153 publicString 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 returnstr;161 }162
163 /**
164 * 把第一个字母变为小写
165 * 如:
166 * str = "UserDao";
167 * return "userDao";
168 *@paramstr169 *@return
170 */
171 publicString getLowercaseChar(String str){172 return str.substring(0,1).toLowerCase()+str.substring(1);173 }174
175 /**
176 * 显示信息177 *@paraminfo178 */
179 public voidshowInfo(String info){180 System.out.println("创建文件:"+ info+ "成功!");181 }182
183 /**
184 * 获取系统时间185 *@return
186 */
187 public staticString getDate(){188 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");189 return simpleDateFormat.format(newDate());190 }191 }