Hibernate常用的Java数据类型映射到mysql和Oracle
研究了常用的Java基本数据类型在mysql和oracle数据库的映射类型。这里使用的是包装类型做研究,一般在hibernate声明的时候最好不要用基本类型,因为数据库中的null空数据有可能映射为基本类型的时候会报错,但是映射到包装类型的时候值为null,不会报错。
1.常见数据类型在Mysql数据库的映射
实体:
package cn.qlq.domain; import java.sql.Time;
import java.util.Date; public class TestType { private Long id;
private Integer age;
private Character sex;
private Boolean isPerson;
private Date birth;
private Time birthTime;
private String name; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Character getSex() {
return sex;
} public void setSex(Character sex) {
this.sex = sex;
} public Boolean getIsPerson() {
return isPerson;
} public void setIsPerson(Boolean isPerson) {
this.isPerson = isPerson;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} public Time getBirthTime() {
return birthTime;
} public void setBirthTime(Time birthTime) {
this.birthTime = birthTime;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "TestType [id=" + id + ", age=" + age + ", sex=" + sex + ", isPerson=" + isPerson + ", birth=" + birth
+ ", birthTime=" + birthTime + ", name=" + name + "]";
} }
xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 配置表与实体对象的关系 -->
<!-- package属性:填写一个包名.在元素内部凡是需要书写完整类名的属性,可以直接写简答类名了. -->
<hibernate-mapping package="cn.qlq.domain" >
<!--
class元素: 配置实体与表的对应关系的
name: 完整类名
table:数据库表名
-->
<class name="TestType" table="testtype" >
<!-- id元素:配置主键映射的属性
name: 填写主键对应属性名
column(可选): 填写表中的主键列名.默认值:列名会默认使用属性名
type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
每个类型有三种填法: java类型|hibernate类型|数据库类型
not-null(可选):配置该属性(列)是否不能为空. 默认值:false
length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
-->
<id name="id" >
<!-- generator:主键生成策略 -->
<!--identity : 主键自增.由数据库来维护主键值.录入时不需要指定主键. -->
<generator class="native"></generator>
</id>
<!-- property元素:除id之外的普通属性映射
name: 填写属性名
column(可选): 填写列名
type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
每个类型有三种填法: java类型|hibernate类型|数据库类型
not-null(可选):配置该属性(列)是否不能为空. 默认值:false
length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
-->
<property name="age"/>
<property name="sex"/>
<property name="isPerson"/>
<property name="birth"/>
<property name="birthTime"/>
<property name="name"/>
</class>
</hibernate-mapping>
Mysql映射的类型:
mysql> desc testtype;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| age | int(11) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| isPerson | bit(1) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| birthTime | time | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
结果:
Long------------------------------bigint
Integer----------------------- int
Character--------------------- char
Bolean---------------------------bit
java.util.Date;--------------datetime
java.sql.Time;------------------time
String----------------------------varchar(255)
- 插入数据:
public static void main(String[] args) {
//3.3以及之前的版本构建会话工厂对象
// SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
//5.0之后获取SessionFactory
//创建服务注册对象
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//创建会话工厂对象
SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
//获取session对象
Session session = sessionFactory.openSession();
//开启事务
Transaction tx = session.beginTransaction();
//保存对象
TestType t1 = new TestType();
TestType t2 = new TestType();
t1.setAge(22);
t2.setAge(23);
t1.setName("zhangsan");
t2.setName("zhangsanhaha");
t1.setSex('1');
t2.setSex('2');
t1.setBirth(new Date());
t2.setBirth(new Date());
t1.setIsPerson(true);
t2.setIsPerson(false);
session.save(t1);
session.save(t2);
tx.commit();
//关闭流
session.close();
sessionFactory.close();
}
结果:
mysql> select * from testtype;
+----+------+------+----------+---------------------+-----------+--------------+
| id | age | sex | isPerson | birth | birthTime | name |
+----+------+------+----------+---------------------+-----------+--------------+
| 1 | 22 | 1 | | 2018-08-10 22:39:19 | NULL | zhangsan |
| 2 | 23 | 2 | | 2018-08-10 22:39:19 | NULL | zhangsanhaha |
+----+------+------+----------+---------------------+-----------+--------------+
2 rows in set (0.00 sec)
- 查询
(1)基本查询:
@Test
// HQL查询所有数据
public void fun1() {
// 1 获得session
Session session = HibernateUtil.openSession();
// 2.书写HQL语句
// String hql = "from cn.qlq.domain.Customer";// from 类名全路径
String hql = "from TestType";// 如果整个项目中只有这一个类名可以直接写名字
// 3.根据hql创建查询对象
Query query = session.createQuery(hql);
// 4.根据查询对象获取查询结果
List<TestType> list = query.list();
System.out.println(list);
}
结果:
[TestType [id=1, age=22, sex=1, isPerson=true, birth=2018-08-10 22:39:19.0, birthTime=null, name=zhangsan], TestType [id=2, age=23, sex=2, isPerson=false, birth=2018-08-10 22:39:19.0, birthTime=null, name=zhangsanhaha]]
(2)条件查询:----针对上面的类型进行条件查询
@Test
// HQL查询所有数据
public void fun1() {
// 1 获得session
Session session = HibernateUtil.openSession();
// 2.书写HQL语句
// String hql = "from cn.qlq.domain.Customer";// from 类名全路径
String hql = "from TestType where age = 22 and sex = 1 and isPerson = true and name = 'zhangsan' and birth like '2018-08-10%'";// 如果整个项目中只有这一个类名可以直接写名字
// 3.根据hql创建查询对象
Query query = session.createQuery(hql);
// 4.根据查询对象获取查询结果
List<TestType> list = query.list();
System.out.println(list);
}
结果:
select
testtype0_.id as id1_0_,
testtype0_.age as age2_0_,
testtype0_.sex as sex3_0_,
testtype0_.isPerson as isPerson4_0_,
testtype0_.birth as birth5_0_,
testtype0_.birthTime as birthTim6_0_,
testtype0_.name as name7_0_
from
testtype testtype0_
where
testtype0_.age=22
and testtype0_.sex=1
and testtype0_.isPerson=1
and testtype0_.name='zhangsan'
and (
testtype0_.birth like '2018-08-10%'
)
[TestType [id=1, age=22, sex=1, isPerson=true, birth=2018-08-10 22:39:19.0, birthTime=null, name=zhangsan]]
补充:Mysql的boolean类型也可以用true_false表示,数据类型会变为char(1),存的是T和F:
<property name="isPerson" type="true_false"/>

