I'm having a problem in retrieving a property from myBatis. It says java.lang.NullPointerException
. What I want is to get only the user_id
from the result map which is auto generated after using a function to insert values to the database (I'm using oracle 10g).
My code in my mapper is as follows
<resultMap type="User" id="userMap">
<result property="userId" column="user_id"/>
<result property="someProperty1" column="property_1"/>
<result property="someProperty2" column="property_2"/>
</resultMap>
<insert id="addUser" parameterType="map" statementType="CALLABLE">
{ CALL
#{userResult, javaType=java.sql.ResultSet, jdbcType=CURSOR, mode=OUT, resultMap=userMap} :=
PROJECT.create_user(
#{surname, javaType=String, jdbcType=VARCHAR, mode=IN},
#{givenName, javaType=String, jdbcType=VARCHAR, mode=IN},
#{middleName, javaType=String, jdbcType=VARCHAR, mode=IN}
)}
</insert>
Here is where the java.lang.NullPointerException
occurs
return Integer.parseInt(paramMap.get("userId").toString());
I'm using private Map<String, Object> paramMap = new HashMap<String, Object>();
It seems that I've used paramMap.get("userId")
is wrong but I am not sure with this.
Any suggestions or hints would be very helpful! Thanks a lot!
Getting auto-generated keys back from the database has never been standardized, so it tends to be database-specific. With Oracle, you need to first select the new key value from a sequence, do the insert and return the result. This question has the real oil for a MyBatis project.
Some of the more recent Java ER mapping frameworks are handling this transparently. For instance Spring JDBC Template has a GeneratedKeyHolder object that abstracts the database work. See this question for an example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.