feat(themes): 添加 JsonbTypeHandler 支持 themeTag 字段
新增 PostgreSQL JSONB 类型处理器,使 themeTag 字段可直接映射到数据库 JSONB 列。
This commit is contained in:
@@ -7,6 +7,7 @@ import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalDateTime;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.yolo.keyboard.framework.mybatis.core.type.JsonbTypeHandler;
|
||||
import com.yolo.keyboard.framework.mybatis.core.dataobject.BaseDO;
|
||||
|
||||
/**
|
||||
@@ -14,7 +15,7 @@ import com.yolo.keyboard.framework.mybatis.core.dataobject.BaseDO;
|
||||
*
|
||||
* @author ziin
|
||||
*/
|
||||
@TableName("keyboard_themes")
|
||||
@TableName(value = "keyboard_themes", autoResultMap = true)
|
||||
@KeySequence("keyboard_themes_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@ToString(callSuper = true)
|
||||
@@ -40,6 +41,7 @@ public class KeyboardThemesDO {
|
||||
/**
|
||||
* 主题标签
|
||||
*/
|
||||
@TableField(typeHandler = JsonbTypeHandler.class)
|
||||
private Object themeTag;
|
||||
/**
|
||||
* 主题下载次数
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.yolo.keyboard.framework.mybatis.core.type;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.postgresql.util.PGobject;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* PostgreSQL jsonb 类型处理器
|
||||
*
|
||||
* @author ziin
|
||||
*/
|
||||
@MappedTypes(Object.class)
|
||||
public class JsonbTypeHandler extends BaseTypeHandler<Object> {
|
||||
|
||||
private static final String JSONB_TYPE = "jsonb";
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
|
||||
PGobject pgObject = new PGobject();
|
||||
pgObject.setType(JSONB_TYPE);
|
||||
pgObject.setValue(JSONUtil.toJsonStr(parameter));
|
||||
ps.setObject(i, pgObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
String value = rs.getString(columnName);
|
||||
return parseJson(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
String value = rs.getString(columnIndex);
|
||||
return parseJson(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
String value = cs.getString(columnIndex);
|
||||
return parseJson(value);
|
||||
}
|
||||
|
||||
private Object parseJson(String value) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return JSONUtil.parse(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user