2.常见数据类型在Oracle数据库的映射
Oracle映射上面直接映射会报错,解决办法: 将boolean映射为hibernate的true_false (原理都是在数据库存T或者F,F为false,T为true)
第一种: boolean映射为yes_no
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 配置表与实体对象的关系 -->
<!-- package属性:填写一个包名.在元素内部凡是需要书写完整类名的属性,可以直接写简答类名了. -->
<hibernate-mapping package="cn.qlq.domain" >
<!--
class元素: 配置实体与表的对应关系的
name: 完整类名
table:数据库表名
-->
<class name="TestType" table="testtype" >
<!-- id元素:配置主键映射的属性
name: 填写主键对应属性名
column(可选): 填写表中的主键列名.默认值:列名会默认使用属性名
type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
每个类型有三种填法: java类型|hibernate类型|数据库类型
not-null(可选):配置该属性(列)是否不能为空. 默认值:false
length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
-->
<id name="id" >
<!-- generator:主键生成策略 -->
<!--identity : 主键自增.由数据库来维护主键值.录入时不需要指定主键. -->
<generator class="native"></generator>
</id>
<!-- property元素:除id之外的普通属性映射
name: 填写属性名
column(可选): 填写列名
type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
每个类型有三种填法: java类型|hibernate类型|数据库类型
not-null(可选):配置该属性(列)是否不能为空. 默认值:false
length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
-->
<property name="age"/>
<property name="sex"/>
<property name="isPerson" type="true_false"/>
<property name="birth"/>
<property name="birthTime"/>
<property name="name"/>
</class>
</hibernate-mapping>
结果:

