当我尝试插入一条主键为空的记录,然后取回键值(oracle 触发器设置键)时,我得到了 MyBatis NPE。
FooMapper接口(interface):
...
public void insertFooObject (final Foo foo);
...
FooMapper.xml:
...
<insert id="insertFooObject" useGeneratedKeys="true" keyColumn="foo_id" keyProperty="fooId">
insert into foos (foo_id, gcor_id, registration_date)
values (#{fooId, jdbcType=NUMERIC}, #{gcorId, jdbcType=NUMERIC}, #{registrationDate, jdbcType=DATE})
</insert>
...
这是模型:
public class Foo {
private final Integer fooId;
private final Integer gcorId;
private final Date registrationDate;
public Foo(final Integer fooId, final Integer gcorId, final Date registrationDate) {
this.fooId = fooId;
this.gcorId = gcorId;
this.registrationDate = registrationDate;
}
...
电话如下:
...
Foo foo = new Foo(null, 229, null);
fooMapper.insertFooObject(foo);
...
fooMapper由spring注入(inject),可以成功用于其他SQL语句。当我传入 fooId 的数字且 RegistrationDate 为 null 时,一切正常。当 fooId 为 null 时(如图所示),我收到错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
Error updating database. Cause: java.lang.NullPointerException
The error may involve defaultParameterMap
The error occurred while setting parameters
SQL: insert into foos (foo_id, gcor_id, registration_date) values (?, ?, ?)
Cause: java.lang.NullPointerException
这是我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<bean id="datasource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@......"/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
</bean>
<bean id="fooMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.foo.FooMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource" />
</bean>
</beans>
有什么想法可以纠正这个问题吗?我没有运气谷歌搜索,并认为通过指定 jdbcType 就可以了。谢谢!
最佳答案
我相信您正在为 Oracle 使用 useGeneeratedkeys
,而 Oracle 不支持该功能。我相信 mysql 和 postgress 支持 generatedKeys。而是使用 selectkeys 来获取下一个值,或者当您可以将 null 值传递给 fooId 时,删除默认为 false
的 useGeneeratedKeys
。您可以使用序列获取下一个值
<selectKey keyProperty="fooId"
resultClass="int">
SELECT nextVal('foo_id_seq')
</selectKey>
关于java - MyBatis 插入 null 时出现 nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27435039/