1.首先导入所需的jar包

wKiom1SUILLCr-0NAADvmylrdcc243.jpg

2.在src目录下创建hibernate.cfg.xml文件:

<?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.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate0909
</property>
<!-- username -->
<property name="connection.username">root</property>
<!-- password -->
<property name="connection.password">123456</property>
<!-- 
hibernate针对建表的操作
update:如果有表,检查表的结构,如果没有则创建
create人-drop 启动hibernate创建表,结束hibernate删除表
create  每次启动都重新创建表
validate 每次启动都检查表的结构
 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="cn/itcast/hibernate0909/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>

3.创建domain:

package cn.itcast.hibernate0909.domain;
import java.io.Serializable;
public class Person implements Serializable{
private Long pid;
private String pname;
private String psex;
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPsex() {
return psex;
}
public void setPsex(String psex) {
this.psex = psex;
}
}

4.创建相应的Person.hbm.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>
<!-- 
class元素用来描述持久化类
name属性:类的全名
table:该类对应的表名 可以不写,如果不写默认值就是类名
catalog 数据库的名字,可以不写,已经在hibernate.cfg.xml中配置
 -->
<class name="cn.itcast.hibernate0909.domain.Person" table="person"  >
<!-- 
主键
name描述的是属性的名称
column 数据库字段的名称
type 类型
length 长度
 -->
<id name="pid" type="java.lang.Long" length="5">
<column name="pid"></column>
<!-- 
主键的产生器
    increment 由hibernate产生
 -->
<generator class="increment" />
</id>
<!-- 
property 是用来标书一般属性
 -->
<property name="pname" type="java.lang.String" length="20">
<!-- 
column可以不写,如果不写,默认属性的名称
 -->
<column name="pname"></column>
</property>
<property name="psex" type="java.lang.String" length="5">
</property>
</class>
</hibernate-mapping>

5.创建测试类

package cn.itcast.hibernate0909.helloworld;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import cn.itcast.hibernate0909.domain.Person;
public class PersonTest {
@Test
public void testSavePerson(){
Configuration configuration = new Configuration();
//加载配置文件
configuration.configure();
//采用工厂模式创建sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
//由于在映射文件中 已经说明主键的产生方式是hibernate内部产生,所以在程序中不用设置主键
person.setPname("队队员");
person.setPsex("纯爷们");
session.save(person);
transaction.commit();
session.close();
}
}