java mybatis如何操作postgresql array数组类型
程序员文章站
2022-06-10 12:49:39
目录我定义了几个基础数据类型的数组java mybatis操作 postgresql array数组类型备忘找了半天没有找到postgresql中关于array数组类型的字段如何对应到java中的数据...
找了半天没有找到postgresql中关于array数组类型的字段如何对应到java中的数据类型,后来找到了mybatis的typehandler,只要实现一个自定义的typehandler就行了,如下,
我定义了几个基础数据类型的数组
public class arraytypehandler extends basetypehandler<object[]> { private static final string type_name_varchar = "varchar"; private static final string type_name_integer = "integer"; private static final string type_name_boolean = "boolean"; private static final string type_name_numeric = "numeric"; @override public void setnonnullparameter(preparedstatement ps, int i, object[] parameter, jdbctype jdbctype) throws sqlexception { string typename = null; if (parameter instanceof integer[]) { typename = type_name_integer; } else if (parameter instanceof string[]) { typename = type_name_varchar; } else if (parameter instanceof boolean[]) { typename = type_name_boolean; } else if (parameter instanceof double[]) { typename = type_name_numeric; } if (typename == null) { throw new typeexception("arraytypehandler parameter typename error, your type is " + parameter.getclass().getname()); } // 这3行是关键的代码,创建array,然后ps.setarray(i, array)就可以了 connection conn = ps.getconnection(); array array = conn.createarrayof(typename, parameter); ps.setarray(i, array); } @override public object[] getnullableresult(resultset resultset, string s) throws sqlexception { return getarray(resultset.getarray(s)); } @override public object[] getnullableresult(resultset resultset, int i) throws sqlexception { return getarray(resultset.getarray(i)); } @override public object[] getnullableresult(callablestatement callablestatement, int i) throws sqlexception { return getarray(callablestatement.getarray(i)); } private object[] getarray(array array) { if (array == null) { return null; } try { return (object[]) array.getarray(); } catch (exception e) { } return null; } }
然后在实体类里面对应的字段配置这个转换器就行,如果是配置文件需要在 xxmap.xml中配置对应属性
@columntype(typehandler = com.mvc.typehandler.arraytypehandler.class) private string[] cities; //所操作的城市code列表,为空表示所有城市 @columntype(typehandler = com.mvc.typehandler.arraytypehandler.class) private string[] areatypes; //操作的行业类型code列表,为空表示所有行业 @columntype(typehandler = com.jmtool.mvc.typehandler.arraytypehandler.class) private integer[] targetstatus; //操作的目标数据状态
java mybatis操作 postgresql array数组类型备忘
postgrey sql数据库字段类型varchar[],java使用string[]类型,自定义arraytypehandler
arraytypehandler.java
public class arraytypehandler extends basetypehandler<object[]> { private static final string type_name_varchar = "varchar"; private static final string type_name_integer = "integer"; private static final string type_name_boolean = "boolean"; private static final string type_name_numeric = "numeric"; @override public void setnonnullparameter(preparedstatement ps, int i, object[] parameter, jdbctype jdbctype) throws sqlexception { string typename = null; if (parameter instanceof integer[]) { typename = type_name_integer; } else if (parameter instanceof string[]) { typename = type_name_varchar; } else if (parameter instanceof boolean[]) { typename = type_name_boolean; } else if (parameter instanceof double[]) { typename = type_name_numeric; } if (typename == null) { throw new typeexception("arraytypehandler parameter typename error, your type is " + parameter.getclass().getname()); } // 这3行是关键的代码,创建array,然后ps.setarray(i, array)就可以了 connection conn = ps.getconnection(); array array = conn.createarrayof(typename, parameter); ps.setarray(i, array); } @override public object[] getnullableresult(resultset resultset, string s) throws sqlexception { return getarray(resultset.getarray(s)); } @override public object[] getnullableresult(resultset resultset, int i) throws sqlexception { return getarray(resultset.getarray(i)); } @override public object[] getnullableresult(callablestatement callablestatement, int i) throws sqlexception { return getarray(callablestatement.getarray(i)); } private object[] getarray(array array) { if (array == null) { return null; } try { return (object[]) array.getarray(); } catch (exception e) { } return null; } }
配置文件中映射配置,增加typehandler属性,填写arraytypehandler.java的全路径。
<result column="source_type" jdbctype="array" property="sourcetype" typehandler="com.wang.common.arraytypehandler" />
用到的sql:
<insert id="insert" parametertype="com.wang.pojo.qzf"> insert into t_qz_qzf(id,source_type) values (#{id,jdbctype=varchar},#{sourcetype,jdbctype=array,typehandler="com.wang.common.arraytypehandler"}) </insert>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。