MyBatis学习过程中遇到的问题

本文详细解析在使用MyBatis框架向Oracle数据库插入数据时遇到的问题,特别是当字段值为null时的错误处理。通过指定jdbcType解决插入空值的问题,并调整keyProperty属性确保正确获取自动生成的主键。
<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~~

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值