tlias管理系统-springAOP的快速入门
This commit is contained in:
93
springboot-aop-quickstart/pom.xml
Normal file
93
springboot-aop-quickstart/pom.xml
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.inmind</groupId>
|
||||
<artifactId>springboot-aop-quickstart</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>springboot-aop-quickstart</name>
|
||||
<description>springboot-aop-quickstart</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-boot.version>2.7.6</spring-boot.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!--AOP起步依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>com.inmind.SpringbootAopQuickstartApplication</mainClass>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>repackage</id>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.inmind;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringbootAopQuickstartApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringbootAopQuickstartApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.inmind.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;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
@Slf4j
|
||||
public class TimeAspect {
|
||||
|
||||
@Around("execution(* com.inmind.service.*.*(..))")//表示AOP作用于com.inmind.service下的所有的类中所有的方法,参数是0或多个
|
||||
public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
//1.记录开始时间
|
||||
long begin = System.currentTimeMillis();
|
||||
//2.调用原始方法
|
||||
Object result = joinPoint.proceed();
|
||||
//3.记录结束时间,计算出方法耗时
|
||||
long end = System.currentTimeMillis();
|
||||
log.info(joinPoint.getSignature()+"业务方法,执行耗时为:{},ms",end-begin);
|
||||
|
||||
//如果是环绕通知Around,一定要返回结果,否则原始方法就获取不到返回值数据
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import com.inmind.pojo.Dept;
|
||||
import com.inmind.pojo.Result;
|
||||
import com.inmind.service.DeptService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/depts")
|
||||
public class DeptController {
|
||||
|
||||
@Autowired
|
||||
private DeptService deptService;
|
||||
|
||||
//查询全部部门
|
||||
@GetMapping
|
||||
public Result list(){
|
||||
List<Dept> deptList = deptService.list();
|
||||
return Result.success(deptList);
|
||||
}
|
||||
|
||||
//删除部门
|
||||
@DeleteMapping("/{id}")
|
||||
public Result delete(@PathVariable Integer id) {
|
||||
deptService.delete(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
//添加部门
|
||||
@PostMapping
|
||||
public Result save(@RequestBody Dept dept){
|
||||
deptService.save(dept);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
//根据ID查询
|
||||
@GetMapping("/{id}")
|
||||
public Result getById(@PathVariable Integer id){
|
||||
Dept dept = deptService.getById(id);
|
||||
return Result.success(dept);
|
||||
}
|
||||
|
||||
//更新部门
|
||||
@PutMapping
|
||||
public Result update(@RequestBody Dept dept){
|
||||
deptService.update(dept);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.inmind.mapper;
|
||||
|
||||
import com.inmind.pojo.Dept;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DeptMapper {
|
||||
//查询全部部门数据
|
||||
@Select("select * from dept")
|
||||
List<Dept> list();
|
||||
|
||||
//删除部门
|
||||
@Delete("delete from dept where id = #{id}")
|
||||
void delete(Integer id);
|
||||
|
||||
//新增部门
|
||||
@Insert("insert into dept(name, create_time, update_time) values (#{name},#{createTime},#{updateTime})")
|
||||
void save(Dept dept);
|
||||
|
||||
//根据ID查询
|
||||
@Select("select * from dept where id = #{id}")
|
||||
Dept getById(Integer id);
|
||||
|
||||
//更新部门
|
||||
@Update("update dept set name = #{name}, update_time = #{updateTime} where id = #{id}")
|
||||
void update(Dept dept);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Dept {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Result {
|
||||
private Integer code;//响应码,1 代表成功; 0 代表失败
|
||||
private String msg; //响应码 描述字符串
|
||||
private Object data; //返回的数据
|
||||
|
||||
//增删改 成功响应
|
||||
public static Result success(){
|
||||
return new Result(1,"success",null);
|
||||
}
|
||||
//查询 成功响应
|
||||
public static Result success(Object data){
|
||||
return new Result(1,"success",data);
|
||||
}
|
||||
//失败响应
|
||||
public static Result error(String msg){
|
||||
return new Result(0,msg,null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.inmind.service;
|
||||
|
||||
import com.inmind.pojo.Dept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DeptService {
|
||||
/**
|
||||
* 查询所有的部门数据
|
||||
* @return
|
||||
*/
|
||||
List<Dept> list();
|
||||
|
||||
/**
|
||||
* 删除部门
|
||||
* @param id
|
||||
*/
|
||||
void delete(Integer id);
|
||||
|
||||
/**
|
||||
* 新增部门
|
||||
* @param dept
|
||||
*/
|
||||
void save(Dept dept);
|
||||
|
||||
/**
|
||||
* 根据ID查询
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Dept getById(Integer id);
|
||||
|
||||
/**
|
||||
* 更新部门
|
||||
* @param dept
|
||||
*/
|
||||
void update(Dept dept);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.inmind.service.impl;
|
||||
|
||||
import com.inmind.aop.MyLog;
|
||||
import com.inmind.mapper.DeptMapper;
|
||||
import com.inmind.pojo.Dept;
|
||||
import com.inmind.service.DeptService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DeptServiceImpl implements DeptService {
|
||||
@Autowired
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
@Override
|
||||
@MyLog
|
||||
public List<Dept> list() {
|
||||
List<Dept> deptList = deptMapper.list();
|
||||
return deptList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@MyLog
|
||||
public void delete(Integer id) {
|
||||
//1. 删除部门
|
||||
deptMapper.delete(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Dept dept) {
|
||||
dept.setCreateTime(LocalDateTime.now());
|
||||
dept.setUpdateTime(LocalDateTime.now());
|
||||
deptMapper.save(dept);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dept getById(Integer id) {
|
||||
// int i = 1/0;
|
||||
return deptMapper.getById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Dept dept) {
|
||||
dept.setUpdateTime(LocalDateTime.now());
|
||||
deptMapper.update(dept);
|
||||
}
|
||||
}
|
||||
10
springboot-aop-quickstart/src/main/resources/application.yml
Normal file
10
springboot-aop-quickstart/src/main/resources/application.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/tlias
|
||||
username: root
|
||||
password: 1234
|
||||
mybatis:
|
||||
configuration:
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
map-underscore-to-camel-case: true
|
||||
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>hello word!!!</h1>
|
||||
<p>this is a html page</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.inmind;
|
||||
|
||||
import com.inmind.service.DeptService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringbootAopQuickstartApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private DeptService deptService;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAopDelete() {
|
||||
deptService.delete(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAopList() {
|
||||
deptService.list();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAopGet() {
|
||||
deptService.getById(10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user