MyBatis映射器中是否需要jdbcType? [英] Is jdbcType necessary in a MyBatis mapper?

查看:735
本文介绍了MyBatis映射器中是否需要jdbcType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索,但不清楚.使用MyBatis映射器时,是否需要设置jdbcType?我正在MySql中使用它.

I've been searching and I don't have this very clear. When using a MyBatis mapper, is it necessary to set the jdbcType? I'm using it with MySql.

对于我所读的内容,它是用于传递空值的,但是我不知道这是否仍然必要或是否已经过时.例如,这两个查询都可以工作:

For what I've read, it's for when you pass null values, but I don't know if this is still necessary or it's something old. For example, both of these queries work:

SELECT <include refid="columns"/> FROM user WHERE uid=#{uid, jdbcType=INTEGER}

SELECT <include refid="columns"/> FROM user WHERE uid=#{uid}

推荐答案

正如您自己提到的那样,在传递参数的空值时需要指定jdbcType.

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

即使值本身为NULL,某些数据库也需要知道该值的类型.因此,为获得最大的可移植性,它是

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.

MyBatis文档:

只有在插入,更新或删除时可为空的列才需要JDBC类型.这 是JDBC的要求,而不是MyBatis的要求.因此,即使您是直接编码JDBC,也需要指定此类型-但仅适用于可为空的值.

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.

大多数时候您不需要指定jdbcType,因为MyBatis足够聪明,可以从正在使用的对象中找出类型.但是,例如,如果将参数发送到HashMap内的MyBatis语句,并且其中一个参数为null,则MyBatis将无法通过查看HashMap来确定参数的类型,因为HashMap只是一个通用容器,null本身不携带任何类型信息.到那时,最好提供jdbcType,这样以后再打开数据库实现时就不会引起任何带有空值的问题.

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.

这篇关于MyBatis映射器中是否需要jdbcType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