数据库表转javaBean
复制后修改部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | package com.study; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class TableToJavaBean { Connection conn = null ; Statement stmt = null ; public Connection getConnection(){ String driverName = "" ; String url = "" ; String username = "" ; String pwd = "" ; try { InputStream in = this .getClass().getResourceAsStream( "/dbconfig.properties" ); Properties pros = new Properties(); pros.load(in); driverName = pros.getProperty( "database.driver" ); url = pros.getProperty( "database.url" ); username = pros.getProperty( "database.user" ); pwd = pros.getProperty( "database.password" ); System.out.println(driverName+ "==" +url); Class.forName(driverName); conn = DriverManager.getConnection(url, username, pwd); } catch (SQLException e) { } catch (ClassNotFoundException e) { } catch (IOException e) { } return conn; } public void closeDB(PreparedStatement pstmt) { if (pstmt != null ) { try { pstmt.close(); } catch (Exception ex) { } } } public void closeDB(Statement stmt) { if (stmt != null ) { try { stmt.close(); } catch (Exception ex) { } } } public void closeDB(Connection con) { if (con != null ) { try { con.close(); } catch (Exception ex) { } } } public void toPojo(String tableName) { String sql = "select * from " +tableName; try { conn = getConnection(); PreparedStatement statement = conn.prepareStatement(sql); //获取数据库的元数据 ResultSetMetaData metadata = statement.getMetaData(); //数据库的字段个数 int len = metadata.getColumnCount(); //字段名称 colnames = new String[len+ 1 ]; //字段类型 --->已经转化为java中的类名称了 colTypes = new String[len+ 1 ]; for ( int i= 1 ;i<=len;i++){ colnames[i] = metadata.getColumnName(i); //获取字段名称 colTypes[i] = sqlType2JavaType(metadata.getColumnTypeName(i)); //获取字段类型 } } catch (SQLException e) { } } private String[] colnames; // 列名数组 //列名类型数组 private String[] colTypes; /* * mysql的字段类型转化为java的类型*/ private String sqlType2JavaType(String sqlType) { if (sqlType.equalsIgnoreCase( "bit" )){ return "boolean" ; } else if (sqlType.equalsIgnoreCase( "tinyint" )){ return "byte" ; } else if (sqlType.equalsIgnoreCase( "smallint" )){ return "short" ; } else if (sqlType.equalsIgnoreCase( "int" )){ return "int" ; } else if (sqlType.equalsIgnoreCase( "bigint" )){ return "long" ; } else if (sqlType.equalsIgnoreCase( "float" )){ return "float" ; } else if (sqlType.equalsIgnoreCase( "decimal" ) || sqlType.equalsIgnoreCase( "numeric" ) || sqlType.equalsIgnoreCase( "real" ) || sqlType.equalsIgnoreCase( "money" ) || sqlType.equalsIgnoreCase( "smallmoney" )){ return "double" ; } else if (sqlType.equalsIgnoreCase( "varchar" ) || sqlType.equalsIgnoreCase( "char" ) || sqlType.equalsIgnoreCase( "nvarchar" ) || sqlType.equalsIgnoreCase( "nchar" ) || sqlType.equalsIgnoreCase( "text" )){ return "String" ; } else if (sqlType.equalsIgnoreCase( "datetime" ) ||sqlType.equalsIgnoreCase( "date" )){ return "Date" ; } else if (sqlType.equalsIgnoreCase( "image" )){ return "Blod" ; } return null ; } /*获取整个类的字符串并且输出为java文件 * */ public StringBuffer getClassStr(String tableName){ //输出的类字符串 StringBuffer str = new StringBuffer( "" ); //获取表类型和表名的字段名 this .toPojo(tableName); //校验 if ( null == colnames && null == colTypes) return null ; //拼接 str.append( "public class " +tableName+ " {\r\n" ); //拼接属性 for ( int index= 1 ; index < colnames.length ; index++){ str.append(getAttrbuteString(colnames[index],colTypes[index])); } //拼接get,Set方法 for ( int index= 1 ; index < colnames.length ; index++){ str.append(getGetMethodString(colnames[index],colTypes[index])); str.append(getSetMethodString(colnames[index],colTypes[index])); } str.append( "}\r\n" ); //输出到文件中 File file = new File(tableName+ ".java" ); BufferedWriter write = null ; try { write = new BufferedWriter( new FileWriter(file)); write.write(str.toString()); write.close(); } catch (IOException e) { e.printStackTrace(); if (write != null ) try { write.close(); } catch (IOException e1) { e1.printStackTrace(); } } return str; } /* * 获取字段字符串*/ public StringBuffer getAttrbuteString(String name, String type) { if (!check(name,type)) { System.out.println( "类中有属性或者类型为空" ); return null ; }; String format = String.format( " private %s %s;\n\r" , new String[]{type,name}); return new StringBuffer(format); } /* * 校验name和type是否合法*/ public boolean check(String name, String type) { if ( "" .equals(name) || name == null || name.trim().length() == 0 ){ return false ; } if ( "" .equals(type) || type == null || type.trim().length() == 0 ){ return false ; } return true ; } /* * 获取get方法字符串*/ private StringBuffer getGetMethodString(String name, String type) { if (!check(name,type)) { System.out.println( "类中有属性或者类型为空" ); return null ; }; String Methodname = "get" +GetTuoFeng(name); String format = String.format( " public %s %s(){\n\r" , new Object[]{type,Methodname}); format += String.format( " return this.%s;\r\n" , new Object[]{name}); format += " }\r\n" ; return new StringBuffer(format); } //将名称首字符大写 private String GetTuoFeng(String name) { name = name.trim(); if (name.length() > 1 ){ name = name.substring( 0 , 1 ).toUpperCase()+name.substring( 1 ); } else { name = name.toUpperCase(); } return name; } /* * 获取字段的get方法字符串*/ private Object getSetMethodString(String name, String type) { if (!check(name,type)) { System.out.println( "类中有属性或者类型为空" ); return null ; }; String Methodname = "set" +GetTuoFeng(name); String format = String.format( " public void %s(%s %s){\n\r" , new Object[]{Methodname,type,name}); format += String.format( " this.%s = %s;\r\n" , new Object[]{name,name}); format += " }\r\n" ; return new StringBuffer(format); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人