中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

溫州多語言網(wǎng)站建設(shè)網(wǎng)站發(fā)帖推廣平臺(tái)

溫州多語言網(wǎng)站建設(shè),網(wǎng)站發(fā)帖推廣平臺(tái),搭建平臺(tái)吸引人才,網(wǎng)站制作軟件都是什么軟件一、MySQL事務(wù)回顧 二、Spring事務(wù)管理 Spring框架的第一大核心:IOC控制反轉(zhuǎn) 在DeptServiceImpl下刪除部門方法下新加一個(gè)刪除員工信息的操作,注意:此時(shí)的id是部門id。 1、問題分析 2、Transactional-Spring事務(wù)管理 一般是在Service實(shí)現(xiàn)類的…

一、MySQL事務(wù)回顧

image-20230519095555441

二、Spring事務(wù)管理

Spring框架的第一大核心:IOC控制反轉(zhuǎn)

image-20230519093354025 image-20230519095638521

在DeptServiceImpl下刪除部門方法下新加一個(gè)刪除員工信息的操作,注意:此時(shí)的id是部門id。

image-20230519094945355 image-20230519095242773

1、問題分析

image-20230519095758677

2、@Transactional-Spring事務(wù)管理

image-20230519100017409

一般是在Service實(shí)現(xiàn)類的方法上加Transactional注解


執(zhí)行多次數(shù)據(jù)訪問的增刪改操作上需要用到事務(wù)

image-20230519101345091 image-20230519101246113 image-20230519101218784 image-20230519101511274 image-20230519102942049

三、Spring事務(wù)進(jìn)階

image-20230519103046587

1、rollbackFor

image-20230519103807812

Int i = 1/0 是屬于運(yùn)行時(shí)異常

image-20230519104208589 image-20230519110308309

這是service層,需要繼續(xù)往上拋異常


image-20230519110226786 image-20230519110436516

默認(rèn)情況下只有運(yùn)行時(shí)異常才會(huì)進(jìn)行事務(wù)回滾

2、propagation

image-20230519133216508 image-20230519133416156 image-20230519133437828 image-20230519133449823

因?yàn)槲覀冃枰馍⒉块T時(shí),無論成功還是失敗,都要記錄操作日志,所以需要用到@Transaction的REQUIRES_NEW的屬性,來新建一個(gè)事務(wù)

image-20230519133808397

pojo/DeptLog

package com.itheima.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDateTime;@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeptLog {private Integer id;private LocalDateTime createTime;private String description;
}

DeptServiceImple

package com.itheima.service.impl;import com.itheima.mapper.DeptLogMapper;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.time.LocalDateTime;
import java.util.List;
import java.util.logging.LogManager;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptMapper deptMapper;@Autowiredprivate EmpMapper empMapper;//  刪除部門id的同時(shí),需要?jiǎng)h除與部門id關(guān)聯(lián)的員工信息@Autowiredprivate DeptLogService deptLogService;//  添加日志對(duì)象注入//    @Autowired
//    private DeptLogMapper deptLogMapper;/*1、查詢操作* *///        實(shí)現(xiàn)方法public List<Dept> list(){List<Dept> deptList = deptMapper.select();return deptList;}/*2、刪除指定id* */@Transactional(rollbackFor = Exception.class)  //  交給了Spring進(jìn)行事務(wù)管理,將所有異常進(jìn)行事務(wù)處理@Overridepublic void deleteById(Integer id) throws Exception{try {deptMapper.deleteById(id);  //  刪除部門idint i = 1/0;
//            if (true){throw new Exception("出錯(cuò)了...");}empMapper.deleteById(id);    //  刪除員工信息} finally {//        記錄日志描述DeptLog deptLog = new DeptLog();deptLog.setCreateTime(LocalDateTime.now());deptLog.setDescription("執(zhí)行了解散部門的操作,此次解散的部門id是"+id);deptLogService.insert(deptLog);/***可以不用寫DeptLogService和DeptServiceImpl,直接寫一個(gè)DeptLogMapper然后交給IOC控制器,再在* 這個(gè)實(shí)現(xiàn)類中注入DeptLogMapper的對(duì)象直接調(diào)用insert方法,在方法前加入* @Transactional(propagation = Propagation.REQUIRES_NEW)*/
//            deptLogMapper.insert(deptLog);}}/*3、新增部門*/public void add(Dept dept){dept.setCreateTime(LocalDateTime.now());dept.setUpdateTime(LocalDateTime.now());deptMapper.insert(dept);}/*根據(jù)ID查詢*/public Dept selectById(Integer id){Dept dept = deptMapper.selectById(id);return dept;}@Override/*更新部門名稱*/public void update(Dept dept) {dept.setUpdateTime(LocalDateTime.now());dept.setCreateTime(LocalDateTime.now());deptMapper.update(dept);}
}

