当前位置:Gxlcms >
数据库问题 >
MyBatis-Spring的sqlSessionTemplate
MyBatis-Spring的sqlSessionTemplate
时间:2021-07-01 10:21:17
帮助过:16人阅读
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
这个bean现在可以直接注入到DAO bean中。你需要在bean中添加一个SqlSession属性,就像下面的代码:
- public class UserDaoImpl implements UserDao{
- private SqlSession sqlSession;
- public void setSqlSession(SqlSession sqlSession){
- this.sqlSession = sqlSession;
- }
- public User getuser(String userId){
- return (User)sqlSession.selectOne
- ("org.mybatis.spring.sample.mapper.UserMapper.getUser",userId);
- }
- }
如下注入SqlSessionTemplate:
- <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
- <property name="sqlSession" ref="sqlSession"/>
- </bean>
MyBatis-Spring的sqlSessionTemplate
标签:factor 默认 select ons 注入 set nts 方法 get