日期:2014-05-18  浏览次数:20724 次

MyBatis Oracle 主键生成
本帖最后由 netes_hai 于 2010-12-06 03:29:08 编辑

<insert id="addPerson" parameterType="org.mybatis.dao.vo.Person">
<selectKey keyProperty="pid" resultType="int" order="BEFORE">
select person_seq.nextVal from dual
</selectKey>
insert into person(pid, pname, age, birthday )
values( #{pid}, #{pname}, #{age}, #{birthday} )
</insert>


// add
Person person = new Person();
//person.setPid(99);
person.setPname("周");
person.setAge(23);
person.setBirthday(Date.valueOf("2009-09-09"));
 
impl.addPerson(person);

异常信息:
Cause: org.apache.ibatis.type.TypeException: Error setting null parameter.  Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: 无效的列类型

映射文件改成:

<insert id="addPerson" parameterType="org.mybatis.dao.vo.Person">
<selectKey keyProperty="pid" resultType="int" order="BEFORE">
select person_seq.nextVal from dual
</selectKey>
insert into person(pid, pname, age, birthday )
values( #{pid,jdbcType=INTEGER}, #{pname,jdbcType=VARCHAR}, 
#{age,jdbcType=INTEGER}, #{birthday,jdbcType=DATE} )
</insert>

异常信息:
### Cause: java.sql.SQLException: ORA-01400: 无法将 NULL 插入 ("SCOTT"."PERSON"."PID")

故此怀疑是主键的问题,但也不是很确定,所以“MyBatis Oracle 主键生成”这个标题不合适了。
我用的是MyBaits,不是之前的IBatis。
那位兄弟知道这个问题出在哪里呢,指点指点。

------解决方案--------------------
对myibatis不是太了解
1. 你的Person类里的id是int类型还是Integer?
2. 可以使用工具看下select person_seq.nextVal from dual这句是否执行了?
------解决方案--------------------
我也遇到这个问题,重新发布后再启动就好了。
------解决方案--------------------
org.mybatis.dao.vo.Person