Interface class:
PackageCom.blog.db.dao;Importcom.blog.util.Pagination;Importjava.util.List; Public InterfacePublicdao<t> { Public voidSetmapper (Class t); Public voidSettablename (String tn); PublicList<t>querylist (pagination pagination); Public voidDeletebyid (intID); Public voidAdd (T t,object[] values,int[] types); PublicT Querybyid (intID); PublicT Querybyfield (String fieldname,string value); Public voidUpdatebyid (T T,intId,object[] values,int[] types);}
Implementation class:
PackageCom.blog.db.dao.impl;ImportCom.blog.db.dao.PublicDao;Importcom.blog.util.Pagination;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.dao.EmptyResultDataAccessException;ImportOrg.springframework.jdbc.core.BeanPropertyRowMapper;Importorg.springframework.jdbc.core.JdbcTemplate;ImportOrg.springframework.jdbc.core.RowMapper;Importorg.springframework.stereotype.Repository;ImportJavax.annotation.Resource;Importjava.sql.Types;Importjava.util.List;/*** Public DAO Implementation *@deprecatedprovide common lists, insert, query single, update data Services *@authorZTF **/@Repository ("Publicdao") Public classPublicdaoimpl<t>ImplementsPublicdao<t>{@ResourcePrivateJdbcTemplate JdbcTemplate; PrivateString table_name = "";//Table name Privatelist<string> fields;//table Field@Value ("${db_schema}") PrivateString schema;//Table Mode PrivateRowmapper<t>mapper; /*** Set Mapper mappings*/ Public voidSetmapper (Class t) { This. Mapper =NewBeanpropertyrowmapper<t>(t); } PublicRowmapper<t>Getmapper () {returnmapper; } Public voidSettablename (String tn) { This. table_name =TN; } PublicString Gettable_name () {returnSchema+ "." +table_name; } PublicList<string>GetFields () {return This. Fields; } Publicstring getfieldstostring () {string _fields; List<String> fields =GetFields (); _fields= String.Join (",", fields); return_fields; } /*** Query list *@parampagination The paging object defined in the incoming public util *@returnlist<t> **/ PublicList<t>querylist (pagination pagination) {List lists=NULL; String SQL= "SELECT * from??"; Lists=jdbctemplate.queryforlist (SQL, Gettable_name (), Pagination.getsql (), Getmapper ()); returnlists; } /*** Delete a line by ID *@paramID incoming Line ID **/ Public voidDeletebyid (intID) {String SQL= "Delete from" +Gettable_name ()+ "where id=?"; Jdbctemplate.update (SQL,Newobject[] {ID},New int[] {types.integer}); } /*** Insert Data *@paramT data object for incoming operation *@paramobject[] Object * for values@paramtypes The type of the value object **/ Public voidAdd (T t,object[] values,int[] types) {String SQL= "INSERT INTO" +gettable_name () + "(" +getfieldstostring () + ") VALUES (?)"; Jdbctemplate.update (SQL, values, types); } /*** Query the specified line based on ID *@paramID Incoming ID **/ PublicT Querybyid (intid) {T t=NULL; String SQL= "SELECT * from" +gettable_name () + "where id=?"; Try{T=jdbctemplate.queryforobject (SQL,Newobject[] {ID},New int[] {Types.integer}, Getmapper ()); }Catch(emptyresultdataaccessexception e) {T=NULL; } returnT; } /*** Single-line data with specified values based on the specified fieldname query *@paramfieldname Incoming ID **/ PublicT Querybyfield (String fieldname,string value) {T T=NULL; String SQL= "SELECT * from" +gettable_name () + "where" +fieldname+ "=?"; Try{T=jdbctemplate.queryforobject (SQL,Newobject[] {value},New int[] {Types.varchar}, Getmapper ()); }Catch(emptyresultdataaccessexception e) {T=NULL; } returnT; } /*** Update a single row of data for the specified value according to the specified ID *@paramT incoming update object *@paramID passed in the specified ID *@paramobject[] Object * for values@paramtype of types value object int[] **/ Public voidUpdatebyid (T T,intId,object[] values,int[] types) {String SQL= "Update" +gettable_name () + "set username=?,password=?,pic=?,describe=?,check_time=?" where id= "+ID; Jdbctemplate.update (SQL, values, types); }}
The core of the place in: How To pass Mapper
Here, I'm using a
Private rowmapper<t> mapper; /** * Set Mapper map */public void setmapper (Class t) { Thisnew beanpropertyrowmapper<t>(T); } Public Rowmapper<t> Getmapper () { return mapper; }
The. Class object that passed in the T class when calling DAO
Call for example (partial code):
@Qualifier ("Publicdao") private publicdao<user> ud; Public throws unsupportedencodingexception { = md5encryption.getencryption (password); Ud.settablename ("users"); Specifies the query table name ud.setmapper (User. class // specifying the Mapping class object
Can realize the basic table additions and deletions, improve the development efficiency,
If you have a special DAO, you can customize a specific DAO and add special methods.
"Java Learning" Spring MVC Common DAO Implementation, definition of basic additions and deletions