!734 update 重写selectOne方法

* update 重写selectOne方法
This commit is contained in:
AprilWind 2025-07-30 02:46:51 +00:00 committed by 疯狂的狮子Li
parent 554152635d
commit f2e0361fb6

View File

@ -6,9 +6,11 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils; import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db; import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.logging.LogFactory;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
@ -130,7 +132,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象 * @return 查询到的单个VO对象
*/ */
default V selectVoById(Serializable id) { default V selectVoById(Serializable id) {
return selectVoById(id, this.currentVoClass()); return this.selectVoById(id, this.currentVoClass());
} }
/** /**
@ -156,7 +158,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的VO对象列表 * @return 查询到的VO对象列表
*/ */
default List<V> selectVoByIds(Collection<? extends Serializable> idList) { default List<V> selectVoByIds(Collection<? extends Serializable> idList) {
return selectVoByIds(idList, this.currentVoClass()); return this.selectVoByIds(idList, this.currentVoClass());
} }
/** /**
@ -182,7 +184,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的VO对象列表 * @return 查询到的VO对象列表
*/ */
default List<V> selectVoByMap(Map<String, Object> map) { default List<V> selectVoByMap(Map<String, Object> map) {
return selectVoByMap(map, this.currentVoClass()); return this.selectVoByMap(map, this.currentVoClass());
} }
/** /**
@ -208,7 +210,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象 * @return 查询到的单个VO对象
*/ */
default V selectVoOne(Wrapper<T> wrapper) { default V selectVoOne(Wrapper<T> wrapper) {
return selectVoOne(wrapper, this.currentVoClass()); return this.selectVoOne(wrapper, this.currentVoClass());
} }
/** /**
@ -219,7 +221,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象 * @return 查询到的单个VO对象
*/ */
default V selectVoOne(Wrapper<T> wrapper, boolean throwEx) { default V selectVoOne(Wrapper<T> wrapper, boolean throwEx) {
return selectVoOne(wrapper, this.currentVoClass(), throwEx); return this.selectVoOne(wrapper, this.currentVoClass(), throwEx);
} }
/** /**
@ -231,7 +233,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象经过类型转换为指定的VO类后返回 * @return 查询到的单个VO对象经过类型转换为指定的VO类后返回
*/ */
default <C> C selectVoOne(Wrapper<T> wrapper, Class<C> voClass) { default <C> C selectVoOne(Wrapper<T> wrapper, Class<C> voClass) {
return selectVoOne(wrapper, voClass, true); return this.selectVoOne(wrapper, voClass, true);
} }
/** /**
@ -251,13 +253,33 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
return MapstructUtils.convert(obj, voClass); return MapstructUtils.convert(obj, voClass);
} }
/**
* 根据条件查询单条记录自动限制返回 1 条数据不依赖 {@code throwEx} 参数
*
* <p>
* <strong>注意</strong>
* 1. 使用 {@code Page<>(1, 1)} 强制分页查询确保 SQL 自动添加 {@code LIMIT 1}因此 {@code throwEx} 参数不再生效
* 2. 原方法的 {@code throwEx} 逻辑多条数据抛异常已被优化掉因为分页查询不会返回多条记录
* </p>
*
* @param queryWrapper 查询条件可为 null
* @param throwEx <del>是否抛出异常已弃用此参数不再生效</del>
* @return 单条记录或无数据时返回 null
*/
@Override
default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, boolean throwEx) {
// 强制分页查询LIMIT 1确保最多返回 1 条记录
List<T> list = this.selectList(new Page<>(1, 1), queryWrapper);
return CollUtil.isEmpty(list) ? null : list.get(0);
}
/** /**
* 查询所有VO对象列表 * 查询所有VO对象列表
* *
* @return 查询到的VO对象列表 * @return 查询到的VO对象列表
*/ */
default List<V> selectVoList() { default List<V> selectVoList() {
return selectVoList(new QueryWrapper<>(), this.currentVoClass()); return this.selectVoList(new QueryWrapper<>(), this.currentVoClass());
} }
/** /**
@ -294,7 +316,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的VO对象分页列表 * @return 查询到的VO对象分页列表
*/ */
default <P extends IPage<V>> P selectVoPage(IPage<T> page, Wrapper<T> wrapper) { default <P extends IPage<V>> P selectVoPage(IPage<T> page, Wrapper<T> wrapper) {
return selectVoPage(page, wrapper, this.currentVoClass()); return this.selectVoPage(page, wrapper, this.currentVoClass());
} }
/** /**