简体   繁体   English

如何从 MyBatis 获取值?

[英]How to get the value from MyBatis?

I'm having a problem in retrieving a property from myBatis.我在从 myBatis 检索属性时遇到问题。 It says java.lang.NullPointerException .它说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).我想要的是仅从使用函数将值插入数据库后自动生成的结果映射中获取user_id (我使用的是 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这是发生java.lang.NullPointerException地方

return Integer.parseInt(paramMap.get("userId").toString());

I'm using private Map<String, Object> paramMap = new HashMap<String, Object>();我正在使用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.似乎我使用paramMap.get("userId")是错误的,但我不确定这一点。

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.使用 Oracle,您需要首先从序列中选择新的键值,进行插入并返回结果。 This question has the real oil for a MyBatis project. 这个问题对 MyBatis 项目有真正的意义。

Some of the more recent Java ER mapping frameworks are handling this transparently.一些较新的 Java ER 映射框架正在透明地处理此问题。 For instance Spring JDBC Template has a GeneratedKeyHolder object that abstracts the database work.例如 Spring JDBC Template 有一个 GeneratedKeyHolder 对象,它抽象了数据库工作。 See this question for an example.有关示例,请参阅此问题

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

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