国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring-Mybatis整合配置文件與AOP織入方式對比

這篇具有很好參考價值的文章主要介紹了Spring-Mybatis整合配置文件與AOP織入方式對比。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

對比AOP織入 方式一

<!-- 方式二:使用切面定義  自定義類    -->
    <bean id="diy" class="com.kuang.diy.Diy"/>
    <aop:config>
<!--     自定義切面,ref要引入的類   -->
        <aop:aspect ref="diy">
<!--         切入點   -->
            <aop:pointcut id="point" expression="execution(* com.kuang.services.ServiceImpl.*(..))"/>
<!--           通知 -->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
        
    </aop:config>

?對比AOP織入 方式二

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
         https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注冊bean-->
    <bean id="service" class="com.kuang.services.ServiceImpl"/>
    <bean id="logBefore" class="com.kuang.log.LogBefore"/>
    <bean id="logAfter" class="com.kuang.log.LogAfter"/>
 
<!-- 方式一:使用原生Spring API接口   -->
<!--配置AOP  :   需要導入aop約束-->
   <aop:config>
<!--   切入點  :expression表達式  = execution(需要執(zhí)行的位置! 修飾符 返回值 類名.方法名.參數(shù)  (* * * * *))  -->
       <aop:pointcut id="pointcut" expression="execution(* com.kuang.services.ServiceImpl.*(..))"/>
<!--   執(zhí)行環(huán)繞增加    -->
       <aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/>
       <aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"/>
 
   </aop:config>
 
</beans>

?注解實現(xiàn)AOP

package com.qf.common;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//增強類  事務管理類
//切面=切點加通知
@Aspect
@Component
public class TransManger {
    //定義切點,無參無返回值的方法  pc相當于切點的ID
    @Pointcut("execution(* com.qf.service..*.*(..))")
    public void pc(){}

    @Before("pc()")
    public  void startTran(){
        System.out.println("開啟事務~~~~~~~~~~~~");
    }

    @AfterReturning("pc()")
    public void commitTran(){
        System.out.println("提交事務.............");
    }
   @AfterThrowing("pc()")
     public void rollBackTran(){
         System.out.println("回滾事務~~~~~~~~~~~~~~");
     }
@After("pc()")
     public void afterMethod(){
         System.out.println("測試AOP");
     }

//     加給AOP:around環(huán)繞通知
    @Around("pc()")
    public Object aroundMethod(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("環(huán)繞執(zhí)行前");
        long start = System.currentTimeMillis();
        Object proceed = pj.proceed();
        long end = System.currentTimeMillis();
        System.out.println(".......delete_after");
        System.out.println(end-start);
        System.out.println("環(huán)繞執(zhí)行后");
        return proceed;
    }

}

spring-dao.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: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
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.qf"/>

    <!-- 整合數(shù)據(jù)源屬性配置文件,參數(shù)化占位符 ,讀取參數(shù)${}  -->
    <context:property-placeholder location="classpath:druid.properties"/>

    <!--配置數(shù)據(jù)源,DataSource就是一個對象,將對象交由spring容器創(chuàng)建并管理-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${druid.driver}"/>
        <property name="url" value="${druid.url}"/>
        <property name="username" value="${druid.username}"/>
        <property name="password" value="${druid.password}"/>
    </bean>
    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--   配置數(shù)據(jù)源    -->
        <property name="dataSource" ref="dataSource"/>
        <!--   配置需要配置別名的實體類包  -->
        <property name="typeAliasesPackage" value="com.qf"/>
        <!--   配置mybatis映射文件加載路徑,如果映射文件與接口文件不在同一路徑下,需要配置 因為是在resource下所以得必須用/   -->
        <property name="mapperLocations" value="classpath:com/qf/mapper/*.xml"/>
        <!--    加載mabatis 核心配置文件,需要時加載,如果配置為空,可以省略不寫    -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>

    </bean>

    <!--  配置SqlSession和接口得代理對象 -->
    <!--  bean默認是單例模式,它里面有一個私有的對象,只設置了一次 sqlSessionFactory 創(chuàng)建出一個sqlSessionTemplate 對象,這個對象相當于sqlSession 接口的代理對象,-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--     指定sqlSessionFactory注入 String字符串 名字,不是bean getbean是在 MapperScannerConfigurer 類內(nèi)部被調(diào)用,拿這個變量去調(diào)用得
         肯定設置了openSession(true) 默認開啟自動提交
          用得同一個session ,將不能使用二級緩存了,一般同個session一級緩存就夠用了,一級緩存,增刪改會清除緩存,但是只是查詢,會找到一級緩存里之前查過的數(shù)據(jù)直接查出來,不調(diào)用sql,
          只是用同一個session 創(chuàng)建getMapper不同得接口代理類,去實現(xiàn)不同的接口,代理這個代理類。
          創(chuàng)建了這個代理對象注入進IOC容器里面。普通得getMapper不能創(chuàng)建 一個類, 他只是創(chuàng)建一個對象,
          然后通過動態(tài)代理這個對象去實現(xiàn)共同的接口的一個類,把這個類才能加入IOC容器里,返回給我們使用這個代理對象。
          -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

        <!--     設置要創(chuàng)建mapper對象得接口所在得包名  因為代理的是接口,所以指定包名是接口的包名,
        通過接口的字節(jié)碼對象,與代理的目標對象的字節(jié)碼對象,加上實現(xiàn)InvocationHandler的類對象,完成代理對象的創(chuàng)建,然后把bean創(chuàng)建給容器。 -->
        <property name="basePackage" value="com.qf.mapper"/>
    </bean>

    <!--配置事務管理器-->
    <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--  注入數(shù)據(jù)源對象,要求與sqlSessionFactory創(chuàng)建時所注入數(shù)據(jù)源對象是同一個  -->
        <property name="dataSource" ref="dataSource"/>

    </bean>

