基于Spring jdbcTemple做的泛型DAO_第1页
免费预览已结束,剩余4页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、基于spring jdbctemple做的泛型dao采纳java的反射机制和spring jdbctemplate完成的泛型dao public interface basedao t, pk tends serializable * a one record fution * param entity * description: public int add(t entity); * add multi record function * param entitycollection * description: public int addlist(collection t entity

2、collection, class t entityclazz); * delete one record function * param entity * description: public int delete(t entity); * delete one record by * param entityclass * param id * description: public int deletebyid(class t entityclass, pk id); * delete many record function * param entitycollection * p

3、aram entityclazz * description: public int deletelist(collection t entitycollection, class t entityclazz); * up the record * param entity * description: public int update(t entity); * update many records meanwhile * param entitycollection * param entityclazz * description: public int updatelist(coll

4、ection t entitycollection, class t entityclazz); * query all record list * param entityclazz * return * description: public list t all(class t entityclazz); * find one special record * return * description: public t findbyid(class t entityclass, pk id); repository("basedao") public class b

5、asedaoimpl t, pk extends serializable extends simpledaosupport implements basedao t, pk private final sing table_prefix = "pub_" * return * description: protect simplejdbcinsert getsimplejdbcinsert() return new simplejdbcinsert(this.getjdbctemplate(); * (non-javadoc) * see com.morningstar.

6、planning.dao.basedaoadd(java.lang.class) override public int add(t entity) string tablename = this.table_prefix + entity.getclass().getsimplename(); simplejdbcinsert insertactor = getsimplejdbcinsert(); insertactor.tablename(tablename.tolowercase(); return insertactor.exee(new beanpropertysqlparamet

7、ersource(entity); * (non-javadoc) * see com.morningstar.planning.dao.basedaoaddall(java.util.collection) override public int addlist(collection t entitycollection, class t entityclazz) string tablename = this.table_prefix + entityclazz.getsimplename(); simplejdbcinsert insertactor = getsimplejdbcins

8、ert(); sqlparametersource batchargs = sqlparametersourceuti.createbatch(entitycollection.toarray(); insertactor.settablename(tablename.tolowercase(); int relt = insertactor.executebatch(batchargs); return result.length; * (non-javadoc) * see com.morningstar.planning.dao.basedaodelete(java.lang.objec

9、t) override public int delete(t entity) string tablename = this.table_prefix + entity.getclass().getsimplename(); string sql = "delete from " + tablename + " where id =:id" return this.getsimplejdbctemplate().update(sql, new beanpropertysqlparametersource(entity); * (non-javadoc)

10、 * see com.morningstar.planning.dao.basedaodeletebyid(java.lang.class, * java.io.serializable) override public int deletebyid(class t entityclazz, pk id) string tablename = this.table_prefix + entityclazz.getsimplename(); string sql = "delete from " + tablename + " where id=?" re

11、turn this.getsimplejdbctemplate().update(sql, id); * (non-javadoc) * see * com.morningstar.planning.dao.basedaodeletelist(java.util.collection, * java.lang.class) override public int deletelist(collection t entitycollection, class t entityclazz) string tablename = this.table_prefix + entityclazz.get

12、simplename(); string sql = "delete from " + tablename + " where id=:id" sqlparametersource batchargs = sqlparametersourceutils.createbatch(entitycollection.toarray(); int result = this.getsimplejdbctemplate().batchupdate(sql, batchargs); return result.length; * (non-javadoc) * se

13、e com.morningstar.planning.dao.basedaoupdate(java.lang.object) override suppresswarnings("rawtypes") public int update(t entity) string tablename = this.table_prefix + entity.getclass().getsimplename(); stringbuffer sql = new stringbuffer("update " + tablename + " set "

14、); field fields = entity.getclass().getdeclaredfields(); for (int i = 0; i fields.length; i+) field fied = fieldsi; string fiedname = fied.getname(); class fiedtype = fied.gettype(); if (!fiedname.equalsignorecase("id") !fiedtype.equals(collections.class) !fiedtype.equals(map.class) !fiedt

15、ype.equals(list.class) !fiedtype.equals(set.class) sql.append(fiedname + " = :" + fiedname + ","); sql.replace(sql.lastindexof(","), sql.length(), ""); sql.append(" where id = :id"); sqlparametersource ps = new beanpropertysqlparametersource(entity);

16、 return this.getsimplejdbctemplate().update(sql.tostring(), ps); * (non-javadoc) * see * com.morningstar.planning.dao.basedaoupdatelist(java.util.collection, * java.lang.class) override suppresswarnings("rawtypes") public int updatelist(collection t entitycollection, class t entityclazz) s

17、tring tablename = this.table_prefix + entityclazz.getsimplename(); stringbuffer sql = new stringbuffer("update " + tablename + " set "); field fields = entityclazz.getdeclaredfields(); for (int i = 0; i fields.length; i+) field fied = fieldsi; string fiedname = fied.getname(); cl

18、ass fiedtype = fied.gettype(); if (!fiedname.equalsignorecase("id") !fiedtype.equals(collections.class) !fiedtype.equals(map.class) !fiedtype.equals(list.class) !fiedtype.equals(set.class) sql.append(fiedname + " = :" + fiedname + ","); sql.replace(sql.lastindexof("

19、;,"), sql.length(), ""); sql.append(" where id = :id"); sqlparametersource batchargs = sqlparametersourceutils.createbatch(entitycollection.toarray(); int result = this.getsimplejdbctemplate().batchupdate(sql.tostring(), batchargs); return result.length; * (non-javadoc) * se

20、e com.morningstar.planning.dao.basedaofindall(java.lang.class) override public list t findall(class t entityclazz) string tablename = this.table_prefix + entityclazz.getsimplename(); string sql = "select * from " + tablename; return this.getsimplejdbctemplate().query(sql, beanpropertyrowmapper.newinstance(entityclazz); * (non-javadoc) * see com.morningstar.planning.dao.basedaofindbyid(java.lang.class, * java.io.serializable) override

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论