mybatis-plus关于乐观锁、SQL分析、非法SQL检查插件的应用

1、乐观锁:OptimisticLockerInterceptor,就是在更新数据的时候,根据版本号,来判断是否为更新的版本,若为更新指定的版本一致,则更新,否则则否。
在实体类添加版本号属性,属性上添加注解@Version
数据库字段的版本号必须为int类型,否则更新后不会自增版本
2、SQL分析插件:SqlExplainInterceptor分析sql执行,避免全表删除及修改
3、非法SQL检查插件,IllegalSQLInterceptor,检查是否有非法SQL,也就是是否有垃圾SQL语句,有则报错
这几个插件,包括分页插件,都是属于同一个包下,分页的在另外的文章分页链接
在这里插入图片描述


代码
1)添加配置文件
MybatissqlSessionFactory配置下 添加

<property name="plugins">
            <array>
                <!--分页查询-->
                <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></bean>
                <!--乐观锁:用于在更新数据库时会查询数据版本号,若版本号一致,才会更新-->
                <bean class="com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor"></bean>
				<!--sql执行分析:避免全表更新或者删除-->
                <bean class="com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor">
                    <property name="sqlParserList">
                        <list>
                            <bean class="com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser"></bean>
                        </list>
                    </property>
                </bean>
                <!--非法sql检查:检查是否有非法sql-->
                <bean class="com.baomidou.mybatisplus.extension.plugins.IllegalSQLInterceptor"></bean>
            </array>
        </property>

2)实体类Admin.java

@TableName("admin")
public class Admin {
    @TableId(value = "admin_account",type = IdType.NONE)
    private String adminAccount;
    private String adminPwd;
    @Version
    private Integer version;
    public Admin() {
    }

    public Admin(String adminAccount, String adminPwd) {
        this.adminAccount = adminAccount;
        this.adminPwd = adminPwd;
    }

    public String getAdminAccount() {
        return adminAccount;
    }

    public void setAdminAccount(String adminAccount) {
        this.adminAccount = adminAccount;
    }

    public String getAdminPwd() {
        return adminPwd;
    }

    public void setAdminPwd(String adminPwd) {
        this.adminPwd = adminPwd;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Admin(String adminAccount, String adminPwd, Integer version) {
        this.adminAccount = adminAccount;
        this.adminPwd = adminPwd;
        this.version = version;
    }
}

(3)测试类

public class MyTest {
	/**
     *  乐观锁插件,
     *  1、使用的时候必须要在配置文件中添加
     *  <bean class="com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor"></bean>
     *  2、在实体类添加版本号属性,属性上添加注解@Version
     *  3、数据库字段的版本号必须为int类型,否则更新后不会自增版本
     */
    @Test
    public void test05(){
        AdminDao bean = applicationContext.getBean(AdminDao.class);
        Admin admin = new Admin();
        admin.setAdminAccount("2333");
        admin.setAdminPwd("5442");
        admin.setVersion(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_account",admin.getAdminAccount());
        int update = bean.update(admin, queryWrapper);
        System.out.println(update);
    }

    /**
     * sql执行分析:分析是否有垃圾sql执行,有的话会报错拦截
     *                 <bean class="com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor">
     *                     <property name="sqlParserList">
     *                         <list>
     *                             <bean class="com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser"></bean>
     *                         </list>
     *                     </property>
     *                 </bean>
     */
    @Test
    public void test06(){
        AdminDao bean = applicationContext.getBean(AdminDao.class);
        Admin admin = new Admin();
        admin.setAdminAccount("2333");
        admin.setAdminPwd("5442");
        admin.setVersion(2);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.or();
        int update = bean.update(admin, queryWrapper);
        System.out.println(update);
    }
	@Test
    public void test07(){
        AdminDao adminDao = applicationContext.getBean(AdminDao.class);
        adminDao.delete(null);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值