1, DAO (data access Object): A class that accesses data information, contains a crud (Create, Read, Update, Delete) on the data, and does not contain any business-related information.
--dao can realize the modularization of function;
-facilitates the maintenance and upgrading of code.
2. Common methods in DAO:
--1) int update (String sql, Object ... args); Performs an update operation that returns the number of rows affected, including INSERT, UPDATE, delete operations.
--2) <T> T get (class<t> clazz, String sql, Object ... args); Queries a record to return the corresponding object.
--3) <T> list<t> getforlist (class<t> clazz, String sql, objeect ... args); Queries multiple records, returning the corresponding list of objects.
--4) <E> E getforvalue (String sql, Object ... args); Returns the value of a field in a record or the number of records for a statistic.
3. Realize:
Public classDAO { Public intUpdate (String sql, Object ... args) {Connection conn=NULL; PreparedStatement PS=NULL; intRowNum = 0; Try{conn=jdbcutils.getconnection (); PS=conn.preparestatement (SQL); for(inti = 0; i < args.length; i++) {Ps.setobject (i+ 1, Args[i]); } rowNum=ps.executeupdate (); returnRowNum; }Catch(Exception e) {e.printstacktrace (); }finally{jdbcutils.release (conn, PS,NULL); } returnRowNum; } Public<T> T Get (class<t>clazz, String sql, Object ... args) {T entity=NULL; Connection Conn=NULL; PreparedStatement PS=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); PS=conn.preparestatement (SQL); for(inti = 0; i < args.length; i++) {Ps.setobject (i+ 1, Args[i]); } RS=Ps.executequery (); Map<string, object> map =NewHashmap<string, object>(); List<String> Columnlabels =Getcolumnlabels (RS); if(Rs.next ()) { for(String columnlabel:columnlabels) {Object Columnvalue=Rs.getobject (ColumnLabel); Map.put (ColumnLabel, Columnvalue); } } if(Map.size () > 0) {Entity=clazz.newinstance (); for(Map.entry<string, object>Entry:map.entrySet ()) {String FieldName=Entry.getkey (); Object value=Entry.getvalue (); //Use the Beanutils tool class to assign a value to a propertyBeanutils.setproperty (Entity, fieldName, value); /**assign a value to a property using reflection field field = Clazz.getdeclaredfield (key); Field.setaccessible (TRUE); Field.set (entity, value);*/ } returnentity; } }Catch(Exception e) {e.printstacktrace (); }finally{jdbcutils.release (conn, PS, RS); } returnentity; } Public<T> list<t> getforlist (class<t>clazz, String sql, Object ... args) {List<T> list =NewArraylist<>(); Connection Conn=NULL; PreparedStatement PS=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); PS=conn.preparestatement (SQL); for(inti = 0; i < args.length; i++) {Ps.setobject (i+ 1, Args[i]); } RS=Ps.executequery (); /*** Convert the resulting resultset result set to map<string, object> list, * where the key value is the alias of the RS table, the value value is the corresponding alias /c4>*/List<map<string, object>> data =Handleresultsettomaplist (RS); /*** Convert maplist to corresponding beanlist*/List=transfermaplisttobeanlist (clazz, data); }Catch(Exception e) {e.printstacktrace (); }finally{jdbcutils.release (conn, PS, RS); } returnlist; } Private<T> list<t> transfermaplisttobeanlist (class<t> clazz,list<map<string, Object>> data)throwsException {List<T> list =NewArraylist<>(); if(Data.size () > 0){ for(Map<string, object>map:data) {T entity=clazz.newinstance (); for(Map.entry<string, object>Entry:map.entrySet ()) {String FieldName=Entry.getkey (); Object value=Entry.getvalue (); Beanutils.setproperty (Entity, fieldName, value);; } list.add (entity); } } returnlist; } Public Static<E>e getforvalue (String sql, Object ... args) {e entity=NULL; Connection Conn=NULL; PreparedStatement PS=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); PS=conn.preparestatement (SQL); for(inti = 0; i < args.length; i++) {Ps.setobject (i+ 1, Args[i]); } RS=Ps.executequery (); if(Rs.next ()) {return(E) Rs.getobject (1); } }Catch(Exception e) {e.printstacktrace (); }finally{jdbcutils.release (conn, PS, RS); } returnentity; } /*** Process The obtained result set into Maplist *@paramRS *@return * @throwsException*/ PrivateList<map<string, object>>handleresultsettomaplist (ResultSet rs)throwsException {List<String> Columnlabels =Getcolumnlabels (RS); List<map<string, object>> data =NewArraylist<map<string, object>>(); while(Rs.next ()) {Map<string, object> map =NewHashmap<string, object>(); for(String columnlabel:columnlabels) {Object Columnvalue=Rs.getobject (ColumnLabel); Map.put (ColumnLabel, Columnvalue); } data.add (map); } returndata; } /*** Get aliases for all columns based on the result set *@paramRS *@return * @throwsException*/ PrivateList<string> Getcolumnlabels (ResultSet rs)throwsexception{List<String> list =NewArraylist<>(); ResultSetMetaData RSMD=Rs.getmetadata (); for(inti = 0; I < Rsmd.getcolumncount (); i++) {List.add (Rsmd.getcolumnlabel (i+ 1)); } returnlist; }}
Jdbc--dao design mode