这篇文章主要讲解了“怎么理解MyBatis中的JdbcType映射”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么理解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文档有说明,引自: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 |
感谢各位的阅读,以上就是“怎么理解MyBatis中的JdbcType映射”的内容了,经过本文的学习后,相信大家对怎么理解MyBatis中的JdbcType映射这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!