<!--    <tx:annotation-driven transaction-manager="transactionManger"/>-->

    <tx:advice id="txAdvice" transaction-manager="transactionManger">
        <tx:attributes>
            <tx:method name="insert*"/>
            <tx:method name="delete*"/>
        </tx:attributes>
    </tx:advice>
<!--配置事務-->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.qf.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>



</beans>

導入的依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qf</groupId>
    <artifactId>Spring-Mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-version>5.3.20</spring-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!--spring相關依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!-- spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!--spring-aspects-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
<!--     druid   -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

文章來源地址http://www.zghlxwxcb.cn/news/detail-608572.html

到了這里,關于Spring-Mybatis整合配置文件與AOP織入方式對比的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • MyBatis與Spring整合以及AOP和PageHelper分頁插件整合

    MyBatis與Spring整合以及AOP和PageHelper分頁插件整合

    目錄 前言 一、MyBatis與Spring整合的好處以及兩者之間的關系 1.好處 2.關系 ?二、MyBatis和Spring集成 1.導入pom.xml 2.編寫配置文件? 3.利用mybatis逆向工程生成模型層代碼 三、常用注解 ?四、AOP整合pageHelper分頁插件 創(chuàng)建一個切面 測試 MyBatis是一個開源的持久層框架,而Spring是一個

    2024年02月11日
    瀏覽(22)
  • Spring、Spring-MVC、Mybatis、Mybatis-generator整合核心配置文件記錄

    Spring、Spring-MVC、Mybatis、Mybatis-generator整合核心配置xml文件記錄 spring-mybatis.xml

    2024年01月22日
    瀏覽(50)
  • Spring與Mybatis集成且Aop整合(放飛雙手,迅速完成CRUD及分頁)

    Spring與Mybatis集成且Aop整合(放飛雙手,迅速完成CRUD及分頁)

    目錄 ?一、概述 二、集成 ( 1 ) 為什么 ( 2 ) 優(yōu)點 ( 3 ) 實例 三、整合 3.1 講述 3.2 整合進行分頁 帶我們帶來的收獲 集成是指將不同的組件、系統(tǒng)或框架整合在一起,使它們能夠協(xié)同工作,共同完成某個功能或提供某種服務。在軟件開發(fā)中,集成通常指的是將多個獨立的模塊或

    2024年02月11日
    瀏覽(23)
  • MyBatis與Spring集成&常用注解以及AOP和PageHelper分頁插件整合

    MyBatis與Spring集成&常用注解以及AOP和PageHelper分頁插件整合

    目錄 前言 一、MyBatis與Spring整合的好處以及兩者之間的關系 1.好處 2.關系 ?二、MyBatis和Spring集成 1.導入pom.xml 2.編寫配置文件? 3.利用mybatis逆向工程生成模型層代碼 三、常用注解 ?四、AOP整合pageHelper分頁插件 創(chuàng)建一個切面 測試 MyBatis是一個開源的持久層框架,而Spring是一個

    2024年02月07日
    瀏覽(27)
  • Spring Boot整合Mybatis配置多數(shù)據(jù)源

    在之前的事件管理系統(tǒng)博客中有提到動態(tài)的多數(shù)據(jù)源配置 工作中難免需要做幾個工具方便自己偷懶,加上之前的擋板,數(shù)據(jù)源肯定沒法單一配置,所以需要多數(shù)據(jù)源配置。這里介紹兩種配置:動態(tài)數(shù)據(jù)源和固定數(shù)據(jù)源模式。這兩種我在目前的工作的工具開發(fā)中都有用到。 M

    2024年01月23日
    瀏覽(30)
  • 【Spring教程31】SSM框架整合實戰(zhàn):從零開始學習SSM整合配置,如何編寫Mybatis SpringMVC JDBC Spring配置類

    【Spring教程31】SSM框架整合實戰(zhàn):從零開始學習SSM整合配置,如何編寫Mybatis SpringMVC JDBC Spring配置類

    歡迎大家回到《Java教程之Spring30天快速入門》,本教程所有示例均基于Maven實現(xiàn),如果您對Maven還很陌生,請移步本人的博文《如何在windows11下安裝Maven并配置以及 IDEA配置Maven環(huán)境》,本文的上一篇為《Rest風格簡介與RESTful入門》 前面我們已經(jīng)把Mybatis、Spring和SpringMVC三個框架

    2024年02月04日
    瀏覽(24)
  • Spring5學習隨筆-整合MyBatis(持久層)、連接池、Mapper文件

    Spring5學習隨筆-整合MyBatis(持久層)、連接池、Mapper文件

    學習視頻:【孫哥說Spring5:從設計模式到基本應用到應用級底層分析,一次深入淺出的Spring全探索。學不會Spring?只因你未遇見孫哥】 JavaEE開發(fā)需要持久層進行數(shù)據(jù)庫的訪問操作。 JDBC MyBatis、Hibernate進行持久開發(fā)過程存在大量的代碼冗余 Spring基于模板設計模式對與上述的持

    2024年02月05日
    瀏覽(50)
  • 04 SpringBoot整合Druid/MyBatis/事務/AOP+打包項目

    04 SpringBoot整合Druid/MyBatis/事務/AOP+打包項目

    項目結構: 引入依賴: 啟動類: 實體類: Controller類: Druid配置文件application.yaml : 啟動項目后進入8080/user/getUser即可獲得返回的json. 先看看總體的步驟: 導入依賴:在pom.xml中添加MyBatis和數(shù)據(jù)庫驅動的相關依賴。 配置數(shù)據(jù)源: application.yml 中配置 數(shù)據(jù)庫連接信息,包括數(shù)據(jù)

    2024年01月21日
    瀏覽(40)
  • DAY02_Spring—第三方資源配置管理&Spring容器&Spring注解開發(fā)&Spring整合Mybatis和Junit

    DAY02_Spring—第三方資源配置管理&Spring容器&Spring注解開發(fā)&Spring整合Mybatis和Junit

    說明:以管理DataSource連接池對象為例講解第三方資源配置管理 問題導入 配置數(shù)據(jù)庫連接參數(shù)時,注入驅動類名是用driverClassName還是driver? 1.1 管理Druid連接池 數(shù)據(jù)庫準備 【第一步】添加Druid連接池依賴 注意:除了添加以上兩個依賴之外,別忘了添加spring-context依賴。 【第二

    2024年02月13日
    瀏覽(124)
  • DAY02_Spring第三方資源配置管理&Spring容器&Spring注解開發(fā)&Spring整合Mybatis和Junit

    DAY02_Spring第三方資源配置管理&Spring容器&Spring注解開發(fā)&Spring整合Mybatis和Junit

    說明:以管理DataSource連接池對象為例講解第三方資源配置管理 問題導入 配置數(shù)據(jù)庫連接參數(shù)時,注入驅動類名是用driverClassName還是driver? 1.1 管理Druid連接池 數(shù)據(jù)庫準備 【第一步】添加Druid連接池依賴 注意:除了添加以上兩個依賴之外,別忘了添加spring-context依賴。 【第二

    2024年02月14日
    瀏覽(105)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包