DpetLogService

package com.itheima.service;import com.itheima.pojo.DeptLog;public interface DeptLogService {void insert(DeptLog deptLog);
}

DeptLogServiceImpl

package com.itheima.service.impl;import com.itheima.mapper.DeptLogMapper;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;@Service
public class DeptLogServiceImpl implements DeptLogService {@Autowiredprivate DeptLogMapper deptLogMapper;//    開啟一個(gè)新事務(wù)@Transactional(propagation = Propagation.REQUIRES_NEW)@Overridepublic void insert(DeptLog deptLog) {deptLogMapper.insert(deptLog);}
}

DeptLogMapper

package com.itheima.mapper;import com.itheima.pojo.DeptLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;@Mapper
public interface DeptLogMapper {@Insert("insert into dept_log(create_time, description) VALUES (#{createTime},#{description})")
//    @Transactional(propagation = Propagation.REQUIRES_NEW)public void insert(DeptLog deptLog);
}

四、AOP基礎(chǔ)-Spring框架的第二大核心

AOP概述

image-20230519144922918 image-20230519144936888

AOP快速入門

image-20230519160123162
package com.itheima.aop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;@Slf4j
@Component  // 交給IOC容器
@Aspect     // 表示這個(gè)是AOP類
public class TimeAspect {
//  當(dāng)前共性功能應(yīng)該加在哪些方法上@Around("execution(* com.itheima.service.*.*(..))") //切入點(diǎn)表達(dá)式public void recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
//        1、獲取方法運(yùn)行開始時(shí)間long begin = System.currentTimeMillis();
//        2、運(yùn)行原始方法,o是原始方法的返回值Object o = joinPoint.proceed();
//        3、獲取方法結(jié)束運(yùn)行結(jié)束時(shí)間long end = System.currentTimeMillis();
//        計(jì)算方法耗時(shí)
//        joinPoint.getSignature():原始方法的簽名log.info(joinPoint.getSignature()+"方法耗時(shí):{}ms",end-begin);}
}
image-20230519160259797

image-20230519160606985

image-20230519161148430

小結(jié)

image-20230519161318405

五、AOP基礎(chǔ)-核心概念

AOP核心概念

image-20230519164857827

AOP執(zhí)行流程

image-20230519164715406

小結(jié)

連接點(diǎn)JoinPoint能夠被AOP所控制的方法
切入點(diǎn)PointCut實(shí)際被AOP控制的方法,通過切入點(diǎn)表達(dá)式
通知Advice將所有共性功能封裝起來的方法
切面Aspect描述通知與切入點(diǎn)之間的對(duì)應(yīng)關(guān)系
目標(biāo)對(duì)象Target通知所對(duì)應(yīng)的對(duì)象
image-20230519170101230

六、AOP進(jìn)階

image-20230519171259826

1、通知類型

image-20230519174120073 image-20230519174551031

切入點(diǎn)表達(dá)式抽取

image-20230519174957352 image-20230519175305641

小結(jié)

image-20230519175400903

2、通知順序

image-20230523105728939 image-20230523110339906

3、切入點(diǎn)表達(dá)式

execution

image-20230523110701548 image-20230523113303585 image-20230523151557536 image-20230523151743153

MyAspect6.java

package com.itheima.aop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Component
@Slf4j
@Aspect
public class MyAspect6 {
//    @Pointcut("execution(public void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
//    @Pointcut("execution(void com.itheima.service.DeptService.delete(java.lang.Integer))")//基于接口描述
//    @Pointcut("execution(void delete(java.lang.Integer))")包名.類名不建議省略,匹配的范圍過大,影響匹配的效率
//    @Pointcut("execution(* com.itheima.service.DeptService.*(*))")//匹配返回值為任意,DeptService接口下所有方法任意類型的一個(gè)參數(shù)
//    @Pointcut("execution(* com..DeptService.get(*))")//匹配返回值為任意,com包下任意子包中DeptService接口/類下get方法,為任意類型的一個(gè)參數(shù)@Pointcut("execution(* com.itheima.service.DeptService.delete(java.lang.Integer)) ||"+"execution(* com.itheima.service.DeptService.list())")//匹配前面一個(gè)或者后面任意一個(gè)public void pt(){}@Before("pt()")public void before(){System.out.println("before ...6");}
}

@annotation

image-20230523153020619

MyLog

