mybatis-plus 3.X 配置


官網配置參數說明地址:https://mp.baomidou.com/config/#logicdeletevalue
本地配置:yml
mybatis-plus:
  mapper-locations: classpath*:mybatis/*.xml
  type-aliases-package: com.genergy.cloud.model.entity
  global-config:
    db-config:
      field-strategy: not_empty
      id-type: auto
      db-type: mysql
    banner: false
    super-mapper-class: com.genergy.cloud.mapper.BaseMapper

 

package com.genergy.cloud.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;


/**
 * BaseMapper 基於 MP 刪減
 *
 * @author Caratacus
 * @see com.baomidou.mybatisplus.core.mapper.BaseMapper
 */
public interface BaseMapper<T> {

    /**
     * <p>
     * 插入一條記錄
     * </p>
     *
     * @param entity 實體對象
     */
    int insert(T entity);

    /**
     * <p>
     * 批量插入數據
     * </p>
     *
     * @param entityList 實體對象集合
     */
    int insertBatchSomeColumn(@Param("list") Collection<T> entityList);

    /**
     * <p>
     * 根據 ID 刪除
     * </p>
     *
     * @param id 主鍵ID
     */
    int deleteById(Serializable id);

    /**
     * <p>
     * 根據 entity 條件,刪除記錄
     * </p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 ID 修改
     * </p>
     *
     * @param entity 實體對象
     */
    int updateById(@Param(Constants.ENTITY) T entity);

    /**
     * <p>
     * 根據 ID 修改
     * </p>
     *
     * @param entity 實體對象
     */
    int updateAllColumnById(@Param(Constants.ENTITY) T entity);

    /**
     * <p>
     * 根據 whereEntity 條件,更新記錄
     * </p>
     *
     * @param entity        實體對象 (set 條件值,不能為 null)
     * @param updateWrapper 實體對象封裝操作類(可以為 null,里面的 entity 用於生成 where 語句)
     */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

    /**
     * <p>
     * 根據 ID 查詢
     * </p>
     *
     * @param id 主鍵ID
     */
    T selectById(Serializable id);

    /**
     * <p>
     * 根據 Wrapper 條件,查詢總記錄數
     * </p>
     *
     * @param queryWrapper 實體對象
     */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 entity 條件,查詢全部記錄
     * </p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 Wrapper 條件,查詢全部記錄
     * 注意: 只返回第一個字段的值
     * </p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 entity 條件,查詢全部記錄(並翻頁)
     * </p>
     *
     * @param page         分頁查詢條件(可以為 RowBounds.DEFAULT)
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

}
package com.genergy.cloud.mybatisplus;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
import com.baomidou.mybatisplus.core.injector.methods.*;
import com.baomidou.mybatisplus.extension.injector.methods.additional.InsertBatchSomeColumn;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * MybatisPlusSql注入器
 * </p>
 *
 * @author Caratacus
 */
public class MybatisPlusSqlInjector extends AbstractSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList() {
        return Stream.of(
                new Insert(),
                new InsertBatchSomeColumn(t -> true),
                new Delete(),
                new DeleteById(),
                new Update(),
                new UpdateById(),
                new UpdateAllColumnById(),
                new SelectById(),
                new SelectCount(),
                new SelectObjs(),
                new SelectList(),
                new SelectPage()
        ).collect(Collectors.toList());
    }

}
package com.genergy.cloud.mybatisplus.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.genergy.cloud.mybatisplus.CommonMetaObjectHandler;
import com.genergy.cloud.mybatisplus.MybatisPlusSqlInjector;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MybatisPlus 配置
 *
 * @author Caratacus
 */
@Configuration
public class MybatisPlusAutoConfiguration {

    /**
     * 分頁
     *
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 樂觀鎖
     *
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 自動填充
     *
     * @return
     */
    @Bean
    public CommonMetaObjectHandler commonMetaObjectHandler() {
        return new CommonMetaObjectHandler();
    }

    /**
     * 自定義注入語句
     *
     * @return
     */
    @Bean
    public MybatisPlusSqlInjector mybatisPlusSqlInjector() {
        return new MybatisPlusSqlInjector();
    }
}

 

注意:
super-mapper-class: com.genergy.cloud.mapper.BaseMapper 官網好像沒有找到super-mapper-class配置說明,如果不配置該項會導致明明繼承了basemapper 但是里面的方法一直報找不到。原因好像在controller時 調用aop,通過sql注入器尋到方法。
 
 
 


 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM