MyBatis中的JdbcType映射使用介绍
作者:bisal(Chen Liu)
MyBatis JdbcType映射介绍
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文档有说明
另外,这篇文章,给出了JdbcType和Oracle以及MySQL
相互之间的映射关系,比较详细,引自:https://www.jb51.net/article/195686.htm
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 |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。