MyBatis框架与Spring框架的结合方式使用sqlSessionTemplate进行构造注入
Posted xingpeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis框架与Spring框架的结合方式使用sqlSessionTemplate进行构造注入相关的知识,希望对你有一定的参考价值。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" > <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <!-- <property name="url"> <value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms? useUnicode=true&characterEncoding=utf-8]]></value> </property> --> <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms? useUnicode=true&characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- 配置SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用数据源组件 --> <property name="dataSource" ref="dataSource" /> <!-- 引用MyBatis配置文件中的配置 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 配置SQL映射文件信息 --> <property name="mapperLocations"> <list> <value>classpath:cn/smbms/dao/**/*.xml</value> </list> </property> </bean> <!-- 配置SqlSessionTemplate --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 配置DAO --> <bean id="userMapper" class="cn.smbms.dao.user.UserMapperImpl"> <property name="sqlSession" ref="sqlSessionTemplate" /> </bean> <!-- 配置业务Bean --> <bean id="userService" class="cn.smbms.service.user.UserServiceImpl"> <property name="userMapper" ref="userMapper" /> </bean> </beans>
Daoimpl层里面继承SqlSessionDaoSupport,可以简化代码,直接获取到SqlSession,
SqlSessionDaoSupport里面有setSqlSessionFactory可以进行设值注入。
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper { /* private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; }*/ @Override public List<User> getUserList(User user) { //使用SqlSessionTemplate //return sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserList", user); //使用SqlSessionDaoSupport return this.getSqlSession().selectList("cn.smbms.dao.user.UserMapper.getUserList", user); } /*public SqlSessionTemplate getSqlSession() { return sqlSession; }
更改applicationContext.xml
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <!-- <property name="url"> <value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms? useUnicode=true&characterEncoding=utf-8]]></value> </property> --> <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms? useUnicode=true&characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- 配置SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用数据源组件 --> <property name="dataSource" ref="dataSource" /> <!-- 引用MyBatis配置文件中的配置 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 配置SQL映射文件信息 --> <property name="mapperLocations"> <list> <value>classpath:cn/smbms/dao/**/*.xml</value> </list> </property> </bean> <!-- 配置SqlSessionTemplate --> <!-- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> --> <!-- 配置DAO --> <bean id="userMapper" class="cn.smbms.dao.user.UserMapperImpl"> <!-- <property name="sqlSession" ref="sqlSessionTemplate" /> --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 配置业务Bean --> <bean id="userService" class="cn.smbms.service.user.UserServiceImpl"> <property name="userMapper" ref="userMapper" /> <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> --> </bean>
以上是关于MyBatis框架与Spring框架的结合方式使用sqlSessionTemplate进行构造注入的主要内容,如果未能解决你的问题,请参考以下文章