Reputation: 81
I got a magic problem when handling oracle db query operaiotn with mybatis. I query some columns out of my oracle database and set the resultType to Map,the query worked fine but when I tried to get a value from the Map variable and transfer the value to a String Object,I got a cast Exception:“java.sql.Timestamp cannot be cast to java.lang.String”
My Java Mapper code are like following:
@Mapper
public interface DynamicDataMapper {
Map<String, String> selectDataDetail(@Param("traceRuleDO") TraceRuleDO traceRuleDO, @Param("traceQueryVO") TraceQueryVO traceQueryVO);
}
My Mybatis XML mapper files are like following:
<select id="selectDataDetail" resultType="java.util.Map" statementType="STATEMENT" >
<!--select ${traceRuleDO.shownColumns}-->
select *
from ${traceRuleDO.tableName}
where ${traceRuleDO.primaryKey} = ${traceQueryVO.keyValue}
<if test="traceQueryVO.secondaryKeyName != null and traceQueryVO.secondaryKeyName != ''">
and ${traceRuleDO.secondaryKey} = ${traceQueryVO.secondaryKeyValue}
</if>
</select>
When I query a table with this interface,the query worked.but when I tried to get a value that of "Date” types in oracle database and tried to set the value to a String type variable,the exception occured:
String date = dataMap.get("startDate");//the startDate is of "Date” type in db
what Confused me is that why is it ok to query and get a result but not when I got value from the Map?
Besides the cause of this problem, I am also curious about the solution to meet such type of query needs.Thanks for your help!
Upvotes: 0
Views: 518
Reputation: 148
java generic types would be erasured after compiled, so the type of value in map was the actually type, not the type you declared by generic
Upvotes: 0
Reputation: 56
i think String date = dataMap.get("startDate");
like String date = (String)Object;
,if object is not String, then whill throw a ClassCastException, you can change Map<String, String>
to Map<String, Object>
or create a mybatis typeHandlers. you can try it, I'm not sure I'm right, but i hope i can help you.
Upvotes: 2