开发者学堂课程【Java Web 开发系列课程:Spring 框架入门:spring 整合mybatis02】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/538/detail/7352
spring 整合 mybatis02
内容简介:
一、SqISessionDaoSupport 介绍
二、继承 SqlSessionDaoSupport
三、Dao 的实现
一、SqISessionDaoSupport 介绍
SqlSessionDaosupport is an abstract support class that provides you with a sqlsession. Calling getsqlsession() you will get a SqlsessionTerplate which can then be used to execute SOL methods, like the following:
public class UserDaoImpl extends SqlsessionDaoSupport implements UserDao{
public User getUser(String userId){
return(User)getSqlSession().selectOne("ong.mybatis.spring,sample,mapper,UserMapper,getUser", userId);
}
}
Usually MappezFactorybean is preferred to this class, since it requires no extra code. But,this class is useful if you need to do other non-MyBatis work in your DAO and concrete classes are required
SqlSesaionDaosupport requires either an sgisescionfactory or an agiseasiontemplate property to be set. lf both properties are set, the sg15essionEactory is ignored.
Assuming a class UserDaoImpl that subclasses sqlsessionDaoSupport , it can be configured in Spring like the following
二、继承SqlSessionDaoSupport
使用 mybatis-spring-1.2.3整合;在 spring 配置文件中不需要管理sqlSessionTemplate 在 Dao 的实现中,需要继承 SqlSessionDaoSupport。
class="org.mybatis.spring.SqlSessionFactoryBean">
value="classpath:mybatis.cfg.xml"/>
ref="sqlSessionFactory"/>
三、Dao 的实现
public class UserDaoImpl extends SqlsessionDaosupport implements UserDao
@Override
public List selectUser(){
return
this.getSqlsession().selectList("cn.sxt.vo.user.mapper.selec tAll");
}
}