研究了常用的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的更多相关文章

  1. (转)C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)

    一.C# vs SQLite: C# SQLite 字段名 类型 库类型 GetFieldType(#) 转换 备注 F_BOOL bool BIT NOT NULL Boolean F_BOOL_N ...

  2. C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)

    一.C# vs SQLite: C# SQLite 字段名 类型 库类型 GetFieldType(#) 转换 备注 F_BOOL bool BIT NOT NULL Boolean F_BOOL_N ...

  3. Mysql到Java数据类型映射的JDBC规范

  4. hibernate.cfg.xml 配置SQL server,MySQL,Oracle

    1.连接SQL server <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiberna ...

  5. Oracle 数据类型映射C#

    Oracle 数据类型映射 下表列出 Oracle 数据类型及其与 OracleDataReader 的映射. Oracle 数据类型 由 OracleDataReader.GetValue 返回的  ...

  6. 使用Hibernate注解Annotations进行对象映射的异常处理

    通过Hibernate注解Annotations进行对象映射,想在Oracle数据库中自动创建表,代码如下: 实体类: import javax.persistence.Basic;import ja ...

  7. java:Hibernate框架1(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态,增删查改)

    1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件  准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cf ...

  8. Java数据类型与MySql数据类型对照表

    这篇文章主要介绍了Java数据类型与MySql数据类型对照表,以表格形式分析了java与mysql对应数据类型,并简单讲述了数据类型的选择与使用方法,需要的朋友可以参考下 本文讲述了Java数据类型与 ...

  9. Hibernate数据类型映射

    Hibernate映射类型分为两种:内置的映射类型和客户化映射类型.内置映射类型负责把一些常见的Java类型映射到相应的SQL类型:此外,Hibernate还允许用户实现UserType或Compos ...

随机推荐

  1. 《Linux内核分析》第二周笔记 操作系统是如何工作的

    操作系统是如何工作的 一.函数调用堆栈 1.三个法宝 计算机是如何工作的?(总结)——三个法宝(存储程序计算机.函数调用堆栈.中断机制) 1)存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: ...

  2. HDU 2030 汉字统计

    http://acm.hdu.edu.cn/showproblem.php?pid=2030 Problem Description 统计给定文本文件中汉字的个数.   Input 输入文件首先包含一 ...

  3. jsonp 接口

    一.请求接口 <script type="text/javascript" src="js/jquery.min.js"></script&g ...

  4. ABP-JavaScript API

    一.AJAX 1,ABP采用的方式 ASP.NET Boilerplate通过用abp.ajax函数包装AJAX调用来自动执行其中的一些步骤. 一个例子ajax调用: var newPerson = ...

  5. 对象内存空间 在创建对象后 运行时 会把对象的方法放到jvm的方法区中 调用时 将方法拿到栈中 执行完后 这个方法会出栈 然后新的方法方法进栈

  6. BZOJ2768 JLOI2012冠军调查(最小割)

    容易想到网络流.将每个人拆成0和1两个点.若某人值为0的话则让源连向0,否则让1连向汇,流量为1.相互认识的人之间01各自连边.跑最小割即可. #include<iostream> #in ...

  7. 二、spring boot 1.5.4 异常控制

    spring boot 已经做了统一的异常处理,下面看看如何自定义处理异常 1.错误码页面映射 1.1静态页面 必须配置在 resources/static/error文件夹下,以错误码命名 下面是4 ...

  8. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  9. Python之旅:并发编程之协程

    一 引子 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去 ...

  10. 数组初始化 memset fill

    #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #in ...