Compare commits

...

3 Commits

Author SHA1 Message Date
疯狂的狮子Li
2258962770 Revert "!734 update 重写selectOne方法"
This reverts commit f2e0361fb6.
2025-09-01 14:25:47 +08:00
疯狂的狮子Li
655e84012c Revert "update 优化 增加selectOne使用注意事项"
This reverts commit bf10a13088.
2025-09-01 14:25:40 +08:00
疯狂的狮子Li
f683ef00b8 fix 修复 json模块配置 默认覆盖了spring module 配置问题 改为让spring自动加载注册 2025-09-01 11:46:57 +08:00
2 changed files with 26 additions and 45 deletions

View File

@ -1,5 +1,6 @@
package org.dromara.common.json.config;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
@ -28,20 +29,24 @@ import java.util.TimeZone;
@AutoConfiguration(before = JacksonAutoConfiguration.class)
public class JacksonConfig {
@Bean
public Module registerJavaTimeModule() {
// 全局配置序列化返回 JSON 处理
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
javaTimeModule.addDeserializer(Date.class, new CustomDateDeserializer());
return javaTimeModule;
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
// 全局配置序列化返回 JSON 处理
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
javaTimeModule.addDeserializer(Date.class, new CustomDateDeserializer());
builder.modules(javaTimeModule);
builder.timeZone(TimeZone.getDefault());
log.info("初始化 jackson 配置");
};

View File

@ -6,11 +6,9 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.dromara.common.core.utils.MapstructUtils;
@ -132,7 +130,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象
*/
default V selectVoById(Serializable id) {
return this.selectVoById(id, this.currentVoClass());
return selectVoById(id, this.currentVoClass());
}
/**
@ -158,7 +156,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的VO对象列表
*/
default List<V> selectVoByIds(Collection<? extends Serializable> idList) {
return this.selectVoByIds(idList, this.currentVoClass());
return selectVoByIds(idList, this.currentVoClass());
}
/**
@ -184,7 +182,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的VO对象列表
*/
default List<V> selectVoByMap(Map<String, Object> map) {
return this.selectVoByMap(map, this.currentVoClass());
return selectVoByMap(map, this.currentVoClass());
}
/**
@ -210,7 +208,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象
*/
default V selectVoOne(Wrapper<T> wrapper) {
return this.selectVoOne(wrapper, this.currentVoClass());
return selectVoOne(wrapper, this.currentVoClass());
}
/**
@ -221,12 +219,11 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象
*/
default V selectVoOne(Wrapper<T> wrapper, boolean throwEx) {
return this.selectVoOne(wrapper, this.currentVoClass(), throwEx);
return selectVoOne(wrapper, this.currentVoClass(), throwEx);
}
/**
* 根据条件查询单个VO对象并指定返回的VO对象的类型(自动拼接 limit 1)
* 注意不要再自己添加 limit 1 做限制了
* 根据条件查询单个VO对象并指定返回的VO对象的类型
*
* @param wrapper 查询条件Wrapper
* @param voClass 返回的VO对象的Class对象
@ -234,12 +231,11 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的单个VO对象经过类型转换为指定的VO类后返回
*/
default <C> C selectVoOne(Wrapper<T> wrapper, Class<C> voClass) {
return this.selectVoOne(wrapper, voClass, true);
return selectVoOne(wrapper, voClass, true);
}
/**
* 根据条件查询单个实体对象并将其转换为指定的VO对象(自动拼接 limit 1)
* 注意不要再自己添加 limit 1 做限制了
* 根据条件查询单个实体对象并将其转换为指定的VO对象
*
* @param wrapper 查询条件Wrapper
* @param voClass 要转换的VO类的Class对象
@ -255,33 +251,13 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
return MapstructUtils.convert(obj, voClass);
}
/**
* 根据条件查询单条记录自动拼接 limit 1 限制返回 1 条数据不依赖 {@code throwEx} 参数
* 注意不要再自己添加 limit 1 做限制了
* <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对象列表
*
* @return 查询到的VO对象列表
*/
default List<V> selectVoList() {
return this.selectVoList(new QueryWrapper<>(), this.currentVoClass());
return selectVoList(new QueryWrapper<>(), this.currentVoClass());
}
/**
@ -318,7 +294,7 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
* @return 查询到的VO对象分页列表
*/
default <P extends IPage<V>> P selectVoPage(IPage<T> page, Wrapper<T> wrapper) {
return this.selectVoPage(page, wrapper, this.currentVoClass());
return selectVoPage(page, wrapper, this.currentVoClass());
}
/**