春は、簡単な構成のトランザクション制御に接続されているJDBC

図1は、まず、それは勿論であるJDBCポイントと第一バネは、JARパッケージを配置しました。ここでは瓶の中で最も基本的な機能があります。最後の一つは、ログパッケージです。あなたは、各ばねの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パッケージには、接続を確立する必要があります。すべてのオブジェクトは、私たちは新しいを植える支援するために、スプリングの内側にあります。
私たちは、接続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クラスが接続を必要とするため、2番目のBeanは、コンフィギュレーション、JDBCテンプレート、ので、彼のためにデータソースオブジェクトを設定します。これまでのところ、基本的なデータベースが完成選択する;
...
第三Bean構成のトランザクション管理。トランザクション管理は、同じ接続に関連して、そのデータソースオブジェクトの同じ構成を依存しています。
次の注意である:予告それは名前を書く方法を定義するには、に従って厳格でなければならないところ。

<tx:advice></tx:advice>

最後の設定、構成のキーは、接続された通知との取引です。
顧問は、セクション春に定義されています。これは、通知およびエントリポイントが含まれています。
アドバイス-REF = "txAdvice"設定が通知されます。
ポイントカット-REF =「transactionPointcut」構成は、トランザクションが追加される開始点です。
通知および出発点は、カットの組み合わせであります

[OK]を、ので、簡単なトランザクションの設定は完了です。

公開された14元の記事 ウォンの賞賛0 ビュー328

おすすめ

転載: blog.csdn.net/qq_38205881/article/details/103624559