jfinal 生成实体类

叁歲伎倆 2022-04-25 01:30 476阅读 0赞
  1. jfinal根据Generator生成器生成实体类:
  2. package com.ysd.test;
  3. import javax.sql.DataSource;
  4. import com.jfinal.kit.PathKit;
  5. import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
  6. import com.jfinal.plugin.activerecord.generator.Generator;
  7. import com.jfinal.plugin.druid.DruidPlugin;
  8. import com.ysd.entity.ImsChatRoomcashsumLast;
  9. public class _JFinalDemoGenerator {
  10. public static DataSource getDataSource() {
  11. //PropKit.use("a_little_config.txt");
  12. DruidPlugin druidPlugin = new DruidPlugin("jdbc:mysql://47.94.172.202/phpweizan", "temp1", "asdfghjkl");
  13. druidPlugin.start();
  14. ActiveRecordPlugin activeRecord = new ActiveRecordPlugin(druidPlugin);
  15. activeRecord.addMapping("ims_chat_roomcashsum_last", "Id",ImsChatRoomcashsumLast.class);
  16. //activeRecord.addMapping("Askers", "userId",Askers.class);
  17. return druidPlugin.getDataSource();
  18. }
  19. public static void main(String[] args) {
  20. // base model 所使用的包名
  21. String baseModelPackageName = "com.ysd.entity";
  22. // base model 文件保存路径
  23. String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/ysd/entity";
  24. // model 所使用的包名 (MappingKit 默认使用的包名)
  25. String modelPackageName = "com.ysd.entity";
  26. // model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
  27. String modelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/ysd/entity";
  28. // 创建生成器
  29. //Generator generator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
  30. //generator.setMetaBuilder(new _MetaBuilder(getDataSource()));
  31. // 设置是否生成链式 setter 方法
  32. //generator.setGenerateChainSetter(false);
  33. // 添加不需要生成的表名
  34. //generator.addExcludedTable("ims_chat_roomcashsum_last");
  35. // 设置是否在 Model 中生成 dao 对象
  36. //generator.setGenerateDaoInModel(true);
  37. // 设置是否生成链式 setter 方法
  38. //generator.setGenerateChainSetter(true);
  39. // 设置是否生成字典文件
  40. //generator.setGenerateDataDictionary(false);
  41. // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
  42. //generator.setRemovedTableNamePrefixes("t_");
  43. // 生成
  44. //generator.generate();
  45. }
  46. }

