//第一次学习Jfinal框架,挺方便的,就是这个ActiveRecordPlugin有点头疼,
//是不是失去了将数据库对象化的操作了,怪自己懒,写一个算是数据库映射吧..(大虾莫笑)
1. [代码][Java]代码
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 |
packagecom.npc.core.jfinal.create; importjava.io.File; importjava.io.FileWriter; importjava.io.IOException; importjava.sql.SQLException; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importcom.npc.model.MyModel; importfreemarker.template.Configuration; importfreemarker.template.Template; importfreemarker.template.TemplateException; /** * 根据数据库的表跟列生成jfinal需要的entity格式 * * @ClassName: Create * @Description: TODO * * @date:2013-3-3 下午8:28:02 */ publicclassCreate { publicstaticvoidmain(String[] args)throwsSQLException, IOException, TemplateException { Configuration config =newConfiguration(); config.setClassForTemplateLoading(Create.class,"/ftl"); Template temp = config.getTemplate("entity.ftl"); Map<String,MyModel> map =newHashMap<String,MyModel>(); MyModel myModel =newMyModel(); myModel.setPackageName(DBConn.p.getProperty("package")); List<String> tables = DBConn.getTableNamesByDBName();// 获取该数据库的所有表名称 for(String table : tables) { myModel.setTableName(table);// 生成当前的Entity类 myModel.setColumnsNames(DBConn.getColumnsNamesByTableName(table));// 根据表名称获取所有的列名称 map.put("myModel", myModel); File createFolder =newFile(System.getProperty("user.dir")+"/src/"+DBConn.p.getProperty("package").replace(".","/")); createFolder.mkdirs(); //预先创建文件夹,预防没有文件夹而找不到路径 temp.process(map,newFileWriter(createFolder+"/"+newCreate().toLowerCaseTheFristChar(table)+".java")); } System.out.println("生成Entity成功!.请查看"); } privateString toLowerCaseTheFristChar(String str){ byte[] items = str.getBytes(); items[0] = (byte)((char)items[0]-‘a‘+‘A‘); returnnewString(items); } } |
2. [代码][Java]代码
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 |
packagecom.npc.core.jfinal.create; importjava.io.IOException; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.ResultSet; importjava.sql.SQLException; importjava.util.ArrayList; importjava.util.List; importjava.util.Properties; importcom.mysql.jdbc.Statement; publicclassDBConn { publicstaticfinalProperties p =newProperties(); static{ try{ p.load(DBConn.class.getResourceAsStream("/createEntity.properties")); Class.forName(p.getProperty("className")); }catch(ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } publicstaticConnection getConnection()throwsIOException, SQLException { returnDriverManager.getConnection(p.getProperty("driverName"), p.getProperty("userName"), p.getProperty("userPassword")); } /** * 获取数据库的所有表名称 * * @Title: getTableNamesByDBName * @Description: TODO * @param @return * @param @throws SQLException * @param @throws IOException * @author: 陈文希 * @return List<String> * @throws */ publicstaticList<String> getTableNamesByDBName()throwsSQLException, IOException { Statement stame = (Statement) DBConn.getConnection().createStatement(); ResultSet rs = stame.executeQuery("show tables;"); List<String> list =newArrayList<String>(); while(rs.next()) { list.add(rs.getString(1)); } returnlist; } /** * 根据表名称获取表的所有字段名称 * @Title: getColumnsNamesByTableName * @Description: TODO * @param @param tName * @param @return * @param @throws SQLException * @param @throws IOException * @author: 陈文希 * @return List<String> * @throws */ publicstaticList<String> getColumnsNamesByTableName(String tName)throwsSQLException, IOException{ List<String> list =newArrayList<String>(); Statement stame = (Statement) DBConn.getConnection().createStatement(); ResultSet rs = stame.executeQuery("desc "+tName+";"); while(rs.next()) { list.add(rs.getString(1)); } returnlist; } } |
3. [代码][Java]代码
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 |
packagecom.npc.model; importjava.util.List; /** * Model 实体类的数据承载 * @ClassName: MyModel * @Description: TODO * @author: 陈文希 * @date:2013-3-3 下午11:54:27 */ publicclassMyModel { privateString packageName; privateString tableName; privateList<String> columnsNames; publicString getPackageName() { returnpackageName; } publicvoidsetPackageName(String packageName) { this.packageName = packageName; } publicString getTableName() { returntableName; } publicvoidsetTableName(String tableName) { this.tableName = tableName; } publicList<String> getColumnsNames() { returncolumnsNames; } publicvoidsetColumnsNames(List<String> columnsNames) { this.columnsNames = columnsNames; } } |
4. [代码][Java]代码
1 2 3 4 5 6 7 8 9 10 |
package${myModel.packageName}; importcom.jfinal.plugin.activerecord.Model; publicclass${myModel.tableName?cap_first}extendsModel<${myModel.tableName?cap_first}>{ publicstaticfinal${myModel.tableName?cap_first} dao =new${myModel.tableName?cap_first}(); <#list myModel.columnsNames as column> publicstaticfinalString ${column?upper_case} ="${column}"; </#list> } |