1.包的介绍
2.HelloWorld
- 建立项目
- 创建hibernate.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>
<!-- 数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hiber01</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 设置数据库连接池的初始化连接数 -->
<property name="connection.pool_size">1</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<!--跟表的映射配置文件-->
<mapping resource="model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 创建实体类
package model;
import java.util.Date;
public class User {
private int userId;
private String uname;
private String gender;
private Date birthday;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
- Object 与 relation的映射 文件名xx.hbm.xml
<
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--tutorial.domain-->
<hibernate-mapping>
<class name="model.User" table="t_user">
<id name="userId" column="user_id">
<generator class="assigned"></generator>
</id>
<property name="uname" column="uname"/>
<property name="gender" column="gender"/>
<property name="birthday" column="birthday"/>
</class>
</hibernate-mapping>
- 生成表
package utils;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class DBExport {
public static void main(String[] args) {
// 创建hebernate配置对象
Configuration cfg = new Configuration();
// 指定hibernate.cfg.xml位置
cfg.configure("hibernate.cfg.xml");
// 创建表的对象
SchemaExport se = new SchemaExport(cfg);
se.create(true, true);
}
}
log:
- insert测试
package test;
import model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import org.hibernate.service.ServiceRegistry;
import java.util.Date;
public class Testhebernate {
@Test
public void test(){
// 创建hibernate配置对象
Configuration cfg = new Configuration();
// 配置核心文件的位置
cfg.configure("hibernate.cfg.xml");
// 注册配置属性信息
ServiceRegistry sr = (ServiceRegistry) new StandardServiceRegistryBuilder().
applySettings(cfg.getProperties()).build();
// 创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory(sr);
// 创建Session会话
Session session = factory.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
// 创建对象
User user = new User();
user.setUserId(1);
user.setUname("你妹");
user.setGender("男");
user.setBirthday(new Date());
// 通过Session保存对象
session.save(user);
// 提交事务
tx.commit();
session.close();
}
}