【SSH系列】---Hibernate的基本映射
2016-06-14
首先,什么是映射
对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。从效果上说,它其实是创建了一个可在编程语言里使用的“虚拟对象数据库”。它的作用就是在关系型数据库和对象之间做了一个映射。从对象(Object)映射到关系(Relation),再从关系映射到对象。Hibernate的映射分类,小编简单的画了一张思维导图,如下所示:
接着,小编结合deom来具体的讲解一下基本映射。
小试牛刀
第一步、我们使用XML配置映射代码如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bjpowernode.hibernate">
<class name="User3" table="t_user3">
<id name="id" >
<generator class="assigned"/>
</id>
<property name="name" length="30" unique="true" not-null="true"/>
<property name="password"/>
<property name="expireTime" column="expireTime" not-null="false" type="date"/>
<property name="createTime" column="createTime" not-null="false" type="date"/>
</class>
</hibernate-mapping>
第二步、建立实体类User3,代码如下所示:package com.bjpowernode.hibernate;
import java.util.Date;
public class User3 {
private int id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
第三步、编写hibernate.cfg.xml文件,将实体类User加入到hibernate.cfg.xml配置文件中,完成基本配置,代码如下所示:<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<!-- MySql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库名称 -->
<property name="hibernate.connection.url"> jdbc:mysql:///hibernate_basemapping</property>
<!-- 数据库的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的密码 -->
<property name="hibernate.connection.password">123456</property>
<!-- 方言:为每一种数据库提供适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 显示语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式排版 -->
<!-- <property name="hibernate.format_sql">true</property> -->
<mapping resource="com/bjpowernode/hibernate/User3.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第四步、编写工具类ExportDB.java,代码如下所示:package com.bjpowernode.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author 丁国华
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
第五步、编写客户端Client.java,添加用户数据到数据库,代码如下所示:
package com.bjpowernode.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args){
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//取得session
Session session = null;
try{
session = factory.openSession();
//开启事物
session.beginTransaction();
User3 user = new User3();
user.setId(1000);
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存user对象
session.save(user);
//提交事物
session.getTransaction().commit();
}catch (Exception e){
e.printStackTrace();
//回滚事物
session.getTransaction().rollback();
}finally{
if (session != null){
if(session.isOpen()){
//关闭session
session.close();
}
}
}
}
}
最后,来运行一下,看看我们的数据库,效果如下所示:
小编寄语:该博文,小编主要介绍了hibernate中的基本映射,分别从什么是映射、ORM的实现原理、映射的分类、配上demo进行讲解,希望可以帮助有需要的小伙伴们,有不同意见的小伙伴欢迎在下方留言,在后续SSH系列博文中,小编将继续介绍hibernate的相关知识,敬请期待`(*∩_∩*)′。