SpringMVC(6)-ssm整合实现增删改查-spring层


1.引言:spring层主要做两件事情

  1.1创建一个spring-dao.xml,关联数据库配置文件(context:property-placeholder location="xxx"),配置数据源,SqlSessionFactory(注入数据源,绑定mybatis核心配置文件mybatis-config.xml),扫描dao接口(注入sqlSessionFactory,给出需要扫描的dao包)

  1.2创建一个spring-service.xml,配置扫描service层下的包,将所有的业务类注入到spring,声明式事务

2.步骤:

  2.1在resources文件夹下新建一个spring.xml文件,命名为spring-dao.xml,右键resources-》new-》file-》输入文件名spring-dao.xml,点击ok。(这里需要注意的是关联的数据库配置文件以及sqlsessionFactory绑定mybatis配置文件要对,通过MapperScannerConfigurer扫描的dao接口包路径要对)

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

    
    <context:property-placeholder location="classpath:database.properties"/>

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

    
    <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.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="basePackage" value="com.xiaoma1.dao"/>
    bean>
beans>

  2.2同样的方法创建一个名为spring-service.xml的配置文件

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

    
    <context:component-scan base-package="com.xiaoma1.service"/>

    
    <bean id="bookServiceImpl" class="com.xiaoma1.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    bean>

    
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource"/>
    bean>
beans>