mybatis的基礎Dao

話很少說,直接貼代碼吧,由於不少博客都須要用到這個基礎dao,怕你們很差查詢。java

這個基類主要是使用了泛型,這樣我就沒必要爲每個實體都寫一個dao,大大節省了時間。其中sqlSessionTemplate是在spring 配置文件配置的數據模板。spring

  1 package com.xdx.dao;
  2 
  3 import java.io.Serializable;
  4 import java.lang.reflect.ParameterizedType;
  5 import java.lang.reflect.Type;
  6 import java.util.List;
  7 import java.util.Map;
  8 
  9 import javax.annotation.Resource;
 10 
 11 import org.mybatis.spring.SqlSessionTemplate;
 12 import org.springframework.stereotype.Repository;
 13 
 14 /**
 15  * 全部dao基類
 16  * 
 17  * @author xdx
 18  *
 19  * @param <T>
 20  * @param <PK>
 21  */
 22 @Repository("baseDao")
 23 public  class BaseDao<T, PK extends Serializable> {
 24     static{
 25         System.out.println("加載BaseDao");
 26     }
 27     private Class<T> enetityClass;
 28     @Resource(name = "sqlSessionTemplate")
 29     private SqlSessionTemplate sqlSessionTemplate;
 30 
 31     // 構造方法,根據實例類自動獲取實體類型,這邊利用java的反射
 32     public BaseDao() {
 33         this.enetityClass = null;
 34         Class c = getClass();
 35         Type t = c.getGenericSuperclass();
 36         if (t instanceof ParameterizedType) {
 37             ParameterizedType p = (ParameterizedType) t;
 38             Type[] type = p.getActualTypeArguments();
 39             this.enetityClass = (Class<T>) type[0];
 40         }
 41         System.out.println(this+"實例化BaseDao");
 42     }
 43 
 44     /**
 45      * 獲取實體
 46      * 
 47      * @param id
 48      * @return
 49      */
 50     public T getT(String sql, Object param) {
 51         return sqlSessionTemplate.selectOne(sql, param);
 52     }
 53     /**
 54      * 獲取map
 55      * @param sql
 56      * @return
 57      */
 58     public List<Map<String,Object>>findMapList(String sql){
 59         return sqlSessionTemplate.selectList(sql);
 60     }
 61     /**
 62      * 不帶查詢參數的列表
 63      * @param str
 64      * @return
 65      * @throws Exception
 66      */
 67     public List<T> findTList(String sql){
 68         return sqlSessionTemplate.selectList(sql);
 69     }
 70     /**
 71      * 根據param獲取Map形式返回的list
 72      * @param sql
 73      * @param param
 74      * @return
 75      */
 76     public List<Map<String,Object>>findMapListByPm(String sql,Object param){
 77         return sqlSessionTemplate.selectList(sql, param);
 78     }
 79 
 80     /**
 81      * 帶有參數的列表
 82      * 
 83      * @param str
 84      * @param param
 85      * @return
 86      * @throws Exception
 87      */
 88     public List<T> findTListByParam(String sql, Object param) {
 89         return sqlSessionTemplate.selectList(sql, param);
 90     }
 91 
 92     /**
 93      * 插入一條數據,參數是t
 94      * 
 95      * @param sql
 96      * @param t
 97      * @return
 98      */
 99     public int addT(String sql, T t) {
100         return sqlSessionTemplate.insert(sql, t);
101     }
102     /**
103      * 修改一條數據,參數是t
104      * @param sql
105      * @param t
106      * @return
107      */
108     public int updateT(String sql,T t){
109         return sqlSessionTemplate.update(sql, t);
110     }
111     /**
112      * 修改
113      */
114     public int updateBySql(String sql,Object obj){
115         return sqlSessionTemplate.update(sql, obj);
116     }
117     /**
118      * 刪除t,參數是主鍵
119      * @param sql
120      * @param t
121      * @return
122      */
123     public int deleteT(String sql,PK pk){
124         return sqlSessionTemplate.delete(sql, pk);
125     }
126     /**
127      * 根據param獲取一個對象
128      * @param sql
129      * @param param
130      * @return
131      */
132     public Object getObject(String sql,Object param){
133         return sqlSessionTemplate.selectOne(sql,param);
134     }
135     public Object getObject(String sql){
136         return sqlSessionTemplate.selectOne(sql);
137     }
138 }
相關文章
相關標籤/搜索