MyBatisPlus系列之自定义全局操作

上篇:MyBatisPlus系列之插件扩展

一、自定义全局操作

1、简介

根据MybatisPlus 的AutoSqlInjector可以自定义各种你想要的sql ,注入到全局中,相当于自定义Mybatisplus 自动注入的方法

之前需要在xml中进行配置的SQL语句,现在通过扩展AutoSqlInjector 在加载mybatis环境时就注入

2、AutoSqlInjector【自定义全局操作】

2.1、编码思路

(1)配置自定义全局操作【applicationContext.xml

	<!--TODO  定义MybatisPlus的全局策略配置-->
	<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
		<!-- 在2.3版本以后,dbColumnUnderline 默认值就是true -->
		<property name="dbColumnUnderline" value="false"></property>

		<!-- 全局的主键策略 -->
		<property name="idType" value="0"></property>

		<!-- 全局的表前缀策略配置 -->
		<property name="tablePrefix" value="tbl_"></property>

<!--	注入自定义全局操作-->
    <property name="sqlInjector" ref="mySqlInjector"></property>


</bean>

<!-- 定义自定义注入器 -->
	<bean id="mySqlInjector" class="org.apache.mybatisplus.Injector.MySqlInjector"></bean>

(2)在mapper类写deleteAll方法

package org.apache.mybatisplus.mapper;

import org.apache.mybatisplus.beans.Employee;
import com.baomidou.mybatisplus.mapper.BaseMapper;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author weiyunhui
 * @since 2021-09-19
 */
public interface EmployeeMapper extends BaseMapper<Employee> {
    int deleteAll();

}

(3)自定义全局操作类

package org.apache.mybatisplus.Injector;

import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.mapper.AutoSqlInjector;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;

/**
 * 自定义全局操作
 */
public class MySqlInjector extends AutoSqlInjector {
    /**
     * 扩展inject 方法,完成自定义全局操作
     * @param configuration
     * @param builderAssistant
     * @param mapperClass
     * @param modelClass
     * @param table
     */
    @Override
    public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
        //将EmployeeMapper中定义的deleteAll, 处理成对应的MappedStatement对象,加入到configuration对象中

        //注入的SQL语句
        String sql = "delete from " +table.getTableName();
        //注入的方法名   一定要与EmployeeMapper接口中的方法名一致
        String method = "deleteAll" ;

        //构造SqlSource对象
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);

        //构造一个删除的MappedStatement
        this.addDeleteMappedStatement(mapperClass, method, sqlSource);


    }
}

(4)测试类

package org.apache.test;//package org.apache.test;


import com.baomidou.mybatisplus.plugins.Page;
import org.apache.mybatisplus.beans.Employee;
import org.apache.mybatisplus.mapper.EmployeeMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestMP03 {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	EmployeeMapper employeeMapper = ctx.getBean("employeeMapper", EmployeeMapper.class);

	/**
	 * 测试自定义全局操作
	 */
	@Test
	public void  testMySqlInjector() {
		Integer result = employeeMapper.deleteAll();
		System.out.println("result: " +result );
	}
}

打印输出,错误:

 Error: Full table operation is prohibited. SQL: delete from tbl_employee

3、自定义注入器的应用之 逻辑删除

假删除、逻辑删除: 并不会真正的从数据库中将数据删除掉,而是将当前被删除的这条数据中的一个逻辑删除字段置为删除状态

tbl_user logic_flag = 1 → -1 

(1)com.baomidou.mybatisplus.mapper.LogicSqlInjector

(2)logicDeleteValue 逻辑删除全局值 

(3)logicNotDeleteValue 逻辑未删除全局值

(4)会在mp自带查询和更新方法的sql后面,追加『逻辑删除字段』=『LogicNotDeleteValue默认值』 删除方法: deleteById()和其他delete方法, 底层SQL调用的是update tbl_xxx set 『逻辑删除字段』=『logicDeleteValue默认值』

4、全局Sql注入器应用

4.1、逻辑删除配置

(1)创建tbl_user

CREATE TABLE `tbl_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(50),
  `logic_flag` int(11)) 

(2)添加表数据

 

 (3)配置注入逻辑删除、注入逻辑删除全局值【applicationContext.xml

全局注入

<!-- 注入逻辑删除 -->
<property name="sqlInjector" ref="logicSqlInjector"></property>
	 	
<!-- 注入逻辑删除全局值 -->
<property name="logicDeleteValue" value = "-1"></property>
<property name="logicNotDeleteValue" value="1"></property>

定义逻辑删除

<!-- 逻辑删除 -->
<bean id="logicSqlInjector" class="com.baomidou.mybatisplus.mapper.LogicSqlInjector"></bean>

 如图所示:

(4)编写实体类

package org.apache.mybatisplus.beans;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableLogic;
import lombok.Data;

@Data
public class User {
    private Integer id  ;
    private String name ;
    @TableLogic   // 逻辑删除属性
    @TableField(value = "logic_flag")
    private Integer logicFlag ;
}

(5)编写mapper接口

package org.apache.mybatisplus.mapper;


import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.mybatisplus.beans.User;

public interface UserMapper extends BaseMapper<User> {
}

 (5)写测试类

package org.apache.test;//package org.apache.test;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableLogic;
import lombok.Data;

public class TestMP03 {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	UserMapper userMapper = ctx.getBean("userMapper",UserMapper.class);

	/**
	 * 测试逻辑删除
	 */
	@Test
	public void testLogicDelete() {
		Integer result = userMapper.deleteById(1);
		System.out.println("result:" +result );
	}
}

打印输出

查看数据表

 

当我们想知道,在表中的数据被执行过逻辑删除操作,我们可以通过查询方式去判断它是否执行该操作,如果表中确定是有数据,但是查询出来返回为null,说明它是执行了逻辑删除的操作

如:

package org.apache.test;//package org.apache.test;


import org.apache.mybatisplus.beans.User;
import org.apache.mybatisplus.mapper.UserMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestMP03 {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	//EmployeeMapper employeeMapper = ctx.getBean("employeeMapper", EmployeeMapper.class);
	UserMapper userMapper = ctx.getBean("userMapper",UserMapper.class);

	/**
	 * 测试逻辑查询
	 */
	@Test
	public void testLogicDelete() {
		User user = userMapper.selectById(1);
		System.out.println(user);

	}
}

打印输出

 数据表信息:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值