springMVC+mybatis 之 借助SqlSessionTemplate 实现Dao层通用写法
2017-11-08 10:49
435 查看
在springMVC+mybatis框架开发时,通常我们会写多的dao,如userDao,roleDao,departDao等等,这样开发效果就很慢,不是很好,所以编写个通用dao.
1.编写dao接口
[java] view
plain copy
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 实现类
[java] view
plain copy
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.那么怎么使用呢?
[java] view
plain copy
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中的配置
[java] view
plain copy
<?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>
[xml] view
plain copy
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="Test">
<select id="queryDataCount"
parameterType="com.mybatis.Test"
resultType="int">
SELECT COUNT(1) FROM INF_CONSUMER T WHERE T.UUID = #{uuid}
</select>
</mapper>
4.具体DAO配置
[java] view
plain copy
package com.dwr;
import org.springframework.stereotype.Repository;
import com.mybatis.Test;
@Repository
public class TestDAO extends MyBatisDAO {
public int getCount(Test test) {
return (Integer) this.getObject("Test.queryDataCount", test);
}
}
好了,就这样,是不是觉得很简单,直接释放出dao层,一个通用dao.
1.编写dao接口
[java] view
plain copy
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 实现类
[java] view
plain copy
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.那么怎么使用呢?
[java] view
plain copy
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中的配置
[java] view
plain copy
<?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>
namespqce:是随意起的 不是规定必须叫 userNamespace
3.mybatis配置文件[xml] view
plain copy
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="Test">
<select id="queryDataCount"
parameterType="com.mybatis.Test"
resultType="int">
SELECT COUNT(1) FROM INF_CONSUMER T WHERE T.UUID = #{uuid}
</select>
</mapper>
4.具体DAO配置
[java] view
plain copy
package com.dwr;
import org.springframework.stereotype.Repository;
import com.mybatis.Test;
@Repository
public class TestDAO extends MyBatisDAO {
public int getCount(Test test) {
return (Integer) this.getObject("Test.queryDataCount", test);
}
}
好了,就这样,是不是觉得很简单,直接释放出dao层,一个通用dao.
相关文章推荐
- SpringMVC+MyBatis,连接池采用druid,启动报错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
- ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
- springmvc+mybatis+sql server实现简单登录功能【转】
- Mybatis-Spring SqlSessionTemplate 源码解析
- ssm(Spring+Spring mvc+mybatis)Dao层配置sql的文件——DeptDaoMapper.xml
- MyBatis-Spring的sqlSessionTemplate
- springmvc+mybatis+sql server实现简单登录功能
- 整合maven+spring+springmvc+mybatis实现ssm通用增删查改基础开发框架
- spring-mybatis整合-SqlSessionTemplate
- 通过 sqlSessionTemplate 整合 Spring 和 MyBatis 的方法
- springmvc+mybatis+sql server实现简单登录功能
- MyBatis-Spring整合 注入SqlSessionTemplate
- spring boot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap实现后台权限管理系统源码
- springMVC+mybatis 进行单元测试时 main SqlSessionFactoryBean - Parsed configuration file: 'class path resource' 无限的读取xml文件
- springmvc+mybatis+sql server实现简单登录功能【转】
- springMVC+mybatis 进行单元测试时 main SqlSessionFactoryBean - Parsed configuration file: 'class path resource' 无限的读取xml文件
- Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常
- Spring与mybatis的整合实践之SqlSessionTemplate持久化模板
- 【SSM进阶之路】使用Spring SqlSessionTemplate API实现查询数据
- Spring和Mybatis整合过程中遇到的Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required的异常