zoukankan      html  css  js  c++  java
  • MyBatis 3与spring整合之使用SqlSession

    SqlSessionTemplate是MyBatis-Spring的核心。这个类负责管理MyBatis的SqlSession。调用MyBatis的SQL方法。

    SqlSessionTemplate是线程安全的,可以被多个DAO所共享使用。

    SqlSessionTemplate对象可以使用SqlSessionFactory作为构造方法的参数来创建。

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

      <constructor-arg index="0" ref="sqlSessionFactory" />

    </bean>

    这个bean现在可以直接注入DAO bean中。你需要在bean中添加一个SqlSession属性。就像下面的代码:

    public class UserDaoImpl implements UserDao {

      public 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>

  • 相关阅读:
    P2949 [USACO09OPEN]工作调度Work Scheduling
    P1279 字串距离 (动态规划)
    P2634 [国家集训队]聪聪可可
    点分治模板
    网站收集
    P1131 [ZJOI2007]时态同步
    P1446 [HNOI2008]Cards
    P1437 [HNOI2004]敲砖块
    蓝桥杯-k倍区间
    atom / vscode (配置c++环境流程)
  • 原文地址:https://www.cnblogs.com/man-li/p/4363133.html
Copyright © 2011-2022 走看看