总结:
Long------------------------------number
Integer----------------------- number
Character--------------------- char
Bolean---------------------------char
java.util.Date;--------------date
java.sql.Time;------------------date
String----------------------------varchar(255)
添加数据:
package cn.qlq.util; import java.util.Date; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry; import cn.qlq.domain.TestType; public class TestSave { public static void main(String[] args) {
//3.3以及之前的版本构建会话工厂对象
// SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //5.0之后获取SessionFactory
//创建服务注册对象
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//创建会话工厂对象
SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); //获取session对象
Session session = sessionFactory.openSession();
//开启事务
Transaction tx = session.beginTransaction();
//保存对象
TestType t1 = new TestType();
TestType t2 = new TestType();
t1.setAge(22);
t2.setAge(23); t1.setName("zhangsan");
t2.setName("zhangsanhaha"); t1.setSex('1');
t2.setSex('2'); t1.setBirth(new Date());
t2.setBirth(new Date()); t1.setIsPerson(true);
t2.setIsPerson(false); session.save(t1);
session.save(t2);
tx.commit();
//关闭流
session.close();
sessionFactory.close();
} }
结果:

- 查询所有:
@Test
// HQL查询所有数据
public void fun1() {
// 1 获得session
Session session = HibernateUtil.openSession();
// 2.书写HQL语句
String hql = "from cn.qlq.domain.TestType";// from 类名全路径// 3.根据hql创建查询对象
Query query = session.createQuery(hql);
// 4.根据查询对象获取查询结果
List<TestType> list = query.list();
System.out.println(list);
}
结果:
Hibernate:
select
testtype0_.id as id1_0_,
testtype0_.age as age2_0_,
testtype0_.sex as sex3_0_,
testtype0_.isPerson as isPerson4_0_,
testtype0_.birth as birth5_0_,
testtype0_.birthTime as birthTim6_0_,
testtype0_.name as name7_0_
from
testtype testtype0_
[TestType [id=15, age=22, sex=1, isPerson=true, birth=2018-08-10 23:19:33.0, birthTime=null, name=zhangsan], TestType [id=16, age=23, sex=2, isPerson=false, birth=2018-08-10 23:19:33.0, birthTime=null, name=zhangsanhaha]]
- 按上面的条件查询:
@Test
// HQL查询所有数据
public void fun1() {
// 1 获得session
Session session = HibernateUtil.openSession();
// 2.书写HQL语句
String hql = "from TestType where age = 22 and sex = 1 and isPerson = true and name = 'zhangsan'";// 如果整个项目中只有这一个类名可以直接写名字
// 3.根据hql创建查询对象
Query query = session.createQuery(hql);
// 4.根据查询对象获取查询结果
List<TestType> list = query.list();
System.out.println(list);
}
结果: (日期不能直接like了)
Hibernate:
select
testtype0_.id as id1_0_,
testtype0_.age as age2_0_,
testtype0_.sex as sex3_0_,
testtype0_.isPerson as isPerson4_0_,
testtype0_.birth as birth5_0_,
testtype0_.birthTime as birthTim6_0_,
testtype0_.name as name7_0_
from
testtype testtype0_
where
testtype0_.age=22
and testtype0_.sex=1
and testtype0_.isPerson='T'
and testtype0_.name='zhangsan'
[TestType [id=15, age=22, sex=1, isPerson=true, birth=2018-08-10 23:19:33.0, birthTime=null, name=zhangsan]]
总结:
对于mysql和oracle的boolean的通用类型就是true_false,hibernate会将字段类型设置为char(1),然后true的时候存T,false的时候存F。

