一、常用的Hibernat映射类型有如下几种:
- string
- integer
- double
- date 日期,只表示年月日
- datetime 日期,只表示年月日
- timestamp 时间戳,存放年月日时分秒
- yes_no 将在数据库中存放一个字符“Y"或“N”
- true_false 将在数据库中存放一个字符“T"或者"F".
二、案例
1、新建数据库表
DROP TABLE IF EXISTS t_emp; CREATE TABLE t_emp ( t_id int(11) NOT NULL AUTO_INCREMENT, t_name varchar(50) NOT NULL, t_salary double(9,2) NOT NULL, t_hire_date date NOT NULL, t_last_login timestamp NOT NULL, t_register char(1) NOT NULL, PRIMARY KEY (t_id) ) ENGINE=InnoDB;varchar对应String
date对应年月日
timestamp对应年月日时分秒
通常情况下,使用char(1),值为true/false,来表示Boolean类型。
2、新建POJO类Emp
package com.xsyu.tts.po; import java.util.Date; public class Emp { private Integer id; private String name; private double salary; private Date hireDate; // 入职时间 private Date lastLogin; // 最后登录日期 private boolean register; // 是否注册 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public Date getLastLogin() { return lastLogin; } public void setLastLogin(Date lastLogin) { this.lastLogin = lastLogin; } public boolean isRegister() { return register; } public void setRegister(boolean register) { this.register = register; } }3、新建Emp.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tarena.tts.po"> <class name="Emp" table="t_emp"> <id name="id" type="integer" column="t_id"> <!-- 用来指明主键的生成方式 --> <generator class="identity"> </generator> </id> <property name="name" type="string" column="t_name" /> <property name="salary" type="double" column="t_salary" /> <property name="hireDate" type="date" column="t_hire_date" /> <property name="lastLogin" type="timestamp" column="t_last_login" /> <property name="register" type="yes_no" column="t_register" /> </class> </hibernate-mapping>4、修改配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接信息 --> <property name="connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- Hibernate配置信息 --> <!-- dialect方言,用亍配置生成针对哪个数据库的SQL语句 --> <property name="dialect"> <!--方言类,Hibernate提供的,用亍封装某种特定数据库的方言 --> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.show_sql">true</property> <!-- 在配置文件中关联映射文件 --> <mapping resource="com/xsyu/tts/po/User.hbm.xml" /> <mapping resource="com/xsyu/tts/po/Foo.hbm.xml" /> <mapping resource="com/xsyu/tts/po/Emp.hbm.xml" /> </session-factory> </hibernate-configuration>5、修改TestPersistence
/**
* 用于测试Hibernate映射类型 string interger double date timestamp yes_no true_false
*/
@Test
public void testType() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Emp emp = new Emp();
emp.setName("BigYellow");
emp.setSalary(15000.90);
emp.setHireDate(new Date());
emp.setLastLogin(new Date());
emp.setRegister(true);
session.save(emp);
tx.commit();
session.close();
}