SSM 框架——整合 Spring

155 阅读1分钟

「时光不负,创作不停,本文正在参加2021年终总结征文大赛

整合 Spring

编写 jdbc.properties 配置文件

url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf8&useSSL=false
driver=com.mysql.jdbc.Driver
user=root
password=123456

注:MySQL 8.0版本之后 url 需要配置时区 serverTimezone=GMT%2B8

spring-dao.xml 对 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 
                           https://www.springframework.org/schema/context/spring-context.xsd">

    <!--关联数据库配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--Druid 数据库连接池配置-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init">
        <!-- 基本属性driverClassName、 url、username、password -->
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
        <!-- 配置初始化大小、最小、最大、获取连接等待超时的时间、连接在池中最小生存的时间 -->
        <property name="initialSize" value="2"/>
        <property name="minIdle" value="5"/>
        <property name="maxActive" value="50"/>
        <property name="maxWait" value="5000"/>
        <property name="minEvictableIdleTimeMillis" value="30000"/>
    </bean>

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

    <!--配置 dao 接口扫描包,动态实现 Dao 接口注入 Spring 容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入 sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--扫描 dao 包-->
        <property name="basePackage" value="dao"/>
    </bean>

</beans>

spring-service.xml 对 Service 进行整合

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

    <!--扫描 service 下的包-->
    <context:component-scan base-package="service"/>

    <!--将所有业务类注入到 Spring ,可以通过配置或注解实现-->
    <bean id="bookServiceImpl" class="service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!--声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事务支持-->

    <!--给所有方法配置事务-->
    <!--配置事务的传播特性:new propagation-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--aop配置事务切入-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* dao.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

</beans>