Hibernate常用的Java数据类型映射到mysql和Oracle

2023-06-15,,

  研究了常用的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的相关教程结束。

《Hibernate常用的Java数据类型映射到mysql和Oracle.doc》

下载本文的Word格式文档,以方便收藏与打印。