package com.itheima.aop;import lombok.extern.slf4j.Slf4j;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)//該注解表示注解的生命周期
@Target(ElementType.METHOD)//表示該注解作用在哪一部分
public @interface MyLog {
}

MyAspect7

package com.itheima.aop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Slf4j
@Component
@Aspect
public class MyAspect7 {@Pointcut("@annotation(com.itheima.aop.MyLog)")public void pc(){}@Before("pc()")public void func(){log.info("MyAspect7...");}
}
image-20230523160655953

只需要在想要匹配的方法上加@MyLog注解就可以調(diào)用通知方法

小結(jié)

image-20230523161211924

4、連接點(diǎn)

image-20230523162227036

MyAspect8

package com.itheima.aop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;import java.util.Arrays;@Component
@Slf4j
@Aspect
public class MyAspect8 {@Pointcut("execution(* com.itheima.service.DeptService.*(..))")public void pt(){}@Before("pt()")public void before(JoinPoint joinPoint){log.info("before....");}@Around("pt()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {log.info("MyAspect8 around before...");//        1、獲取目標(biāo)類名String className = joinPoint.getTarget().getClass().getName();log.info("目標(biāo)類名:{}",className);//        2、獲取目標(biāo)方法簽名Signature signature = joinPoint.getSignature();log.info("目標(biāo)方法簽名:{}",signature);//        3、獲取目標(biāo)方法名String signatureName = joinPoint.getSignature().getName();log.info("目標(biāo)方法名:{}",signatureName);//        4、獲取目標(biāo)方法運(yùn)行參數(shù)Object[] args = joinPoint.getArgs();String arg = Arrays.toString(args);log.info("目標(biāo)方法參數(shù):{}",arg);//        5、執(zhí)行原始方法,獲取返回值Object result = joinPoint.proceed();log.info("目標(biāo)方法的返回值:{}",result);log.info("MyAspect8 after ...");return result;}@After("pt()")public void after(){log.info("after...");}
}

測(cè)試類

package com.itheima;import com.itheima.service.DeptService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringbootAopQuickstart1ApplicationTests {@AutowiredDeptService deptService;@Testpublic void test1(){deptService.delete(10);}@Testpublic void test2(){deptService.list();}
}

執(zhí)行delete之后

image-20230523165601716

只有環(huán)繞通知才可以執(zhí)行原始方法

前置通知在原始方法執(zhí)行前運(yùn)行,后置通知在原始方法執(zhí)行后運(yùn)行

七、AOP案例

image-20230523171728414

? image-20230523172429155

1、準(zhǔn)備工作

<!--AOP依賴-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>2.7.1</version>
</dependency>
-- AOP案例
-- 操作日志表
create table operate_log(id int unsigned primary key auto_increment comment 'ID',operate_user int unsigned comment '操作人ID',operate_time datetime comment '操作時(shí)間',class_name varchar(100) comment '操作的類名',method_name varchar(100) comment '操作的方法名',method_params varchar(1000) comment '方法參數(shù)',return_value varchar(2000) comment '返回值',cost_time bigint comment '方法執(zhí)行耗時(shí), 單位:ms'
) comment '操作日志表';

sql表格對(duì)應(yīng)的實(shí)體類

package com.itheima.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDateTime;@Data
@NoArgsConstructor
@AllArgsConstructor
public class OperateLog {private Integer id; //IDprivate Integer operateUser; //操作人IDprivate LocalDateTime operateTime; //操作時(shí)間private String className; //操作類名private String methodName; //操作方法名private String methodParams; //操作方法參數(shù)private String returnValue; //操作方法返回值private Long costTime; //操作耗時(shí)
}

2、編碼

image-20230524154418945

LogAspect.java

package com.itheima.aop;import com.alibaba.fastjson.JSONObject;
import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.OperateLog;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;@Component
@Slf4j
@Aspect //切面類
public class LogAspect {@Autowired//用于獲取jwt令牌private HttpServletRequest httpServletRequest;@Autowiredprivate OperateLogMapper operateLogMapper;@Around("@annotation(com.itheima.anno.Log)")//切點(diǎn)表達(dá)式public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {
//        1、獲取操作人ID,當(dāng)前登錄員工id,這一步需要用到j(luò)wt令牌,在Header中String jwt = httpServletRequest.getHeader("token");
//        解析jwt令牌Claims claims = JwtUtils.parseJwt(jwt);Integer operateUser  = (Integer) claims.get("id");//        2、操作時(shí)間LocalDateTime operateTime = LocalDateTime.now();//        3、操作類名String className = joinPoint.getTarget().getClass().getName();//        4、操作方法名String methodName = joinPoint.getSignature().getName();//        5、操作方法參數(shù)Object[] args = joinPoint.getArgs();String methodParams = Arrays.toString(args);//         獲取方法開始時(shí)間long begin = System.currentTimeMillis();
//        6、操作方法返回值,轉(zhuǎn)換為json格式的字符串保存起來Object result = joinPoint.proceed();String returnValue = JSONObject.toJSONString(result);
//        獲取結(jié)束時(shí)間long end = System.currentTimeMillis();//        7、操作耗時(shí)long costTime = end - begin;//        將日志內(nèi)容插入到表中OperateLog operateLog = new OperateLog(null,operateUser,operateTime,className,methodName,methodParams,returnValue,costTime);operateLogMapper.insert(operateLog);log.info("AOP操作日志:{}",operateLog);return result;//返回的是執(zhí)行方法的返回值}
}

OperateLogMapper

package com.itheima.mapper;import com.itheima.pojo.OperateLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface OperateLogMapper {@Insert("insert into operate_log (operate_user, operate_time, class_name, method_name, method_params, return_value, cost_time) " +"values (#{operateUser}, #{operateTime}, #{className}, #{methodName}, #{methodParams}, #{returnValue}, #{costTime});")public void insert(OperateLog operateLog);}

注意

image-20230524155552227
http://www.risenshineclean.com/news/1806.html

相關(guān)文章:

