分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

代码生成器实现的Entity,Dao,Service,Controller,JSP神器(含代码附件)

发布时间:2023-09-06 01:34责任编辑:胡小海关键词:暂无标签
???
Java代码
  1. packagecom.flong.codegenerator;
  2. importjava.sql.Connection;
  3. importjava.sql.DatabaseMetaData;
  4. importjava.sql.PreparedStatement;
  5. importjava.sql.ResultSet;
  6. importjava.sql.ResultSetMetaData;
  7. importjava.sql.SQLException;
  8. importjava.sql.Timestamp;
  9. importjava.text.SimpleDateFormat;
  10. importjava.util.ArrayList;
  11. importjava.util.Date;
  12. importjava.util.HashMap;
  13. importjava.util.List;
  14. importjava.util.Map;
  15. importjava.util.Set;
  16. importorg.apache.commons.lang3.StringUtils;
  17. /***
  18. *@Author:liangjilong
  19. *@Date:2015年12月5日下午12:25:12
  20. *@Email:jilongliang@sina.com
  21. *@Version:1.0
  22. *@CopyRight(c)FlongIntergrityLtd.
  23. *@Desction:★★★★★★★★★★★★★★★代码生成器实现思路★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
  24. *
  25. *★★在快速开发的过程中,为了节省时间和成本很多人都会开发自己的代码生成器,而且成为程序员开发过程中必不可少的神器.
  26. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
  27. *第一种:先有数据库表,然后通过jdbc链接数据库再读取表的字段等属性出来生成Entity,Dao,Service,Controller,JSP等代码
  28. *这种必须是有数据库和表的思想,通过程序去读取数据库表的属性等信息,然后组织代码通过文件流生成文件.
  29. *
  30. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
  31. *第二种:已经设计好数据库表文档,把所有表的字段属性配置到EXCEL或者CSV格式的文件通过JXL/POI技术去读取文件的字段实现
  32. *Entity,Dao,Service,Controller,JSP,在过程中会借助Freemaker,Velocity去实现.三层和jsp,然后通过一下ORM(hibernate,
  33. *ibatis,myibatis等)技术逆向生成数据库表.这种是无数据库表的思想.在生成java的代码一般不建议建ORM映射主从表关系,通过
  34. *SQL去建立关系,为啥?因为在一些大型的公司如:银行,阿里巴巴,电信等公司,很多项目开发过程中在数据库表很少建立表关系的
  35. *因为在些业务复杂的情况下通过SQL和程序控制的解决方案比ORM映射关系方案占优势.比如优化性能/维护/灵活性更加好等.
  36. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
  37. *
  38. *此程序以MySQL为主,未实现其他数据库.此程序可以再优化的,为了有些初学者,就不作太多的处理和优化.一些高手会编程更好的生
  39. *成器,此程序只提供参考和学习,如有什么问题,可以多指出.共同学习和进步.谢谢!~
  40. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
  41. */
  42. @SuppressWarnings("all")
  43. publicclassCodeGenerator{
  44. /*************************变量****Begin************************************/
  45. privatestaticfinalStringmyEmail="jilongliang@sina.com";//Email
  46. privatestaticfinalStringVersion="1.0";//版本
  47. privatestaticfinalStringDescription="";//描述
  48. publicstaticfinalStringENTER="\n";//换行
  49. publicstaticfinalStringTAB="";//tab空格.
  50. publicstaticfinalStringNAME="NAME";
  51. publicstaticfinalStringTABLE_CAT="TABLE_CAT";//表catalog
  52. publicstaticfinalStringTABLE_SCHEM="TABLE_SCHEM";//表schema
  53. publicstaticfinalStringTABLE_NAME="TABLE_NAME";//表名
  54. publicstaticfinalStringTABLE_TYPE="TABLE_TYPE";//表类型
  55. publicstaticfinalStringREMARKS="REMARKS";//表注释
  56. publicstaticfinalStringTYPE="TYPE";//表的类型
  57. publicstaticfinalStringSIZE="SIZE";//大小
  58. publicstaticfinalString;//类别
  59. /*************************变量****End************************************/
  60. publicstaticfinalStringNOW_DATE=newSimpleDateFormat("yyyy-MM-dd").format(newDate());
  61. /***************获取数据库的配置连接************/
  62. publicstaticfinalStringDB_NAME=PropertiesHelper.getValueByKey("jdbc.url").substring(
  63. PropertiesHelper.getValueByKey("jdbc.url").lastIndexOf("/")+1,
  64. PropertiesHelper.getValueByKey("jdbc.url").indexOf("?")==-1?
  65. PropertiesHelper.getValueByKey("jdbc.url").length():
  66. PropertiesHelper.getValueByKey("jdbc.url").indexOf("?"));
  67. //从配置获取工程的报名路径
  68. publicstaticfinalStringROOT_PACKAGE=PropertiesHelper.getValueByKey("rootPackage");
  69. //获取作者.
  70. publicstaticfinalStringAUTHOR=PropertiesHelper.getValueByKey("author");
  71. //忽略表的后缀.
  72. publicstaticfinalList<String>IGNORE_TABLE_PREFIX=newArrayList<String>();
  73. /*******定义代码块*******/
  74. static{
  75. StringignoreTablePrefix=PropertiesHelper.getValueByKey("ignoreTablePrefix");
  76. if(ignoreTablePrefix.length()>0){
  77. String[]ignoreTablePrefixs=ignoreTablePrefix.split("\\s*\\,\\s*");
  78. for(Stringelem:ignoreTablePrefixs){
  79. IGNORE_TABLE_PREFIX.add(elem);
  80. }
  81. }
  82. }
  83. /***
  84. *生成实体类的代码
  85. *@paramtable
  86. *@throwsException
  87. */
  88. publicvoidcreateEntityClass(Stringtable)throwsException{
  89. StringtableConstantName=getTableConstantName(table);
  90. StringclassName=getClassName(tableConstantName);
  91. StringBuilderbuffer=newStringBuilder();
  92. buffer.append("package"+ROOT_PACKAGE+".entity;").append(ENTER);
  93. buffer.append("importjava.util.Date;").append(ENTER);
  94. buffer.append("importcom.flong.commons.persistence.Entity;").append(ENTER);
  95. buffer.append("importcom.flong.commons.persistence.annotation.Column;").append(ENTER);
  96. buffer.append("importcom.flong.commons.persistence.annotation.Id;").append(ENTER);
  97. buffer.append("importcom.flong.commons.persistence.annotation.Relation;").append(ENTER);
  98. buffer.append("importcom.flong.commons.persistence.annotation.Table;").append(ENTER);
  99. buffer.append(ENTER);
  100. buffer.append(ENTER);
  101. buffer.append("/**\n*@Created:"+NOW_DATE+"\n*@Author"+AUTHOR+"\n");
  102. buffer.append("*@Version:").append(Version).append(ENTER);
  103. buffer.append("*@Description:").append(className).append(ENTER);
  104. buffer.append("*@Email:").append(myEmail).append("\n*/");
  105. buffer.append(ENTER);
  106. buffer.append("@Relation("+className+".TABLE)");
  107. buffer.append(ENTER);
  108. buffer.append("publicclass"+className+"extendsEntity{");
  109. buffer.append(ENTER);
  110. buffer.append(ENTER);
  111. buffer.append(TAB);
  112. buffer.append("/**表名常量*/");
  113. buffer.append(ENTER);
  114. buffer.append(TAB);
  115. buffer.append("publicstaticfinalStringTABLE=Table."+tableConstantName+";");
  116. buffer.append(ENTER);
  117. buffer.append(ENTER);
  118. buffer.append(TAB);
  119. buffer.append("/**");
  120. buffer.append(ENTER);
  121. buffer.append(TAB);
  122. buffer.append("*列名常量");
  123. buffer.append(ENTER);
  124. buffer.append(TAB);
  125. buffer.append("*/");
  126. buffer.append(ENTER);
  127. for(Map<String,Object>col:getCols(table)){
  128. StringcolName=col.get(NAME).toString().toUpperCase();
  129. buffer.append(TAB);//生成字段变量
  130. buffer.append("publicstaticfinalStringCOL_"+colName+"=\""+colName+"\";//"+col.get(REMARKS));
  131. buffer.append(ENTER);
  132. }
  133. buffer.append(ENTER);
  134. buffer.append(TAB);
  135. buffer.append("/**");
  136. buffer.append(ENTER);
  137. buffer.append(TAB);
  138. buffer.append("*列属性");
  139. buffer.append(ENTER);
  140. buffer.append(TAB);
  141. buffer.append("*/");
  142. StringtablePrimaryKeys=getTablePrimaryKeys(table);//如果是主键
  143. //if(col.get(NAME).toString().equalsIgnoreCase("ID")){
  144. if(tablePrimaryKeys!=null){
  145. buffer.append(ENTER+TAB);
  146. //如果主键不为空的时候就给一个@Id注解.
  147. //如果是hibernate的可以给其他的注解,如@GeneratedValue(strategy=GenerationType.IDENTITY)@SequenceGenerator等
  148. //并要在包的下面头部导入
  149. //importjavax.persistence.Column;
  150. //importjavax.persistence.Entity;
  151. //importjavax.persistence.GeneratedValue;
  152. //importjavax.persistence.GenerationType;
  153. //importjavax.persistence.Id;
  154. //importjavax.persistence.Table;
  155. buffer.append("@Id");
  156. //这里不赋值给,因为下面这个for循环有一个.
  157. //sb.append("@Column(COL_"+tablePrimaryKeys+")");
  158. }
  159. for(Map<String,Object>col:getCols(table)){
  160. buffer.append(TAB);
  161. buffer.append(ENTER);
  162. buffer.append(TAB);
  163. buffer.append("@Column(COL_"+col.get(NAME).toString().toUpperCase()+")");
  164. buffer.append(ENTER);
  165. buffer.append(TAB);
  166. buffer.append("private");
  167. //这行代码的意思就是说,如果找到数据库表的字段是为ID的时候,或后缀有_ID的就认为是主键,并且忽略大小写就给一个Long
  168. //在实际过程中应该判断是它的字段是不是为了PrimaryKey才设为Long才适合.
  169. //if(col.get(NAME).toString().equalsIgnoreCase("ID")||col.get(NAME).toString().toUpperCase().endsWith("_ID")){
  170. if(Class.forName(col.get(CLASS).toString()).isAssignableFrom(Date.class)||Class.forName(col.get(CLASS).toString())==Timestamp.class){
  171. buffer.append("Date");
  172. }elseif(getClassName(col.get(NAME).toString()).equals(Class.forName(col.get(CLASS).toString()).getSimpleName())){
  173. buffer.append(col.get(CLASS));
  174. }else{
  175. buffer.append(Class.forName(col.get(CLASS).toString()).getSimpleName());
  176. }
  177. //sb.append(""+getFieldName(col.get(NAME).toString())+";");
  178. buffer.append(""+col.get(NAME).toString()+";");
  179. buffer.append(ENTER);
  180. }
  181. buffer.append(ENTER);
  182. for(Map<String,Object>col:getCols(table)){
  183. buffer.append(TAB);
  184. buffer.append("public");
  185. if(Class.forName(col.get(CLASS).toString()).isAssignableFrom(Date.class)||Class.forName(col.get(CLASS).toString())==Timestamp.class){
  186. buffer.append("Date");
  187. }elseif(getClassName(col.get(NAME).toString()).equals(Class.forName(col.get(CLASS).toString()).getSimpleName())){
  188. buffer.append(col.get(CLASS));
  189. }else{
  190. buffer.append(Class.forName(col.get(CLASS).toString()).getSimpleName());
  191. }
  192. buffer.append("").append("get").append(col.get(NAME).toString().replaceFirst("\\b(\\w)|\\s(\\w)",col.get(NAME).toString().substring(0,1).toUpperCase()));
  193. buffer.append("(){");
  194. buffer.append(ENTER);
  195. buffer.append(TAB);
  196. buffer.append(TAB);
  197. buffer.append("return").append(col.get(NAME).toString()).append(";");
  198. buffer.append(ENTER);
  199. buffer.append(TAB);
  200. buffer.append("}");
  201. buffer.append(ENTER);
  202. buffer.append(TAB);
  203. buffer.append("publicvoid").append("set").append(col.get(NAME).toString().replaceFirst("\\b(\\w)|\\s(\\w)",col.get(NAME).toString().substring(0,1).toUpperCase()));
  204. buffer.append("(");
  205. if(Class.forName(col.get(CLASS).toString()).isAssignableFrom(Date.class)||Class.forName(col.get(CLASS).toString())==Timestamp.class){
  206. buffer.append("Date");
  207. }elseif(getClassName(col.get(NAME).toString()).equals(Class.forName(col.get(CLASS).toString()).getSimpleName())){
  208. buffer.append(col.get(CLASS));
  209. }else{
  210. buffer.append(Class.forName(col.get(CLASS).toString()).getSimpleName());
  211. }
  212. buffer.append("").append(col.get(NAME).toString());
  213. buffer.append("){");
  214. buffer.append(ENTER);
  215. buffer.append(TAB);
  216. buffer.append(TAB);
  217. buffer.append("this.").append(col.get(NAME).toString()).append("=").append(col.get(NAME).toString()).append(";");
  218. buffer.append(ENTER);
  219. buffer.append(TAB);
  220. buffer.append("}");
  221. buffer.append(ENTER);
  222. }
  223. buffer.append("}");
  224. buffer.append(ENTER);
  225. FileUtils.save("output-code/"+ROOT_PACKAGE.replaceAll("\\.","/")+"/entity/"+className+".java",buffer.toString());
  226. }
  227. /***
  228. *生成dao接口interface类的代码
  229. *@paramtable
  230. *@throwsException
  231. */
  232. publicvoidcreateDaoClass(Stringtable)throwsException{
  233. StringclassName=getClassName(getTableConstantName(table));
  234. StringobjectName=StringUtils.uncapitalize(className);
  235. StringBuilderbuffer=newStringBuilder();
  236. buffer.append("package"+ROOT_PACKAGE+".dao;").append(ENTER);
  237. buffer.append("importjava.io.Serializable;").append(ENTER);
  238. buffer.append("importjava.util.List;").append(ENTER);
  239. buffer.append("importcom.flong.commons.persistence.bean.SimplePage;").append(ENTER);
  240. buffer.append("importcom.flong.commons.persistence.dao.EntityDao;").append(ENTER);
  241. buffer.append("importcom.flong.modules.pojo."+className+";").append(ENTER);
  242. buffer.append(ENTER);
  243. buffer.append(ENTER);
  244. buffer.append("/**\n*@Created:"+NOW_DATE+"\n*@Author"+AUTHOR+"\n");
  245. buffer.append("*@Version:").append(Version).append(ENTER);
  246. buffer.append("*@Description:").append(className).append(ENTER);
  247. buffer.append("*@Email:").append(myEmail).append("\n*/");
  248. buffer.append(ENTER);
  249. buffer.append("publicinterface"+className+"DaoextendsEntityDao<"+className+">{").append(ENTER);
  250. buffer.append("/**查询*/").append(ENTER);
  251. buffer.append("publicList<"+className+">list(SimplePagesimplePage,"+className+""+objectName+");").append(ENTER);
  252. buffer.append("/**保存数据*/").append(ENTER);
  253. buffer.append("publicvoidsaveData("+className+""+objectName+");").append(ENTER);
  254. buffer.append("/**更新数据*/").append(ENTER);
  255. buffer.append("publicvoidupdateData("+className+""+objectName+");").append(ENTER);
  256. buffer.append("/**删除数据*/").append(ENTER);
  257. buffer.append("publicvoiddeleteData(Longpk);").append(ENTER);
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved