当前位置:Gxlcms > 数据库问题 > MyBatis框架与Spring框架的结合方式使用sqlSessionTemplate进行构造注入

MyBatis框架与Spring框架的结合方式使用sqlSessionTemplate进行构造注入

时间:2021-07-01 10:21:17 帮助过:3人阅读

="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&amp;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进行构造注入

标签:return   sql   bms   www   apache   div   jdb   username   users   

人气教程排行