  • 網(wǎng)頁瀏覽設(shè)置在哪里打開網(wǎng)頁優(yōu)化
  • 做網(wǎng)站比較好的環(huán)球網(wǎng)疫情最新
  • 2015做哪個(gè)網(wǎng)站能致富沈陽專業(yè)seo關(guān)鍵詞優(yōu)化
  • 建設(shè)公司企業(yè)網(wǎng)站個(gè)人網(wǎng)頁怎么制作
  • WordPress網(wǎng)頁加載時(shí)間seo網(wǎng)站優(yōu)化快速排名軟件
  • 備案名 網(wǎng)站名互聯(lián)網(wǎng)營(yíng)銷師教材
  • 有幾個(gè)網(wǎng)站能在百度做推廣怎么弄推廣廣告
  • 手機(jī)門戶網(wǎng)站百度推廣賬戶登錄首頁
  • 城鄉(xiāng)與建設(shè)部網(wǎng)站首頁seo工具不包括
  • 購(gòu)物網(wǎng)站英語濟(jì)南做seo的公司排名
  • wordpress鏈接亞馬遜在線優(yōu)化工具
  • 云南省網(wǎng)站備案要求網(wǎng)絡(luò)推廣企劃
  • 長(zhǎng)沙市住建委和城鄉(xiāng)建設(shè)網(wǎng)站長(zhǎng)沙網(wǎng)絡(luò)公司排名
  • WordPress圖片置頂寧波seo在線優(yōu)化
  • 友點(diǎn)企業(yè)網(wǎng)站管理系統(tǒng)模板下載百度如何優(yōu)化排名靠前
  • 搜索的網(wǎng)站后大拇指分享數(shù)量不見了收錄批量查詢工具
  • 2017年網(wǎng)站設(shè)計(jì)磁力貓引擎
  • 上海專業(yè)做網(wǎng)站公司電話注冊(cè)域名費(fèi)用一般多少錢
  • 手機(jī)怎么制作釣魚網(wǎng)站網(wǎng)絡(luò)營(yíng)銷組織的概念
  • 大公司做網(wǎng)站seo分析
  • 用什么做視頻網(wǎng)站比較好個(gè)人在百度上發(fā)廣告怎么發(fā)
  • 大型門戶網(wǎng)站建設(shè)包括哪些方面seoapp推廣
  • 上海網(wǎng)站建設(shè)推薦今日新聞?lì)^條最新消息
  • 注冊(cè)了域名怎么做網(wǎng)站成全在線觀看免費(fèi)高清動(dòng)漫
  • 做炫舞情侶頭像動(dòng)態(tài)圖網(wǎng)站推廣是什么意思
  • 國(guó)外做節(jié)目包裝的網(wǎng)站網(wǎng)上做推廣怎么收費(fèi)
  • 2015年做啥網(wǎng)站能致富廣告推廣一個(gè)月多少錢
  • 青海高端網(wǎng)站建設(shè)價(jià)格長(zhǎng)沙網(wǎng)紅打卡景點(diǎn)排行榜
  • 網(wǎng)站備案查詢 whois百度網(wǎng)站提交
  • 網(wǎng)站開發(fā)預(yù)算報(bào)表企點(diǎn)官網(wǎng)