spring简单配置jdbc连接与事务控制

1、首先,使用spring配jdbc第一点当然就是配置jar包啦。下面是配的是最基本的jar包。最后一个是日志包。如果想了解spring各个jar包的作用。
点击这个作者:http://www.imooc.com/article/9909

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.12.1</version>
        </dependency>
    </dependencies>

2、jar包有了,就得建立连接了。spring里面一切的对象都是工厂帮我们new的。
我们建立jdbc连接需要的datasource对象也是工厂给我们提供。我们需要在src/main/resources文件夹下建个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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/user?serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="1234"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="userDao" class="com.dao.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <bean id="userService" class="com.serviceImpl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <!--以下对事务处理的配置是通用的-->
    <!--事务处理的核心类,其依赖与dataSource类对象-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--方法名必须以下面字符开头; support表示不支持事务,但如果处于事务内,也会按事务处理。,required支持事务-->
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="count*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="exist*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="edit*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <!--* com.serviceImpl.UserServiceImpl.*.*(..)其中*表示任意返回值,该类下的任意参数的任意方法都配置了事务-->
        <aop:pointcut id="transactionPointcut" expression="execution(* com.serviceImpl.UserServiceImpl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config>
</beans>

第一个bean配置connection,配置DruidDataSource,得到datasource对象,并为该对象设置url,username,password,driverClassName等属性。
第二个bean配置jdbc模板,因为jdbcTemplate类需要connection,所以为他配置了datasource对象。至此,数据库基本来接完成;

第三个bean配置事务管理。事务管理依赖与同一个connection连接,所以同样配置了datasource对象;
下面配的是通知:其中注意它定义了方法名的书写方式,必须严格依照。

<tx:advice></tx:advice>

关键在最后一个config,配置的是通知和事务的连接。
advisor是spring定义的切面。包含通知和切入点。
advice-ref="txAdvice"配置的是通知。
pointcut-ref="transactionPointcut"配置的是切入点,就是在哪里加事务。
把通知和切入点结合就是切面

ok,这样简单的事务配置就完成了。

发布了14 篇原创文章 · 获赞 0 · 访问量 328

猜你喜欢

转载自blog.csdn.net/qq_38205881/article/details/103624559
今日推荐