스프링은 간단한 구성 트랜잭션 제어 JDBC 연결된다

도 1은, 우선은 물론이다 JDBC 포인트를 갖는 제 1 스프링은 단지 패키지를 배치. 여기에 항아리의 가장 기본적인 기능입니다. 마지막 하나는 로그 패키지입니다. 각 봄 항아리 패키지의 역할을 이해하십시오.
이 저자를 클릭 : 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, 항아리 패키지가, 당신은 연결을 설정해야합니다. 모든 개체는 우리가 새로운 식물 도움이되는 봄 안에 있습니다.
우리는 연결 JDBC 데이터 소스 객체가 공장으로 우리를 제공하기 위해도 설정해야합니다. 우리는 SRC에서 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>

연결 대상 데이터 소스를 제공 할 수있는 콩, DruidDataSource 구성의 구성 및 설정 URL, 사용자 이름, 암호, driverClassName은 객체 속성.
JdbcTemplate을 클래스 연결이 필요하기 때문에 두 번째 빈 구성 JDBC 템플릿, 그래서 그를 위해 데이터 소스 오브젝트를 구성 할 수 있습니다. 지금까지 기본 데이터베이스가 완료 데리러;
...
세 번째 빈 구성 트랜잭션 관리. 데이터 소스의 동일한 구성이 객체 있도록 트랜잭션 관리, 동일한 연결과 관련 의존,
다음 통지가로 : 통지가 이름을 작성하는 방법을 정의하는 경우,에 따라 엄격하게해야합니다.

<tx:advice></tx:advice>

마지막 설정의 핵심은, 구성은 연결된 통보 및 거래입니다.
권고는 스프링 섹션에 정의되어있다. 그것은 알림 및 진입 점을 포함한다.
조언-REF = "txAdvice"구성이 통지됩니다.
포인트 컷-REF = "transactionPointcut"구성 트랜잭션이 추가 출발점이다.
알림 및 시작 지점이 컷의 조합입니다

좋아, 그렇게 단순 트랜잭션 구성이 완료됩니다.

게시 14 개 원래 기사 · 원의 칭찬 0 · 조회수 328

추천

출처blog.csdn.net/qq_38205881/article/details/103624559