淺談MyBatis-Plus學習之自定義全局操作及邏輯刪除

一、自定義全局操作介紹

MP中提供擴展AutoSqlInjector可以自定義各種想要的 sql ,注入到全局中,相當於自定義MP 自動注入的方法。
也就是說繼承BaseMapper<T>接口時就帶有的方法,在加載相應的配置環境時就會注入。

二、實現自定義全局操作如下

2.1、在實現的Mapper接口中定義方法

public interface EmployeeMapper extends BaseMapper<Employee> {
    
    /**
     * 自定義注入方法
     * @return
     */
    int deleteAll();
}

2.2、繼承AutoSqlInjector,並重寫內部的inject方法

public class DeleteAllInject extends AutoSqlInjector{
    @Override
    public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass,
            Class<?> modelClass, TableInfo table) {
        /**
         * 核心就是將sql語句和mapper接口中的方法進行映射
         * 封裝成一個MapperStatement對象
         * 然後初始化時Configuration對象自動加載
         */
        String sql = "delete from " + table.getTableName();
        String method = "deleteAll";
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        this.addDeleteMappedStatement(mapperClass, method, sqlSource);
    }
}

2.3、最後在將該Bean注入到全局配置策略中

<!-- mybatis-plus全局配置策略 ,這樣避免重複在每一個實體中使用註解進行配置-->
    <bean id="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
        <property name="dbColumnUnderline" value="true"></property>  <!-- 2.3版本後默認配置數據庫下劃線-->
        <!-- 指定數據庫ID生成策略   0:數據庫自增-->
        <property name="idType" value="0"></property>
        <!-- 指定數據庫表前綴 -->
        <property name="tablePrefix" value="tbl_"></property>
        <!-- 注入全局配置方法 -->
        <property name="sqlInjector" ref="deleteAllInject"></property>
    </bean>
    
     <bean id="deleteAllInject" class="cn.hjj.mp.inject.DeleteAllInject"></bean>

2.4、測試代碼如下

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestAutoSqlInject {
    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Test
    public void testDeleteAll() {
        employeeMapper.deleteAll();
    }
}

 

三、邏輯刪除簡介

邏輯刪除:就是並不是真正的將數據從數據庫中刪除,因爲數據是寶貴的。只是將數據庫中的該刪除的記錄的邏輯刪除字段設置爲刪除狀態

四、實現邏輯刪除操作如下

4.1、創建一個新的實體,並在對應的數據庫中創建相應的表數據

注意標誌字段要使用@TableLogic標識告訴MP知道哪個是標識字段

public class User {
     @TableId(type=IdType.AUTO)
     private Integer id;
     private String name;
     @TableLogic  //標誌是一個邏輯標識符號
     private Integer deleteFlag;
         
         // get Methods
         // set Methods

4.2、在配置文件中進行配置相應的Bean以及參數

applicationContext.xml

<!-- mybatis-plus全局配置策略 ,這樣避免重複在每一個實體中使用註解進行配置-->
    <bean id="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
        <property name="dbColumnUnderline" value="true"></property>  <!-- 2.3版本後默認配置數據庫下劃線-->
        <!-- 指定數據庫ID生成策略   0:數據庫自增-->
        <property name="idType" value="0"></property>
        <!-- 指定數據庫表前綴 -->
        <property name="tablePrefix" value="tbl_"></property>
        <!-- 注入邏輯刪除 -->
        <property name="sqlInjector" ref="logicSqlInjector"></property>
        <!-- 邏輯刪除全局值 -->
        <property name="logicDeleteValue" value="-1"></property>
        <!-- 邏輯未刪除全局值 -->
        <property name="logicNotDeleteValue" value="1"></property>
    </bean>
    <!-- 配置邏輯刪除注入的Bean -->
    <bean id="logicSqlInjector" class="com.baomidou.mybatisplus.mapper.LogicSqlInjector"></bean>

4.3、測試代碼如下

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestLogicDelete {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testDelete() {
        Integer res = userMapper.deleteById(1);
        System.out.println("res: " + res);
    }
    
    @Test
    public void testSelect() {
        User user = userMapper.selectById(1);
        System.out.println("res: " + user);
    }
}

本質:會在進行刪除和查詢的時候的時候附帶邏輯刪除字段,刪除操作其實是一個更新語句,將當前要刪除的記錄的邏輯狀態更新爲配置的“邏輯刪除全局值”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章