当前位置: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属性,就像下面的代码:

    1. public class UserDaoImpl implements UserDao{  
    2.     private SqlSession sqlSession;  
    3.     public void setSqlSession(SqlSession sqlSession){  
    4.         this.sqlSession = sqlSession;  
    5.     }  
    6.     public User getuser(String userId){  
    7.         return (User)sqlSession.selectOne  
    8.         ("org.mybatis.spring.sample.mapper.UserMapper.getUser",userId);  
    9.     }  
    10. }  

    如下注入SqlSessionTemplate:

    1. <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">  
    2.     <property name="sqlSession" ref="sqlSession"/>  
    3. </bean>  

    MyBatis-Spring的sqlSessionTemplate

    标签:factor   默认   select   ons   注入   set   nts   方法   get   

    人气教程排行