mybatis的基础Dao

话不多说,直接贴代码吧,因为很多博客都需要用到这个基础dao,怕大家不好查询。

这个基类主要是使用了泛型,这样我就不必为每一个实体都写一个dao,大大节省了时间。其中sqlSessionTemplate是在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 }

猜你喜欢

转载自www.cnblogs.com/roy-blog/p/9025786.html