1,例如,新建项目hiber02,
2,在hiber02的基础上,再新建src文件夹,倒入11个jar,其中包括数据库的驱动,
3,选中这11个jar文件,右键单击,build path只有一个选项,选中之后生成:
4,在src下新建
取名为hiber.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<!-- 连接驱动类 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 连接数据库 -->
<property name="connection.url">jdbc:mysql://localhost:3306/jd</property>
<!-- 连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 连接数据库的用户密码 -->
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--
create
create-drop
update
validate
-->
<property name="hbm2ddl.auto">update</property>
<!-- 引入实体bean的映射文件 -->
<mapping resource="hiber02/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
5,UserTest测试
package hiber02.unit;
import java.sql.Date;
import hiber02.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class UserTest {
@Test
public void test() {
/* // 创建出configuration对象
Configuration config = new Configuration()
.configure("hiber.cfg.xml");
//获取sessionFactory工厂对象
SessionFactory factory = config.buildSessionFactory();
//获取session对象
Session session = factory.openSession();
//获取事务对象
Transaction ts = session.getTransaction();
//开始事务
ts.begin();
//保存数据
session.save(new User("chjx", "男", 29, new Date(System.currentTimeMillis())));
//提交事务
ts.commit();
//释放资源
session.close();
factory.close();*/
// 创建出configuration对象
Configuration config = new Configuration()
.configure("hiber.cfg.xml");
SchemaExport export = new SchemaExport(config);
export.create(true, true);
}
}
6,User.java
package hiber02.domain;
import java.io.Serializable;
import java.sql.Date;
/**
* 创建实体类
* @author zhao
*
*/
public class User implements Serializable{
private Integer id;
private String name;
private String sex;
private Integer age;
private Date birthday;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String sex, Integer age, Date birthday) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
7,User.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hiber02.domain">
<class name="User" table="user" catalog="jd">
<id name="id" column="user_id">
<generator class="native"/>
</id>
<!-- 属性配置 -->
<property name="name" column="user-name" length="50" not-null="true" unique="true"/>
<property name="age" column="user_age"/>
<property name="sex" column="user_sex" length="2"/>
<column name="birthday" column="user_birthday"/>
</class>
</hibernate-mapping>
8,程序结构如下
9,数据库效果
这只是在学习中的总结,还有许多不足之处
10,hibernate的工作原理概述:
1.读取并解析配置文件
2.读取并解析映射信息,创建SessionFactory
3.打开Sesssion
4.创建事务Transation
5.持久化操作
6.提交事务
7.关闭Session
8.关闭SesstionFactory