Hibernate常用的Java数据类型映射到mysql和Oracle的更多相关文章
- (转)C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
一.C# vs SQLite: C# SQLite 字段名 类型 库类型 GetFieldType(#) 转换 备注 F_BOOL bool BIT NOT NULL Boolean F_BOOL_N ...
- C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
一.C# vs SQLite: C# SQLite 字段名 类型 库类型 GetFieldType(#) 转换 备注 F_BOOL bool BIT NOT NULL Boolean F_BOOL_N ...
- Mysql到Java数据类型映射的JDBC规范
- hibernate.cfg.xml 配置SQL server,MySQL,Oracle
1.连接SQL server <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiberna ...
- Oracle 数据类型映射C#
Oracle 数据类型映射 下表列出 Oracle 数据类型及其与 OracleDataReader 的映射. Oracle 数据类型 由 OracleDataReader.GetValue 返回的 ...
- 使用Hibernate注解Annotations进行对象映射的异常处理
通过Hibernate注解Annotations进行对象映射,想在Oracle数据库中自动创建表,代码如下: 实体类: import javax.persistence.Basic;import ja ...
- java:Hibernate框架1(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态,增删查改)
1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件 准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cf ...
- Java数据类型与MySql数据类型对照表
这篇文章主要介绍了Java数据类型与MySql数据类型对照表,以表格形式分析了java与mysql对应数据类型,并简单讲述了数据类型的选择与使用方法,需要的朋友可以参考下 本文讲述了Java数据类型与 ...
- Hibernate数据类型映射
Hibernate映射类型分为两种:内置的映射类型和客户化映射类型.内置映射类型负责把一些常见的Java类型映射到相应的SQL类型:此外,Hibernate还允许用户实现UserType或Compos ...
随机推荐
- Scrum立会报告+燃尽图(十二月十一日总第四十二次):贡献分配和收集用户报告
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2484 项目地址:https://git.coding.net/zhang ...
- 《Linux内核分析》第五周学习总结 扒开系统调用的三层皮(下)
扒开系统调用的三层皮(下) 郝智宇 无转载 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.给Men ...
- 调研ios开发环境的演变
一:ios的发展演变: 以下两句为百度百科IOS,可自行查阅,不多赘述,就Ctrl+c,Ctrl+v两句表示一下. 2007年1月9日苹果公司在Macworld展览会上公布,随后于同年的6月发布第一版 ...
- Linux用户管理简介
Linux用户管理是Linux的优良特性之一,本文说明了Linux中用户的登录过程和登录用户的类型. 一.Linux用户登录过程 用户要使用Linux系统,必须先进行登录.Linux的登录过程和win ...
- [财务知识] debt debit credit 的区别于联系
https://blog.csdn.net/sjpljr/article/details/70169303 剑桥词典解释分别为: Debt [C or U ] n.something, especia ...
- USACO 2012 December ZQUOJ 24128 Wifi Setup(动态dp)
题意:给出在同一条直线上的n个点和两个数A,B,现在要在这条直线上放置若干个信号塔,每个信号塔有一个r值,假设它的位置是x,则它能覆盖的范围是x-r~x+r,放置一个信号塔的花费是A+B*r,问要覆盖 ...
- CopyOnWriteArrayList、CopyOnWriteArraySet、ConcurrentHashMap的实现原理和适用场景
ConcurrentHashMap代替同步的Map(Collections.synchronized(new HashMap())),众所周知,HashMap是根据散列值分段存储的,同步Map在同步的 ...
- 深入理解Java反射+动态代理
答: 反射机制的定义: 是在运行状态中,对于任意的一个类,都能够知道这个类的所有属性和方法,对任意一个对象都能够通过反射机制调用一个类的任意方法,这种动态获取类信息及动态调用类对象方法的功能称为j ...
- REQUIRES_NEW 如果不在一个事务那么自己创建一个事务 如果在一个事务中 自己在这个大事务里面在创建一个子事务 相当于嵌套事务 双层循环那种
REQUIRES_NEW 如果不在一个事务那么自己创建一个事务 如果在一个事务中 自己在这个大事务里面在创建一个子事务 相当于嵌套事务 双层循环那种 不管是否存在事务,业务方法总会自己开启一个事 ...
- Redis4.0新特性之-大KEY删除
接上一篇,我们得知了redis中存在大KEY,那么这个大KEY如何删除呢?本文将从源码角度分析Redis4.0带来的新特性. 在Redis中,对于大KEY的删除一直是个比较头疼的问题,为了不影响服务, ...