feat(themes): 添加 JsonbTypeHandler 支持 themeTag 字段
新增 PostgreSQL JSONB 类型处理器,使 themeTag 字段可直接映射到数据库 JSONB 列。
This commit is contained in:
@@ -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