1.文件內容
package com.ruoyi.business.mybatisplus.base;import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;/*** 擴展的 Service 接口* 所有自定義 Service 接口都需要繼承此接口** @param <T> 實體類類型*/
public interface BaseService<T> extends IService<T> {/*** 獲取表名*/String getTableName();/*** 查詢所有數據(忽略自動條件)*/List<T> listAll(Wrapper<T> wrapper);Integer deleteAll(Wrapper<T> wrapper);
}
package com.ruoyi.business.mybatisplus.base;import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import java.util.*;
import java.util.function.Function;/*** 擴展的 Service 實現類* 所有自定義 Service 實現類都需要繼承此類** @param <M> Mapper 類型* @param <T> 實體類類型*/
public class BaseServiceImpl <M extends BaseMapperExt<T>, T>extends ServiceImpl<M, T> implements BaseService<T>{/*** 獲取表名*/@Overridepublic String getTableName() {TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);return tableInfo != null ? tableInfo.getTableName() : "";}/*** 查詢所有數據*/@Overridepublic List<T> listAll(Wrapper<T> wrapper) {return baseMapper.selectAll(getTableName(), wrapper);}/*** 刪除滿足條件的所有數據* @param wrapper* @return*/@Overridepublic Integer deleteAll(Wrapper<T> wrapper) {return baseMapper.deleteAll(getTableName(), wrapper);}
}
package com.ruoyi.business.mybatisplus.base;import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;import java.util.List;/*** 通用Mapper擴展接口* @param <T> 實體類型*/
public interface BaseMapperExt<T> extends BaseMapper<T> {/*** 查詢所有數據(忽略邏輯刪除等自動條件)* @param wrapper 查詢條件* @return 數據列表*/@Select("SELECT * FROM ${tableName} ${ew.customSqlSegment}")List<T> selectAll(@Param("tableName") String tableName,@Param(Constants.WRAPPER) Wrapper<T> wrapper);@Delete("DELETE FROM ${tableName} ${ew.customSqlSegment}")Integer deleteAll(@Param("tableName") String tableName,@Param(Constants.WRAPPER) Wrapper<T> wrapper);
}
2.引入方式
public interface IMingmenCommemorate3dService extends BaseService<MingmenCommemorate3d>{}
public class MingmenCommemorate3dServiceImpl extends BaseServiceImpl<MingmenCommemorate3dMapper, MingmenCommemorate3d> implements IMingmenCommemorate3dService{}
public interface MingmenCommemorate3dMapper extends BaseMapperExt<MingmenCommemorate3d>
{}
3.調用
LambdaQueryWrapper<MingmenAlbumImage> wrapperMingmenAlbumImage = Wrappers.lambdaQuery();wrapperMingmenAlbumImage.eq(MingmenAlbumImage::getAlbumId, album.getId());List<MingmenAlbumImage> albumImageList = albumImageService.listAll(wrapperMingmenAlbumImage);albumImageService.deleteAll(wrapperMingmenAlbumImage);