一、 Hibernate介绍
Hibernate是基于对象/关系映射(ORM,Object/Relational Mapping)的一个解决方案。ORM方案的思想是将对象模型表示的对象映射到关系型数据库中,或者反之。Hibernate目前是ORM思想在Java中最成功、最强大的实现。它于2001年的年末发布第一个版本,立即引起了广泛的注意。2003年6月,Hibernate2发表,并且获得Jolt大奖,进而被JBoss吸纳成为它的一个子项目。2005年3月,Hibernate 3发表,其中做了一些比较重大的改进。本文以Hibernate3为基础编写。
另外,Hibernate除了可以在J2EE容器中运行外,还可以运行在Java应用程序中。本文就是以Java应用程序为例来介绍它。
二。编写代码
编写大配置文件(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>
<!-- 设置数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 设置数据库URL -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedem</property>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password"></property>
<!-- 指定对应数据库的方言,hibernate为了更好适配各种关系数据库,针对每种数据库都指定了一个方言dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射文件 -->
<mapping resource="bdqn/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
编写实体类(User)
package bdqn.entity;
import java.util.Date;
/**
* Created by guo on 2017/9/16.
*/
public class User {
private Integer id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
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 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;
}
}
编写小配置文件(User.hbm.xml文件)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernaate.net/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="bdqn.entity">
<class name="User">
<id name="id">
<!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID -->
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="password" column="password"></property>
<property name="createTime" type="date" column="createTime"></property>
<property name="expireTime" type="date" column="expireTime"></property>
</class>
</hibernate-mapping>
编写测试类ExportDB
import bdqn.entity.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.List;
/**
* Created by guo on 2017/9/16.
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure("Hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
//查询
Query query = session.createQuery("from User");
List list = query.list();
System.out.println(list.size());
//添加
/*User user=new User();
user.setPassword("hahas");
user.setName("hahas");
session.save(user);
session.beginTransaction().commit();*/
//删除
/* User user=new User();
user.setId(2);
session.delete(user);
session.beginTransaction().commit();*/
//修改
/* User user = session.load(User.class, 3);
user.setName("guohua");
session.update(user);
session.beginTransaction().commit();*/
System.out.println("success!!!!!");
}
}