mybaits之dao层通用写法sqlsessiontemplate

1.编写dao接口

public interface DAO {  
      
    /** 
     * 保存对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object save(String str, Object obj) throws Exception;  
      
    /** 
     * 修改对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object update(String str, Object obj) throws Exception;  
      
    /** 
     * 删除对象  
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object delete(String str, Object obj) throws Exception;  
  
    /** 
     * 查找对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object findForObject(String str, Object obj) throws Exception;  
  
    /** 
     * 查找对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object findForList(String str, Object obj) throws Exception;  
      
    /** 
     * 查找对象封装成Map 
     * @param s 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object findForMap(String sql, Object obj, String key , String value) throws Exception;  
      
}  
2.编写dao 实现类
public class DaoSupport implements DAO {  
  
    @Resource(name = "sqlSessionTemplate")  
    private SqlSessionTemplate sqlSessionTemplate;  
      
    /** 
     * 保存对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object save(String str, Object obj) throws Exception {  
        return sqlSessionTemplate.insert(str, obj);  
    }  
      
    /** 
     * 批量更新 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object batchSave(String str, List objs )throws Exception{  
        return sqlSessionTemplate.insert(str, objs);  
    }  
      
    /** 
     * 修改对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object update(String str, Object obj) throws Exception {  
        return sqlSessionTemplate.update(str, obj);  
    }  
  
    /** 
     * 批量更新 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public void batchUpdate(String str, List objs )throws Exception{  
        SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();  
        //批量执行器  
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);  
        try{  
            if(objs!=null){  
                for(int i=0,size=objs.size();i<size;i++){  
                    sqlSession.update(str, objs.get(i));  
                }  
                sqlSession.flushStatements();  
                sqlSession.commit();  
                sqlSession.clearCache();  
            }  
        }finally{  
            sqlSession.close();  
        }  
    }  
      
    /** 
     * 批量更新 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object batchDelete(String str, List objs )throws Exception{  
        return sqlSessionTemplate.delete(str, objs);  
    }  
      
    /** 
     * 删除对象  
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object delete(String str, Object obj) throws Exception {  
        return sqlSessionTemplate.delete(str, obj);  
    }  
       
    /** 
     * 查找对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object findForObject(String str, Object obj) throws Exception {  
        return sqlSessionTemplate.selectOne(str, obj);  
    }  
  
    /** 
     * 查找对象 
     * @param str 
     * @param obj 
     * @return 
     * @throws Exception 
     */  
    public Object findForList(String str, Object obj) throws Exception {  
        return sqlSessionTemplate.selectList(str, obj);  
    }  
      
    public Object findForMap(String str, Object obj, String key, String value) throws Exception {  
        return sqlSessionTemplate.selectMap(str, obj, key);  
    }  
      
}  

3.那么怎么使用呢?

public class UserService {  
  
    @Resource(name = "daoSupport")  
    private DaoSupport dao;  
  
    /* 
    *通过id获取数据 
    */  
    public User getUserAndRoleById(String userid) throws Exception {  
        return (User) dao.findForObject("UserMapper.getUserAndRoleById", <span style="font-family: Arial, Helvetica, sans-serif;">userid</span>);  
    }  
      
} 
4. UserMapper.xml中的配置

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
<mapper namespace="UserMapper">  

[java] view plain copy
<select id="getUserById" parameterType="String" resultType="User">  
    select  * from SYS_USER where userId=#{userId}  
</select>  
lt;/mapper>  

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

引用地址 http://blog.csdn.net/u014593633/article/details/52368818

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值