mybatis SqlSessionFacto SqlSession配置注入
来源:互联网 发布:linux who am i 编辑:程序博客网 时间:2024/06/06 01:49
SqlSessionFactory 有一个单独的必须属性,就是 JDBC 的 DataSource。这可以是任意 的 DataSource,其配置应该和其它 Spring 数据库连接是一样的。
一个通用的属性是 configLocation,它是用来指定 MyBatis 的 XML 配置文件路径的。 如果基本的 MyBatis 配置需要改变, 那么这就是一个需要它的地方。 通常这会是<settings> 或<typeAliases>的部分。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" /></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /></bean
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 { 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>SqlSessionTemplate 有一个使用 ExecutorType 作为参数的构造方法。这允许你用来 创建对象,比如,一个批量 SqlSession,但是使用了下列 Spring 配置的 XML 文件:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> <constructor-arg index="1" value="BATCH" /></bean>现在你所有的语句可以批量操作了,下面的语句就可以在 DAO 中使用了。
public void insertUsers(User[] users) { for (User user : users) { sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", user); } }SqlSessionDaoSupport
SqlSessionDaoSupport 是 一 个 抽象 的支 持 类, 用来 为你 提供 SqlSession 。 调 用 getSqlSession()方法你会得到一个 SqlSessionTemplate,之后可以用于执行 SQL 方法, 就像下面这样:
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { public User getUser(String userId) { return (User) getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); }}通常 MapperFactoryBean 是这个类的首选,因为它不需要额外的代码。但是,如果你 需要在 DAO 中做其它非 MyBatis 的工作或需要具体的类,那么这个类就很有用了。
SqlSessionDaoSupport 需要一个 sqlSessionFactory 或 sqlSessionTemplate 属性来 设 置 。 这 些 被 明 确 地 设 置 或 由 Spring 来 自 动 装 配 。 如 果 两 者 都 被 设 置 了 , 那 么 SqlSessionFactory 是被忽略的。
假设类 UserMapperImpl 是 SqlSessionDaoSupport 的子类,它可以在 Spring 中进行如 下的配置:
<bean id="userMapper" class="org.mybatis.spring.sample.mapper.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>
- mybatis SqlSessionFacto SqlSession配置注入
- Mybatis-Spring 的高级配置(SqlSession)
- Mybatis SqlSession
- mybatis sqlsession
- MyBatis sqlSession
- MyBatis-Spring-SqlSession
- 深入浅出MyBatis-Sqlsession
- mybatis sqlSession 深入分析
- 深入浅出MyBatis-Sqlsession
- 深入浅出MyBatis-Sqlsession
- 深入浅出MyBatis-Sqlsession
- 深入浅出MyBatis-Sqlsession
- 深入浅出MyBatis-Sqlsession
- 深入浅出MyBatis-Sqlsession
- 【SSM】Mybatis之SqlSession
- Mybatis(二)-----------------Sqlsession
- MyBatis-Spring-SqlSession
- 深入浅出MyBatis-Sqlsession
- android 屏幕适配1
- C#与SQL Server数据库操作
- Android: ImageView and Bitmap
- Oracle内连接、外连接
- 学习笔记02—枚举
- mybatis SqlSessionFacto SqlSession配置注入
- 谷歌携Blink来势汹汹 WebKit将成明日黄花?
- System.out.println与System.err.println的区别
- HDU 2546 饭卡( 01背包 )
- JS常用代码(4)
- 项目经理杂谈(上)_思考为何年薪没到50万!有步骤的去实现!
- oooooooookkkkkkkkkkkkkkkkkkkkkkkkk
- 汇编程序
- Android应用程序的生命周期