MyBatis中的JdbcType映射介绍

2022-07-22 09:41:12 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

Java项目涉及到数据库交互,以往常用的是JDBC,现在则有Hibernate、Mybatis等这些持久化支持。

项目中用到了MyBatis,和JDBC最显著的区别,就是SQL语句配置化,通过xml文件定义SQL语句,当然JDBC也可以将SQL配置化,需要定制开发,MyBatis则直接支持这种方法。

官方对于MyBatis的介绍,

MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.

简单来讲,MyBatis几乎屏蔽了所有JDBC代码,用一种简单的xml,或者注解,就能完成数据库交互。

xml配置文件,可用MyBatis自己定义的数据类型,引自:http://www.mybatis.org/mybatis-3/configuration.html

Associated JDBC type can be specified by two means:

  • Adding a jdbcType attribute to the typeHandler element (for example: jdbcType=”VARCHAR”).
  • Adding a @MappedJdbcTypes annotation to your TypeHandler class specifying the list of JDBC types to associate it with. This annotation will be ignored if the jdbcType attribute as also been specified.

例如下面的配置,指定companyid参数类型为BIGINT,

<select id=’getMeetingnoByCompanyid’ parameterType=”java.lang.Integer” resultType=”java.lang.String”> select a.meetingno from xxx a where a.companyid = #{companyid, jdbcType=BIGINT} </select>

对于jdbcType,MyBatis的API文档有说明,引自:http://www.mybatis.org/mybatis-3/apidocs/reference/org/apache/ibatis/type/JdbcType.html

另外,这篇文章,给出了JdbcType和Oracle以及MySQL,相互之间的映射关系,比较详细,引自:http://blog.csdn.net/loongshawn/article/details/50496460

JdbcType

Oracle

MySql

JdbcType

ARRAY

JdbcType

BIGINT

BIGINT

JdbcType

BINARY

JdbcType

BIT

BIT

JdbcType

BLOB

BLOB

BLOB

JdbcType

BOOLEAN

JdbcType

CHAR

CHAR

CHAR

JdbcType

CLOB

CLOB

修改为TEXT

JdbcType

CURSOR

JdbcType

DATE

DATE

DATE

JdbcType

DECIMAL

DECIMAL

DECIMAL

JdbcType

DOUBLE

NUMBER

DOUBLE

JdbcType

FLOAT

FLOAT

FLOAT

JdbcType

INTEGER

INTEGER

INTEGER

JdbcType

LONGVARBINARY

JdbcType

LONGVARCHAR

LONG VARCHAR

JdbcType

NCHAR

NCHAR

JdbcType

NCLOB

NCLOB

JdbcType

NULL

JdbcType

NUMERIC

NUMERIC/NUMBER

NUMERIC/

JdbcType

NVARCHAR

JdbcType

OTHER

JdbcType

REAL

REAL

REAL

JdbcType

SMALLINT

SMALLINT

SMALLINT

JdbcType

STRUCT

JdbcType

TIME

TIME

JdbcType

TIMESTAMP

TIMESTAMP

TIMESTAMP/DATETIME

JdbcType

TINYINT

TINYINT

JdbcType

UNDEFINED

JdbcType

VARBINARY

JdbcType

VARCHAR

VARCHAR

VARCHAR

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125264.html原文链接:https://javaforall.cn

0 人点赞