Java中使用pgsql的array类型

1.首先定义实体类的类型

    @ApiModelProperty(value = "教材封面文件路径")
    @Column(name = "thumbnail")
    @ColumnType(typeHandler = MyArrayTypeHandler.class)
    private String[] thumbNail;

2.定义mybaties里的resultMap

    <resultMap type="com.tianwen.springcloud.microservice.base.entity.book.Book" id="bookInfo">
        <result column="thumbnail" jdbcType="ARRAY" property="thumbNail"
             typeHandler="com.tianwen.springcloud.microservice.handler.MyArrayTypeHandler"/>
    </resultMap>

3.使用,在insert时指定类型

    <insert id="insert" parameterType="com.tianwen.springcloud.microservice.base.entity.book.Book">
        INSERT INTO T_E_BOOK(
             thumbnail )
        VALUES (
             #{thumbNail,jdbcType=ARRAY,typeHandler =com.tianwen.springcloud.microservice.handler.MyArrayTypeHandler},
        )
    </insert>
      

MyArrayTypeHandler的类实现

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tianwen.springcloud.microservice.handler;

import com.tianwen.springcloud.microservice.exception.EcrException;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

@MappedJdbcTypes({JdbcType.ARRAY})
@MappedTypes({Object.class})
public class MyArrayTypeHandler 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";

    public MyArrayTypeHandler() {
    }

    public void setNonNullParameter(PreparedStatement ps, int i, Object[] parameter, JdbcType jdbcType) throws SQLException {
        String typeName = null;
        if (parameter instanceof Integer[]) {
            typeName = "integer";
        } else if (parameter instanceof String[]) {
            typeName = "varchar";
        } else if (parameter instanceof Boolean[]) {
            typeName = "boolean";
        } else if (parameter instanceof Double[]) {
            typeName = "numeric";
        } else if (parameter instanceof BigDecimal[]) {
            typeName = "numeric";
        }

        if (typeName == null) {
            throw new EcrException("ArrayTypeHandler parameter typeName error, your type is " + parameter.getClass().getName(), new Object[0]);
        } else {
            Connection conn = ps.getConnection();
            Array array = conn.createArrayOf(typeName, parameter);
            ps.setArray(i, array);
        }
    }

    public Object[] getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return this.getArray(resultSet.getArray(s));
    }

    public Object[] getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return this.getArray(resultSet.getArray(i));
    }

    public Object[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return this.getArray(callableStatement.getArray(i));
    }

    private Object[] getArray(Array array) throws SQLException {
        return array == null ? null : (Object[])((Object[])array.getArray());
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值