Mybatis中什么时候应该声明jdbcType
2016-07-28 14:04
351 查看
疑问来自于,有时候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
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
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
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
the
just a generic container and
carries no type information. At that point it would be o good idea to provide the
that switching the database implementation later on does not cause any issues with null values.
PS:要慢慢习惯看文档,直接google像吃快餐,不能一直吃!
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
jdbcTypewhen
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
jdbcTypeas
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
HashMapbecause
the
HashMapis
just a generic container and
nullitself
carries no type information. At that point it would be o good idea to provide the
jdbcTypeso
that switching the database implementation later on does not cause any issues with null values.
PS:要慢慢习惯看文档,直接google像吃快餐,不能一直吃!
相关文章推荐
- 随手记
- CCF俄罗斯方块201604-2
- Android酷炫实用的开源框架(UI框架)
- bitmap图像工具
- 关于友谊
- SCPPO(十二):SQL误操作如何恢复?
- 使用 CXF 做 webservice 简单例子(转载)
- Nodejs 配置+基础
- mysql连接超时与jndi数据源配置
- 直播当中的装饰品
- 优酷的菜单
- Spring依赖注入详解
- Trie树的DAT实现
- DLL的结构
- MySQL查询结果保存到本地
- (原+译)win7远程连接ubuntu16.04
- c#——Winform TreeView 赋值(id,name,pid)类型数据
- 使用Egret开发的HTML5项目,使用本地电脑作为服务器进行移动端调试流程
- Maven项目中使用本地JAR包
- PYTHON黑帽编程1.5 使用WIRESHARK练习网络协议分析