<insert id="insert" parameterType="xxx.User"
useGeneratedKeys="true"
keyProperty="id">
INSERT INTO x_users (
username,
password,
phone,
email,
) VALUES (
#{username},
#{password},
#{phone},
#{email},
)
</insert>
一、如上代码,测试过程中向Oracle中添加数据时,某项为null时无法正确添加,报错如下:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property=‘gender’, mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId=‘null’, jdbcTypeName=‘null’, expression=‘null’}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #5 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
查阅大佬解决问题的方案,在insert时应该在values中指定jdbcType:
VALUES (
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR},
)
然后再执行插入操作的时候就可以将数据正确插入,但是Junti栏依旧报错:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column #1 from result set.
这里再次寻找大佬解决问题的方案
<insert id="insert" parameterType="xxx.User"
useGeneratedKeys="true"
keyProperty="id">
INSERT INTO x_users (
username,
password,
phone,
email,
) VALUES (
#{username},
#{password},
#{phone},
#{email},
)
</insert>
前辈指出,将keyProperty="id"改为keyProperty=“record.id”,然后我试着跟着改了一下运行就没有问题了,但是由于是新手,没有办法看出前辈解决这个问题的思路是什么,这个问题出在哪里,只能将就先将报错解决,待日后厉害了再来思考这个问题orz
附上record.id的链接~~如有大佬解惑,感激不尽orz~~
本文详细解析在使用MyBatis框架向Oracle数据库插入数据时遇到的问题,特别是当字段值为null时的错误处理。通过指定jdbcType解决插入空值的问题,并调整keyProperty属性确保正确获取自动生成的主键。
1001

被折叠的 条评论
为什么被折叠?



