Spring集成JDBC配置文件

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

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=GMT" />
        <property name="username" value="root" />
        <property name="password" value="zhangpn" />
        <property name="initialSize" value="1" />
        <property name="maxTotal" value="20" />
        <property name="maxIdle" value="2" />
        <property name="minIdle" value="1" />
    </bean>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

dbcp创建一个数据源

创建一个DataSourceTransactionManager数据源事务管理员

声明使用注解方式使用数据源事务管理员

在Service中加上@Transaction注解,OK。

猜你喜欢

转载自www.cnblogs.com/batj/p/9152475.html