jfinal不依靠Generator生成单独实体类

  1. package com.ysd.util;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSetMetaData;
  10. import java.sql.SQLException;
  11. import java.util.Vector;
  12. public class reflectBean {
  13. private Connection connection;
  14. private PreparedStatement UserQuery;
  15. /*mysql url的连接字符串*/
  16. private static String url = "jdbc:mysql://47.94.172.202/phpweizan?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true";
  17. //账号
  18. private static String user = "temp1";
  19. //密码
  20. private static String password = "asdfghjkl";
  21. private Vector<String> vector = new Vector<String>();
  22. //mysql jdbc的java包驱动字符串
  23. private String driverClassName = "com.mysql.jdbc.Driver";
  24. //数据库中的表名
  25. String table = "core_admin";
  26. //数据库的列名称
  27. private String[] colnames; // 列名数组
  28. //列名类型数组
  29. private String[] colTypes;
  30. public reflectBean(){
  31. try {//驱动注册
  32. Class.forName(driverClassName);
  33. if (connection == null || connection.isClosed())
  34. //获得链接
  35. connection = DriverManager.getConnection(url, user, password);
  36. } catch (ClassNotFoundException ex) {
  37. ex.printStackTrace();
  38. System.out.println("Oh,not");
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. System.out.println("Oh,not");
  42. }
  43. }
  44. public Connection getConnection() {
  45. return connection;
  46. }
  47. public void setConnection(Connection connection) {
  48. this.connection = connection;
  49. }
  50. public void doAction(){
  51. String sql = "select * from "+table;
  52. try {
  53. PreparedStatement statement = connection.prepareStatement(sql);
  54. //获取数据库的元数据
  55. ResultSetMetaData metadata = statement.getMetaData();
  56. //数据库的字段个数
  57. int len = metadata.getColumnCount();
  58. //字段名称
  59. colnames = new String[len+1];
  60. //字段类型 --->已经转化为java中的类名称了
  61. colTypes = new String[len+1];
  62. for(int i= 1;i<=len;i++){
  63. //System.out.println(metadata.getColumnName(i)+":"+metadata.getColumnTypeName(i)+":"+sqlType2JavaType(metadata.getColumnTypeName(i).toLowerCase())+":"+metadata.getColumnDisplaySize(i));
  64. //metadata.getColumnDisplaySize(i);
  65. colnames[i] = metadata.getColumnName(i); //获取字段名称
  66. colTypes[i] = sqlType2JavaType(metadata.getColumnTypeName(i)); //获取字段类型
  67. System.out.println(sqlType2JavaType(metadata.getColumnTypeName(i)));
  68. }
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. /*
  74. * mysql的字段类型转化为java的类型*/
  75. private String sqlType2JavaType(String sqlType) {
  76. if(sqlType.equalsIgnoreCase("bit")){
  77. return "boolean";
  78. }else if(sqlType.equalsIgnoreCase("tinyint")){
  79. return "byte";
  80. }else if(sqlType.equalsIgnoreCase("smallint")){
  81. return "short";
  82. }else if(sqlType.equalsIgnoreCase("int")){
  83. return "Integer";
  84. }else if(sqlType.equalsIgnoreCase("bigint")){
  85. return "long";
  86. }else if(sqlType.equalsIgnoreCase("float")){
  87. return "float";
  88. }else if(sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")
  89. || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
  90. || sqlType.equalsIgnoreCase("smallmoney")){
  91. return "double";
  92. }else if(sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")
  93. || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
  94. || sqlType.equalsIgnoreCase("text")){
  95. return "String";
  96. }else if(sqlType.equalsIgnoreCase("datetime") ||sqlType.equalsIgnoreCase("date")){
  97. return "Date";
  98. }else if(sqlType.equalsIgnoreCase("image")){
  99. return "Blod";
  100. }
  101. return null;
  102. }
  103. /*获取整个类的字符串并且输出为java文件
  104. * */
  105. public StringBuffer getClassStr(){
  106. //输出的类字符串
  107. StringBuffer str = new StringBuffer("");
  108. //获取表类型和表名的字段名
  109. this.doAction();
  110. //校验
  111. if(null == colnames && null == colTypes) return null;
  112. //拼接
  113. str.append("public class "+table+" {\r\n");
  114. //拼接属性
  115. for(int index=1; index < colnames.length ; index++){
  116. str.append(getAttrbuteString(colnames[index],colTypes[index]));
  117. }
  118. //拼接get,Set方法
  119. for(int index=1; index < colnames.length ; index++){
  120. str.append(getGetMethodString(colnames[index],colTypes[index]));
  121. str.append(getSetMethodString(colnames[index],colTypes[index]));
  122. }
  123. str.append("}\r\n");
  124. //输出到文件中
  125. File file = new File(table+".java");
  126. BufferedWriter write = null;
  127. try {
  128. write = new BufferedWriter(new FileWriter(file));
  129. write.write(str.toString());
  130. write.close();
  131. } catch (IOException e) {
  132. e.printStackTrace();
  133. if (write != null)
  134. try {
  135. write.close();
  136. } catch (IOException e1) {
  137. e1.printStackTrace();
  138. }
  139. }
  140. return str;
  141. }
  142. /*
  143. * 获取字段字符串*/
  144. public StringBuffer getAttrbuteString(String name, String type) {
  145. if(!check(name,type)) {
  146. System.out.println("类中有属性或者类型为空");
  147. return null;
  148. };
  149. String format = String.format(" private %s %s;\n\r", new String[]{type,name});
  150. return new StringBuffer(format);
  151. }
  152. /*
  153. * 校验name和type是否合法*/
  154. public boolean check(String name, String type) {
  155. if("".equals(name) || name == null || name.trim().length() ==0){
  156. return false;
  157. }
  158. if("".equals(type) || type == null || type.trim().length() ==0){
  159. return false;
  160. }
  161. return true;
  162. }
  163. /*
  164. * 获取get方法字符串*/
  165. private StringBuffer getGetMethodString(String name, String type) {
  166. if(!check(name,type)) {
  167. System.out.println("类中有属性或者类型为空");
  168. return null;
  169. };
  170. String Methodname = "get"+GetTuoFeng(name);
  171. String format = String.format(" public %s %s(){\n\r", new Object[]{type,Methodname});
  172. format += String.format(" return this.%s;\r\n", new Object[]{name});
  173. format += " }\r\n";
  174. return new StringBuffer(format);
  175. }
  176. //将名称首字符大写
  177. private String GetTuoFeng(String name) {
  178. name = name.trim();
  179. if(name.length() > 1){
  180. name = name.substring(0, 1).toUpperCase()+name.substring(1);
  181. }else
  182. {
  183. name = name.toUpperCase();
  184. }
  185. return name;
  186. }
  187. /*
  188. * 获取字段的get方法字符串*/
  189. private Object getSetMethodString(String name, String type) {
  190. /*if(!check(name,type)) {
  191. System.out.println("类中有属性或者类型为空");
  192. return null;
  193. };*/
  194. String Methodname = "set"+GetTuoFeng(name);
  195. String format = String.format(" public void %s(%s %s){\n\r", new Object[]{Methodname,type,name});
  196. format += String.format(" this.%s = %s;\r\n", new Object[]{name,name});
  197. format += " }\r\n";
  198. return new StringBuffer(format);
  199. }
  200. public static void main(String[] args) {
  201. reflectBean bean = new reflectBean();
  202. System.err.println(bean.getClassStr());
  203. }
  204. }

还有手写底层生成数据库中model,和dao的,找不到了,下次更新。。。。。

https://www.cnblogs.com/hwf2029/p/7810766.html 目前只找到了其它人 写的比较好的,我自己啊丢了,你们可以先借鉴下

发表评论

表情:
评论列表 (有 0 条评论,476人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java实体生成工具

    前言 原因是这样的,eclipse有那种生成实体类的插件,可是我感觉装来装去很麻烦,于是我想,干脆自己做一个生成实体类的工具吧,说做就做,然后就自己花了两个小时左右做出来