山东省职业能力建设处网站,派多格宠物网站建设,文字一键生成图片,美术馆网站建设概述背景
公司要求使用磐维数据库#xff0c;于是去了解了这个是基于PostgreSQL构建的#xff0c;在使用时有场景一条图片数据中可以投放到不同的页面#xff0c;由于简化设计就放在数组中#xff0c;于是使用了text[]类型存储#xff1b;表结构
#这是一个简化版表结构…背景
公司要求使用磐维数据库于是去了解了这个是基于PostgreSQL构建的在使用时有场景一条图片数据中可以投放到不同的页面由于简化设计就放在数组中于是使用了text[]类型存储表结构
#这是一个简化版表结构id自增一般设置serial4自增int4类型
CREATE TABLE public.t_expand (id int4 NOT NULL DEFAULT nextval(t_expand_id_seq::regclass),role_id int4 DEFAULT NULL,expand_role_ids text[] COLLATE pg_catalog.default DEFAULT NULL,environment varchar(64) COLLATE pg_catalog.default DEFAULT NULL::character varying,remark varchar(255) COLLATE pg_catalog.default DEFAULT NULL::character varying
);实体类
Data
public class TExpand implements Serializable {/*** 主键id*/private Integer id;/*** 角色id*/private Integer roleId;/*** 外部角色id绑定*/private ListString expandRoleIds;/*** 环境标识*/private String environment;/*** 备注*/private String remark;private static final long serialVersionUID 1L;
}mybatis 配置
注意text[]不能直接对应java的ListString类型故需要自己转换
public class TextListTypeHandler extends BaseTypeHandlerListString {Overridepublic void setNonNullParameter(PreparedStatement ps, int i, ListString parameter, JdbcType jdbcType) throws SQLException {// 将 ListString 转换为 PostgreSQL text[] 类型Array array ps.getConnection().createArrayOf(text, parameter.toArray());ps.setArray(i, array);}Overridepublic ListString getNullableResult(ResultSet rs, String columnName) throws SQLException {return toList(rs.getArray(columnName));}Overridepublic ListString getNullableResult(ResultSet rs, int columnIndex) throws SQLException {return toList(rs.getArray(columnIndex));}Overridepublic ListString getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {return toList(cs.getArray(columnIndex));}private ListString toList(Array array) throws SQLException {if (array null) {return null;}return Arrays.asList((String[]) array.getArray());}
}resultMap idBaseResultMap typecom.domain.TExpandresult propertyid columnid jdbcTypeINTEGER/result propertyroleId columnrole_id jdbcTypeINTEGER/result propertyexpandRoleIds columnexpand_role_ids jdbcTypeARRAY javaTypejava.util.List typeHandlercom.TextListTypeHandler/result propertyenvironment columnenvironment jdbcTypeVARCHAR/result propertyremark columnremark jdbcTypeVARCHAR//resultMap#在使用此参数查询时也要指定类型否则无法匹配类型select idqueryAllByExpandRoleIds resultMapBaseResultMapselectinclude refidBase_Column_List /from t_expandwhere expand_role_ids ![CDATA[]] ARRAY[foreach itemrole collectionroles separator,#{role}::text/foreach]::text[]/select
#插入修改也是要匹配insert idinsertSelectiveinsert into t_expandtrim prefix( suffix) suffixOverrides,if testid ! nullid,/ifif testroleId ! nullrole_id,/ifif testexpandRoleIds ! nullexpand_role_ids,/ifif testenvironment ! nullenvironment,/ifif testremark ! nullremark,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testid ! null#{id,jdbcTypeINTEGER},/ifif testroleId ! null#{roleId,jdbcTypeINTEGER},/ifif testexpandRoleIds ! null#{expandRoleIds,jdbcTypeARRAY,typeHandlercom.TextListTypeHandler},/ifif testenvironment ! null#{environment,jdbcTypeVARCHAR},/ifif testremark ! null#{remark,jdbcTypeVARCHAR},/if/trim/insert常见问题
IllegalArgumentException: invalid comparison: java.util.ArrayList and java.lang.string] with root ca
if testtouchCode ! null and touchCode ! and s.touch_code ![CDATA[]] ARRAY[foreach itemcode collectiontouchCode separator,#{code}::text/foreach]::text[]
/if
这里and touchCode ! 会影响传参应当去掉或者使用size函数
if testtouchCode ! null
或者
if testtouchCode ! null and touchCode.size0