springMVC+mybatis 之 借助SqlSessionTemplate 实现Dao层通用写法

在springMVC+mybatis框架开发时,通常我们会写多的dao,如userDao,roleDao,departDao等等,这样开发效果就很慢,不是很好,所以编写个通用dao.

1.编写dao接口

[java]  view plain  copy
  1. public interface DAO {  
  2.       
  3.     /** 
  4.      * 保存对象 
  5.      * @param str 
  6.      * @param obj 
  7.      * @return 
  8.      * @throws Exception 
  9.      */  
  10.     public Object save(String str, Object obj) throws Exception;  
  11.       
  12.     /** 
  13.      * 修改对象 
  14.      * @param str 
  15.      * @param obj 
  16.      * @return 
  17.      * @throws Exception 
  18.      */  
  19.     public Object update(String str, Object obj) throws Exception;  
  20.       
  21.     /** 
  22.      * 删除对象  
  23.      * @param str 
  24.      * @param obj 
  25.      * @return 
  26.      * @throws Exception 
  27.      */  
  28.     public Object delete(String str, Object obj) throws Exception;  
  29.   
  30.     /** 
  31.      * 查找对象 
  32.      * @param str 
  33.      * @param obj 
  34.      * @return 
  35.      * @throws Exception 
  36.      */  
  37.     public Object findForObject(String str, Object obj) throws Exception;  
  38.   
  39.     /** 
  40.      * 查找对象 
  41.      * @param str 
  42.      * @param obj 
  43.      * @return 
  44.      * @throws Exception 
  45.      */  
  46.     public Object findForList(String str, Object obj) throws Exception;  
  47.       
  48.     /** 
  49.      * 查找对象封装成Map 
  50.      * @param s 
  51.      * @param obj 
  52.      * @return 
  53.      * @throws Exception 
  54.      */  
  55.     public Object findForMap(String sql, Object obj, String key , String value) throws Exception;  
  56.       
  57. }  
2.编写dao 实现类

[java]  view plain  copy
  1. public class DaoSupport implements DAO {  
  2.   
  3.     @Resource(name = "sqlSessionTemplate")  
  4.     private SqlSessionTemplate sqlSessionTemplate;  
  5.       
  6.     /** 
  7.      * 保存对象 
  8.      * @param str 
  9.      * @param obj 
  10.      * @return 
  11.      * @throws Exception 
  12.      */  
  13.     public Object save(String str, Object obj) throws Exception {  
  14.         return sqlSessionTemplate.insert(str, obj);  
  15.     }  
  16.       
  17.     /** 
  18.      * 批量更新 
  19.      * @param str 
  20.      * @param obj 
  21.      * @return 
  22.      * @throws Exception 
  23.      */  
  24.     public Object batchSave(String str, List objs )throws Exception{  
  25.         return sqlSessionTemplate.insert(str, objs);  
  26.     }  
  27.       
  28.     /** 
  29.      * 修改对象 
  30.      * @param str 
  31.      * @param obj 
  32.      * @return 
  33.      * @throws Exception 
  34.      */  
  35.     public Object update(String str, Object obj) throws Exception {  
  36.         return sqlSessionTemplate.update(str, obj);  
  37.     }  
  38.   
  39.     /** 
  40.      * 批量更新 
  41.      * @param str 
  42.      * @param obj 
  43.      * @return 
  44.      * @throws Exception 
  45.      */  
  46.     public void batchUpdate(String str, List objs )throws Exception{  
  47.         SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();  
  48.         //批量执行器  
  49.         SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);  
  50.         try{  
  51.             if(objs!=null){  
  52.                 for(int i=0,size=objs.size();i<size;i++){  
  53.                     sqlSession.update(str, objs.get(i));  
  54.                 }  
  55.                 sqlSession.flushStatements();  
  56.                 sqlSession.commit();  
  57.                 sqlSession.clearCache();  
  58.             }  
  59.         }finally{  
  60.             sqlSession.close();  
  61.         }  
  62.     }  
  63.       
  64.     /** 
  65.      * 批量更新 
  66.      * @param str 
  67.      * @param obj 
  68.      * @return 
  69.      * @throws Exception 
  70.      */  
  71.     public Object batchDelete(String str, List objs )throws Exception{  
  72.         return sqlSessionTemplate.delete(str, objs);  
  73.     }  
  74.       
  75.     /** 
  76.      * 删除对象  
  77.      * @param str 
  78.      * @param obj 
  79.      * @return 
  80.      * @throws Exception 
  81.      */  
  82.     public Object delete(String str, Object obj) throws Exception {  
  83.         return sqlSessionTemplate.delete(str, obj);  
  84.     }  
  85.        
  86.     /** 
  87.      * 查找对象 
  88.      * @param str 
  89.      * @param obj 
  90.      * @return 
  91.      * @throws Exception 
  92.      */  
  93.     public Object findForObject(String str, Object obj) throws Exception {  
  94.         return sqlSessionTemplate.selectOne(str, obj);  
  95.     }  
  96.   
  97.     /** 
  98.      * 查找对象 
  99.      * @param str 
  100.      * @param obj 
  101.      * @return 
  102.      * @throws Exception 
  103.      */  
  104.     public Object findForList(String str, Object obj) throws Exception {  
  105.         return sqlSessionTemplate.selectList(str, obj);  
  106.     }  
  107.       
  108.     public Object findForMap(String str, Object obj, String key, String value) throws Exception {  
  109.         return sqlSessionTemplate.selectMap(str, obj, key);  
  110.     }  
  111.       
  112. }  
3.那么怎么使用呢?

[java]  view plain  copy
  1. public class UserService {  
  2.   
  3.     @Resource(name = "daoSupport")  
  4.     private DaoSupport dao;  
  5.   
  6.     /* 
  7.     *通过id获取数据 
  8.     */  
  9.     public User getUserAndRoleById(String userid) throws Exception {  
  10.         return (User) dao.findForObject("UserMapper.getUserAndRoleById", <span style="font-family: Arial, Helvetica, sans-serif;">userid</span>);  
  11.     }  
  12.       
  13. }  
4. UserMapper.xml中的配置

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4. <mapper namespace="UserMapper">  
[java]  view plain  copy
  1. <select id="getUserById" parameterType="String" resultType="User">  
  2.     select  * from SYS_USER where userId=#{userId}  
  3. </select>  
  4. lt;/mapper>  


namespqce:是随意起的 不是规定必须叫 userNamespace

3.mybatis配置文件 

[xml]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3.     PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  
  4.     "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
  5. <mapper namespace="Test">  
  6.   
  7.     <select id="queryDataCount"  
  8.         parameterType="com.mybatis.Test"  
  9.         resultType="int">  
  10.         SELECT COUNT(1) FROM INF_CONSUMER T WHERE T.UUID = #{uuid}  
  11.     </select>  
  12.       
  13. </mapper>  


4.具体DAO配置  

[java]  view plain  copy
  1. package com.dwr;  
  2.   
  3. import org.springframework.stereotype.Repository;  
  4.   
  5. import com.mybatis.Test;  
  6.   
  7. @Repository  
  8. public class TestDAO extends MyBatisDAO {  
  9.   
  10.     public int getCount(Test test) {  
  11.         return (Integer) this.getObject("Test.queryDataCount", test);  
  12.     }  
  13. }  




好了,就这样,是不是觉得很简单,直接释放出dao层,一个通用dao.

猜你喜欢

转载自blog.csdn.net/hellolovelife/article/details/78476099