ssm框架整合---数据库访问方式

在学习mybatis时,我们配置好了数据库连接之后,都是先加载mybatis配置文件为输入流,把这个流交给sqlSessionFactoryBuilder构建一个sqlSessionFactory,根据这个工厂获取sqlSession,通过session获取指定的mapper执行我们的sql命令。

在整合spring时,我们把很大一部分都交给spring来完成。主要有两种方式来实现:

1.通过配置SqlSessionTemplate这个bean,通过这个bean来获取一个sqlsession模板,其功能与sqlsession差不多。

在mybatis的dao中,一般是有一个接口还有一个xml来编写sql命令。现在我们需要添加一个实现类,service可以获取这个实现类的对象从而完成数据库的操作。从而简化了操作。现在目的就是如何编写这个实现类。sql命令的执行都是通过session获取到mapper,通过mapper来执行接口方法。这里也不例外。

刚才我们提到了在bean中添加了SqlSessionTemplate,所以我们可以在实现类中使用这个属性来获取mapper来执行sql。具体代码如下:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入数据库配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置datasource-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <property name="autoCommitOnClose" value="false"/>
        <property name="checkoutTimeout" value="10000"/>
        <property name="acquireRetryAttempts" value="2"/>

    </bean>

    <!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <bean class="org.mybatis.spring.SqlSessionTemplate" id="sessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>
package com.lifue.dao;

import com.lifue.pojo.Book;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class BookMapperImpl implements BookMapper {
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<Book> queryAllBooks() {
        return sqlSession.getMapper(BookMapper.class).queryAllBooks();
    }
}

2.另一种实现方式是通过MapperScannerConfigurer将所有的mapper接口自动添加到spring容器中,在service层中可以直接注入属性。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
    <property name="basePackage" value="com.lifue.dao"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

 

posted @ 2020-09-20 22:40  LIFue  阅读(882)  评论(0)    收藏  举报