@Intercepts为基础实现数据完整性保护

本文介绍了如何使用Mybatis的@Intercepts注解创建拦截器,拦截query、insert、update、delete等数据库操作,以确保数据完整性。Interceptor接口的plugin方法用于封装目标对象,intercept方法在拦截时执行,setProperties方法则用于设置配置属性。
摘要由CSDN通过智能技术生成

本文以@Intercepts为基础,通过拦截器的方式拦截数据库操作包括query、insert、update、delete操作对数据的完整性保护。

@Intercepts是mybatis中的一个常用拦截器注解,表明当前对象是一个拦截器,当前类通过implements Interceptor实现Interceptor接口。

@Intercepts的配置是一个@Signature数组,@Signature用于声明要拦截的接口、方法和参数。

使用方式:

@Intercepts({

@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})

})

@Intercepts({

@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})

})

上面其中method="update"代表的是:update包含insert、update、delete操作。

实现Interceptor接口,可以看到Interceptor接口包含三哥方法。

1、plugin 方法

plugin 方法是拦截器用于封装目标对象的,通过该方法我们可以返回目标对象本身,也可以返回一个它的代理。当返回的是代理的时候我们可以对其中的方法进行拦截来调用 intercept 方法,当然也可以调用其他方法,

2、 intercept 方法

intercept 方法就是要进行拦截的时候要执行的方法。

3、setProperties 方法

setProperties 方法是用于在 Mybatis 配置文件中指定一些属性的。

参考:Mybatis自定义拦截器 | 拙计 (gitee.io)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值