[Author]: Kwu
Automatic restful development based on reflection, universal only needs to write query database SQL, and add the corresponding JavaBean implementation of fast restful service development.
1, write the database query SQL, corresponding sql.properties
Daily = Dailyreport;select T.day,t.cnt,t.type from (select Day,cnt,type from Dailyreport where type=? ORDER BY day Desc Li Mit? ) t order by T.day; String,integer
SQL properties file to ";" Separated.Description:
1) PV is the label for the SQL.
2) The first parameter is, Dailyreport is the class name of the corresponding JavaBean
3) The second parameter is the SQL of the query with the "?" placeholder
4) The third parameter is the type of the parameter, separated by ","
2, write the corresponding Pojo class
Import Com.hexun.bean.base.chartsdata;public class Dailyreport implements Chartsdata {private String day, Type;private Integer cnt;public String GetDay () {return day;} public void Setday (String day) {this.day = day;} Public String GetType () {return type;} public void SetType (String type) {this.type = type;} Public Integer getcnt () {return CNT;} public void setcnt (Integer cnt) {this.cnt = cnt;}}
3. Start RESTful service access
Http://localhost:9088/restful?tag=pv&args=3
With Reflection DAO Implementations:
Package Com.hexun.dao;import Java.lang.reflect.field;import Java.sql.connection;import java.sql.PreparedStatement; Import Java.sql.resultset;import java.util.arraylist;import Java.util.hashmap;import Java.util.List;import Java.util.map;import Com.hexun.bean.base.attrdata;import Com.hexun.bean.base.chartsdata;import Com.hexun.utils.dbutil;import com.hexun.utils.GetSqlStatement; @SuppressWarnings ("Rawtypes") public class Chartdao { private static Dbutil Dbutil = new Dbutil ();p ublic static list<chartsdata> getrestdata (String tag, string[] args) th Rows Exception {Connection con = Dbutil.getcon (); String content = getsqlstatement.getsql (tag), if (content==null) {return new arraylist<chartsdata> ();} String splited[] = Content.split (";"); String className = splited[0]; String sql = splited[1]; Class clazz = Class.forName ("Com.hexun.bean." + className); list<attrdata> listattr = Getclassattrs (clazz); PreparedStatement pstmt = con.preparestatement (sql), if (splited.length = = 3) {String type= Splited[2];int idx = 1; string[] types = Type.split (","); for (int i = 0; i < types.length; i++) {if (Types[i].trim (). Equalsignorecase ("String") ) {pstmt.setstring (idx, args[i]);} if (Types[i].trim (). Equalsignorecase ("Integer")) {String tmp = Args[i];int intval = integer.parseint (tmp);p Stmt.setint (idx, intval);} idx++;}} ResultSet rs = Pstmt.executequery (); list<chartsdata> listData = new arraylist<chartsdata> (); while (Rs.next ()) {Chartsdata bean = (chartsdata) Clazz.newinstance (); for (Attrdata attribute:listattr) {Field f1 = Clazz.getdeclaredfield (Attribute.getname ()); String Attrtype = Attribute.gettype (), if ("Class Java.lang.Integer". Equals (Attrtype)) {Integer val = rs.getint ( Attribute.getname ()); F1.setaccessible (true); F1.set (Bean, Val);} else {String val = rs.getstring (Attribute.getname ()); F1.setaccessible (true); F1.set (Bean, Val);}} Listdata.add (bean);} Dbutil.close (RS, pstmt, con); return listData;} /** * Gets the property collection of the class * * @param listattr * @param clazz */private static list< Attrdata> getclassattrs (Class clazz) {list<attrdata> listattr = new arraylist<attrdata> (); field[] FieldList = Clazz.getdeclaredfields (); for (int i = 0; i < fieldlist.length; i++) {Attrdata data = new Attrdata ( ); Field fld = Fieldlist[i]; String type = Fld.gettype (). toString (); String name = Fld.getname ();d ata.setname (name);d ata.settype (type); Listattr.add (data);} System.out.println (listattr); return listattr;} /** * Rest data converted to List * * @param restdata * @return * @throws classnotfoundexception */public static map<string, List> ; Getrestdataforlist (list<chartsdata> restdata, String tag) throws Exception {map<string, list> Map = new Hashm Ap<string, list> (); String content = getsqlstatement.getsql (tag); String splited[] = Content.split (";"); String className = splited[0]; Class clazz = Class.forName ("Com.hexun.bean." + className); list<attrdata> listattr = Getclassattrs (Clazz); for (Attrdata attribute:listattr) {String Attrtype = Attribute.get Type (); if ("Class Java.lang.Integer". Equals (Attrtype)) {list<integer> listData = new arraylist<integer> (); for ( Chartsdata data:restdata) {Field f1 = Clazz.getdeclaredfield (Attribute.getname ()); F1.setaccessible (true); Object Object = F1.get (data); Listdata.add ((object = = null)? 0:integer.parseint (Object.ToString ()));} Map.put (Attribute.getname (), listData);} else {list<string> listData = new arraylist<string> (); for (Chartsdata data:restdata) {Field F1 = clazz.getde Claredfield (Attribute.getname ()); F1.setaccessible (true); object object = F1.get (data); Listdata.add ((Object = = null)? "": object.tostring ());} Map.put (Attribute.getname (), listData);}} return map;} public static void Main (string[] args) throws Exception {list<chartsdata> Restdata = Chartdao.getrestdata (" Newsclick ", new string[] {" Hour_trend "," 15 "}); System.out.println (restdata);//map<string, list> restdataforlist =//chartdao.getrestdataforlist (Restdata, " Daily ");//System.out.println (RestdataforliST);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Automated restful development based on reflection