最新代码
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.starry.admin.common.conf;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -7,7 +8,6 @@ import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -16,13 +16,18 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据库数据-String 和 List 自动转换
|
||||
*
|
||||
* @author admin
|
||||
*/
|
||||
@Slf4j
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR) //数据库类型
|
||||
@MappedTypes({List.class}) //java数据类型
|
||||
public abstract class ListTypeHandler<T> extends BaseTypeHandler<List<T>> {
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
@MappedTypes({List.class})
|
||||
public abstract class AbstractListTypeHandler<T> extends BaseTypeHandler<List<T>> {
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType) throws SQLException {
|
||||
String content = StringUtils.isEmpty(parameter) ? null : JSON.toJSONString(parameter);
|
||||
String content = StrUtil.isEmptyIfStr(parameter) ? null : JSON.toJSONString(parameter);
|
||||
ps.setString(i, content);
|
||||
}
|
||||
|
||||
@@ -42,7 +47,7 @@ public abstract class ListTypeHandler<T> extends BaseTypeHandler<List<T>> {
|
||||
}
|
||||
|
||||
private List<T> getListByJsonArrayString(String content) {
|
||||
return StringUtils.isEmpty(content) ? new ArrayList<>() : JSON.parseObject(content, this.specificType());
|
||||
return StrUtil.isEmptyIfStr(content) ? new ArrayList<>() : JSON.parseObject(content, this.specificType());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.starry.admin.common.conf;
|
||||
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class StringTypeHandler extends ListTypeHandler<String> {
|
||||
// 将ListTypeHandler<T>(T为任意对象),具体为特定的对象String
|
||||
/**
|
||||
* @author admin
|
||||
*/
|
||||
public class StringTypeHandler extends AbstractListTypeHandler<String> {
|
||||
@Override
|
||||
protected TypeReference<List<String>> specificType() {
|
||||
return new TypeReference<List<String>>() {
|
||||
|
||||
Reference in New Issue
Block a user