我在编写TypeHandler时遇到困难,无法将Oracle用户定义的表类型转换为Java对象列表。
数据库类型和功能规范的定义如下:
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;
我有一个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>
最后,我正在尝试编写一个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;
}
}
我似乎无法从传递到CallableStatement
的{{1}}到我想要的列表。
答案 0 :(得分:0)
以下是我为此付出的努力。
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;
}
}
然后,DTO类本身需要实现ORAData
和ORADataFactory
来粘合所有内容:
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;
}
}
当然,您需要进行适当的空值和数据检查等。