简体   繁体   English

为用户定义的Oracle表类型编写MyBatis3 TypeHandler

[英]Writing MyBatis3 TypeHandler for User-Defined Oracle Table Type

I am having difficulty writing a TypeHandler to convert an Oracle user-defined table type to a list of Java objects. 我在编写TypeHandler时遇到困难,无法将Oracle用户定义的表类型转换为Java对象列表。

The database types and function spec are defined like this: 数据库类型和函数规范定义如下:

CREATE OR REPLACE TYPE MySchema.MY_TYPE IS OBJECT (
    first_name    VARCHAR2(50),
    last_name     VARCHAR2(50)
);

CREATE OR REPLACE TYPE MySchema.MY_TYPE_TBL IS TABLE OF MY_TYPE;

FUNCTION GET_MY_STUFF(my_user_name IN VARCHAR2) RETURN MySchema.MY_TYPE_TABLE;

I have a MyBatis mapper that has the following call in it: 我有一个MyBatis映射器,其中包含以下调用:

<resultMap id="myResultMap" type="GetMyStuffResult" />
<select id="getMyStuff" statementType="CALLABLE" parameterType="map">
    {#{return_value, mode=OUT,
                     typeHandler=MyStuffToList
                     jdbcTypeName=MySchema.MY_TYPE_TABLE,
                     jdbcType=ARRAY} = 
         call MySchema.MyPackage.GET_MY_STUFF (
              my_user_name => #{userName, mode=IN, jdbcType=VARCHAR}
     )}
</select>

Finally, I am attempting to write a TypeHandler, but am failing miserably in the getResult override: 最后,我正在尝试编写一个TypeHandler,但是在getResult覆盖中失败了:

public class MyStuffToList implements TypeHandler<List<GetMyStuffResult>> {

    // Other Overrides Here

    @Override
    public List<GetMyStuffResult> getResult(CallableStatement cs, int columnIndex) 
    throws SQLException {
        List<GetMyStuffResult> results = new ArrayList<GetMyStuffResult>();
        Array array = cs.getArray(columnIndex);

        // HOW DO I CONVERT THE Array TO List<GetMyStuffResult> ???

        return results;
    }
}

I cannot seem to get from the CallableStatement passed into the TypeHandler to the list that I want. 我似乎无法从传递给TypeHandlerCallableStatement获得我想要的列表。

Here is what I did to get this to work. 以下是我为实现这一目标所做的工作。

The TypeHandler needs to create a typemap that references the single Oracle type (not the table type): TypeHandler需要创建一个引用单个 Oracle类型(而不是表类型)的类型映射:

public class MyStuffToList implements TypeHandler<List<GetMyStuffResult>> {

    // Other Overrides Here

    @Override
    public List<GetMyStuffResult> getResult(CallableStatement cs, int columnIndex) 
    throws SQLException {
        List<GetMyStuffResult> results = new ArrayList<GetMyStuffResult>();
        Array array = cs.getArray(columnIndex);

        // Add a TypeMap to map the Oracle object to a Java class
        Map<String, Class<?>> typeMap = new HashMap<String, Class<?>>();
        typeMap.put("MySchema.MY_TYPE", GetMyStuffResult.class);

        // Get an array of Java objects using that type map
        Object[] javaObjects = (Object[]) array.getArray(typeMap);

        // add each of these converted objects to the results list
        for (Object javaObject : javaObjects) {
            results.add((GetMyStuffResult) javaObject);
        }

        return results;
    }
}

Then, the DTO class itself needs to implement ORAData and ORADataFactory to glue everything together: 然后,DTO类本身需要实现ORADataORADataFactory将所有内容粘合在一起:

public class GetMyStuffResult implements ORAData, ORADataFactory {

    string first_name;
    string last_name;

    // Assume appropriate getters and setters for all properties here
    // public String getFirstName() {
    //      |
    //      |

    // Implement ORAData and ORADataFactory
    public ORAData create(Datum datum, int sqlType throws SQLException {
       GetMyStuffResult result = new GetMyStuffResult();

       Struct javaStruct = (Struct) datum;
       Object[] attributes = javaStruct.getAttributes();
       // ORDER MATTERS HERE - must be in order defined in Oracle
       result.setFirstName(attributes[0].toString());
       result.setLastName(attributes[1].toString());
    }

    public Datum toDatum(Connection conn) throws SQLException {
        return null;
    }
}

Of course, you'll want to do proper null and data checks, etc. 当然,您需要进行适当的空值和数据检查等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM