Mybatis中什么时候应该声明jdbcType

疑问来自于,有时候Mapper.xml中

pid = #{pid,jdbcType=INTEGER}

pid = #{pid}

都可以用


那么问题来了,


Mybatis中什么时候应该声明jdbcType?

当Mybatis不能自动识别你传入对象的类型时。


什么情况下,Mybatis不能自动识别我的传入类型?

例如:当你传入空值的时候。


简而言之,加上肯定不会报错。


来源:Is jdbcType necessary in a MyBatis mapper?

引用:

As you mentioned yourself, you need to specify the jdbcType when passing null values for parameters.

Some databases need to know the value's type even if the value itself is NULL. For this reason, for maximum portability, it's the JDBC specification itself that requires the type to be specified and MyBatis needs to pass it along since it's build on top of JDBC.

From the MyBatis documentation:

The JDBC type is only required for nullable columns upon insert, update or delete. This is a JDBC requirement, not a MyBatis one. So even if you were coding JDBC directly, you'd need to specify this type – but only for nullable values.

Most of the times you don't need to specify the jdbcType as MyBatis is smart enough to figure out the type from the objects you are working with. But if you send your parameters to the MyBatis statement inside a HashMap, for example, and one of the parameters is null, MyBatis won't be able to determine the type of the parameter by looking at the HashMap because the HashMap is just a generic container and null itself carries no type information. At that point it would be o good idea to provide the jdbcType so that switching the database implementation later on does not cause any issues with null values.


PS:要慢慢习惯看文档,直接google像吃快餐,不能一直吃!

  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值