移除ListStringTypeHandler类,添加通用的GenericListTypeHandler类以支持多种类型的列表处理
This commit is contained in:
parent
a6a822e39e
commit
631dd2f5c3
@ -0,0 +1,131 @@
|
|||||||
|
package com.ruoyi.common.handler;
|
||||||
|
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import org.apache.ibatis.type.BaseTypeHandler;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用的List类型处理器,可处理任何元素类型
|
||||||
|
*
|
||||||
|
* 用法示例:
|
||||||
|
* 1. 在XML中配置:
|
||||||
|
* {@code <result column="tags" property="tags"
|
||||||
|
* typeHandler=
|
||||||
|
"com.ruoyi.generator.handler.GenericListTypeHandler$StringList"/>}
|
||||||
|
*
|
||||||
|
* 2. 在字段上使用注解:
|
||||||
|
* {@code @Result(column="tags", property="tags",
|
||||||
|
* typeHandler=GenericListTypeHandler.StringList.class)}
|
||||||
|
*
|
||||||
|
* 3. 在xml插值语法中使用
|
||||||
|
* {@code #{tags,typeHandler=com.ruoyi.common.handler.GenericListTypeHandler$StringList}
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @param <T> 列表元素类型
|
||||||
|
*/
|
||||||
|
public class GenericListTypeHandler<T> extends BaseTypeHandler<List<T>> {
|
||||||
|
|
||||||
|
private final Function<String, T> converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
*
|
||||||
|
* @param converter 字符串到元素类型T的转换器
|
||||||
|
*/
|
||||||
|
protected GenericListTypeHandler(Function<String, T> converter) {
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType)
|
||||||
|
throws SQLException {
|
||||||
|
String value = StringUtils.join(parameter, ",");
|
||||||
|
ps.setString(i, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
return convertToList(rs.getString(columnName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
|
return convertToList(rs.getString(columnIndex));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
|
return convertToList(cs.getString(columnIndex));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<T> convertToList(String value) {
|
||||||
|
if (StringUtils.isBlank(value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<T> list = new ArrayList<>();
|
||||||
|
String[] items = value.split(",");
|
||||||
|
for (String item : items) {
|
||||||
|
if (StringUtils.isNotBlank(item)) {
|
||||||
|
list.add(converter.apply(item.trim()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 内部类 - 常用类型的TypeHandler实现
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String类型列表的TypeHandler
|
||||||
|
*/
|
||||||
|
public static class StringList extends GenericListTypeHandler<String> {
|
||||||
|
public StringList() {
|
||||||
|
super(String::valueOf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integer类型列表的TypeHandler
|
||||||
|
*/
|
||||||
|
public static class IntegerList extends GenericListTypeHandler<Integer> {
|
||||||
|
public IntegerList() {
|
||||||
|
super(Integer::valueOf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Long类型列表的TypeHandler
|
||||||
|
*/
|
||||||
|
public static class LongList extends GenericListTypeHandler<Long> {
|
||||||
|
public LongList() {
|
||||||
|
super(Long::valueOf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Double类型列表的TypeHandler
|
||||||
|
*/
|
||||||
|
public static class DoubleList extends GenericListTypeHandler<Double> {
|
||||||
|
public DoubleList() {
|
||||||
|
super(Double::valueOf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boolean类型列表的TypeHandler
|
||||||
|
*/
|
||||||
|
public static class BooleanList extends GenericListTypeHandler<Boolean> {
|
||||||
|
public BooleanList() {
|
||||||
|
super(Boolean::valueOf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,41 +0,0 @@
|
|||||||
package com.ruoyi.generator.handler;
|
|
||||||
|
|
||||||
import java.sql.CallableStatement;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.ibatis.type.BaseTypeHandler;
|
|
||||||
import org.apache.ibatis.type.JdbcType;
|
|
||||||
|
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
|
||||||
|
|
||||||
public class ListStringTypeHandler extends BaseTypeHandler<List<String>> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType)
|
|
||||||
throws SQLException {
|
|
||||||
String value = StringUtils.join(parameter, ",");
|
|
||||||
ps.setString(i, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
||||||
String value = rs.getString(columnName);
|
|
||||||
return StringUtils.isBlank(value) ? null : StringUtils.str2List(value, ",", true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
||||||
String value = rs.getString(columnIndex);
|
|
||||||
return StringUtils.isBlank(value) ? null : StringUtils.str2List(value, ",", true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
||||||
String value = cs.getString(columnIndex);
|
|
||||||
return StringUtils.isBlank(value) ? null : StringUtils.str2List(value, ",", true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -15,7 +15,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<result property="joinType" column="join_type" />
|
<result property="joinType" column="join_type" />
|
||||||
<result property="orderNum" column="order_num"/>
|
<result property="orderNum" column="order_num"/>
|
||||||
<result property="newTableId" column="new_table_id"/>
|
<result property="newTableId" column="new_table_id"/>
|
||||||
<result property="joinColumns" column="join_columns" typeHandler="com.ruoyi.generator.handler.ListStringTypeHandler"/>
|
<result property="joinColumns" column="join_columns" typeHandler="com.ruoyi.common.handler.GenericListTypeHandler$StringList"/>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<sql id="selectGenJoinTableVo">
|
<sql id="selectGenJoinTableVo">
|
||||||
@ -63,7 +63,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<if test="leftTableFk != null">#{leftTableFk},</if>
|
<if test="leftTableFk != null">#{leftTableFk},</if>
|
||||||
<if test="rightTableFk != null">#{rightTableFk},</if>
|
<if test="rightTableFk != null">#{rightTableFk},</if>
|
||||||
<if test="joinType != null">#{joinType},</if>
|
<if test="joinType != null">#{joinType},</if>
|
||||||
<if test="joinColumns != null">#{joinColumns,typeHandler=com.ruoyi.generator.handler.ListStringTypeHandler},</if>
|
<if test="joinColumns != null">#{joinColumns,typeHandler=com.ruoyi.common.handler.GenericListTypeHandler$StringList},</if>
|
||||||
<if test="orderNum != null">#{orderNum},</if>
|
<if test="orderNum != null">#{orderNum},</if>
|
||||||
<if test="newTableId != null">#{newTableId},</if>
|
<if test="newTableId != null">#{newTableId},</if>
|
||||||
</trim>
|
</trim>
|
||||||
@ -79,7 +79,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<if test="leftTableFk != null"> left_table_fk = #{leftTableFk},</if>
|
<if test="leftTableFk != null"> left_table_fk = #{leftTableFk},</if>
|
||||||
<if test="rightTableFk != null"> right_table_fk = #{rightTableFk},</if>
|
<if test="rightTableFk != null"> right_table_fk = #{rightTableFk},</if>
|
||||||
<if test="joinType != null">join_type = #{joinType},</if>
|
<if test="joinType != null">join_type = #{joinType},</if>
|
||||||
<if test="joinColumns != null">join_columns = #{joinColumns,typeHandler=com.ruoyi.generator.handler.ListStringTypeHandler},</if>
|
<if test="joinColumns != null">join_columns = #{joinColumns,typeHandler=com.ruoyi.common.handler.GenericListTypeHandler$StringList},</if>
|
||||||
<if test="orderNum != null">order_num = #{orderNum},</if>
|
<if test="orderNum != null">order_num = #{orderNum},</if>
|
||||||
<if test="newTableId != null">new_table_id = #{newTableId},</if>
|
<if test="newTableId != null">new_table_id = #{newTableId},</if>
|
||||||
</trim>
|
</trim>
|
||||||
|
Loading…
Reference in New Issue
Block a user