一个完整的切面小例子
controller层:
package com..aop_mybatis.controller; import com..aop_mybatis.entity.AopExsInfo; import com..aop_mybatis.mapper.AopExsInfoMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.List; @Controller public class AopExController { @Autowired private AopExsInfoMapper aopExsInfoMapper; //http://localhost:8080/selectById/1 @RequestMapping(value="/selectById/{id}") public @ResponseBody String selectById(@PathVariable("id") Integer id) { AopExsInfo aopExsInfo = aopExsInfoMapper.selectByPrimaryKey(id); System.out.println(aopExsInfo); if(aopExsInfo==null) return "查无此数据"; return aopExsInfo.toString(); } //http://localhost:8080/selectById2?id=1 @RequestMapping(value="/selectById2") public @ResponseBody String selectById2(HttpServletRequest request) { String id = request.getParameter("id"); if(id==null) return "id为空!"; AopExsInfo aopExsInfo = aopExsInfoMapper.selectByPrimaryKey(Integer.parseInt(id)); System.out.println(aopExsInfo); if(aopExsInfo==null) return "查无此数据"; return aopExsInfo.toString(); } @RequestMapping(value="/insert") public @ResponseBody int insert() { AopExsInfo aopExsInfo = new AopExsInfo(); aopExsInfo.setId(2); aopExsInfo.setCupSize("B"); aopExsInfo.setAge(22); aopExsInfo.setMoney(200.00); int mu = aopExsInfoMapper.insert(aopExsInfo); System.out.println(mu); return mu; } @GetMapping(value="/list") public @ResponseBody List<AopExsInfo> list(){ List<AopExsInfo> list = aopExsInfoMapper.selectAll(); System.out.println(list); return list; } }
切面类:
package com..aop_mybatis.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.Objects; @Aspect @Component public class HttpAspect { private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); //切入点 @Pointcut("execution(public * com..aop_mybatis.controller.AopExController.* (..))") public void log(){ } @Before("log()") public void doBefore(JoinPoint joinPoint){ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); request.getServletPath(); System.out.println("请求的url :"+request.getRequestURL()); System.out.println("GET/POST :"+ request.getMethod()); System.out.println("请求的IP :"+request.getRemoteAddr()); System.out.println("类方法 :"+joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); System.out.println("参数 :"+ Arrays.toString(joinPoint.getArgs())); } @After("log()") public void doAfter(JoinPoint joinPoint){ System.out.println("请求完成"); } @AfterReturning(returning = "object", pointcut = "log()") public void doAfterReturning(Object object) { System.out.println("响应内容 :"+object.toString()); } }
数据库实体类:
package com..aop_mybatis.entity; public class AopExsInfo { private Integer id; private String cupSize; private Integer age; private Double money; public Integer getId() { return id; } public void setId(Integer id){ this.id = id; } public String getCupSize(){ return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize == null ? null : cupSize.trim(); } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "AopExsInfo{" + "id=" + id + ", cupSize='" + cupSize + '\'' + ", age=" + age + ", money=" + money + '}'; } }
数据库接口类:
package com..aop_mybatis.mapper; import com..aop_mybatis.entity.AopExsInfo; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Component; import java.util.List; @Mapper @Component(value = "AopExsInfoMapper") public interface AopExsInfoMapper { int deleteByPrimaryKey(Integer id); int insert(AopExsInfo record); AopExsInfo selectByPrimaryKey(Integer id); List<AopExsInfo> selectAll(); int updateByPrimaryKey(AopExsInfo record); }
mybatis 链接数据库 xml 配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com..aop_mybatis.mapper.AopExsInfoMapper"> <resultMap id="BaseResultMap" type="com..aop_mybatis.entity.AopExsInfo"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="cup_size" jdbcType="VARCHAR" property="cupSize" /> <result column="age" jdbcType="INTEGER" property="age" /> <result column="money" jdbcType="DOUBLE" property="money" /> </resultMap> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from aopexs where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com..aop_mybatis.entity.AopExsInfo"> insert into aopexs (id, cup_size, age, money) values (#{id,jdbcType=INTEGER}, #{cupSize,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{money,jdbcType=DOUBLE}) </insert> <update id="updateByPrimaryKey" parameterType="com..aop_mybatis.entity.AopExsInfo"> update aopexs set cup_size = #{cupSize,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, money = #{money,jdbcType=DOUBLE} where id = #{id,jdbcType=INTEGER} </update> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select id, cup_size, age, money from aopexs where id = #{id,jdbcType=INTEGER} </select> <select id="selectAll" resultMap="BaseResultMap"> select id, cup_size, age, money from aopexs </select> </mapper>
application.yml 配置:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver #数据库连接地址 url: jdbc:mysql://localhost:3306/ac-new?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai #数据库连接用户名 username: root #数据库连接密码 password: root #进行数据库连接池的配置 dbcp2: #初始化提供的连接数 initial-size: 5 #数据库连接池的最小维持连接数 min-idle: 5 #最大的连接数 max-total: 5 #等待连接获取的最大超时时间 max-wait-millis: 200 validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false #mybatis配置 mybatis: mapper-locations: - classpath:mapper/*.xml