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);
    }
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