1、导包
antlr-2.7.6.jar
backport-util-concurrent.jar
c3p0-0.9.1.jar
commons-collections-3.1.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
ehcache-1.5.0.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j.jar
mysql-connector-java-5.1.10-bin.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
2、建domain 文件
/**
* 对象的序列化的作用:让对象在网络上传输,以二进制的形式传输
* Serializable 标识接口
*/
public class Person implements Serializable{
private Long pid;
private String pname;
private String psex;
封装……
}
3、创建 Person.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!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 数据库的名称 一般不写
-->
<class name="cn.google.hibernate.sh.domain.Person">
<!--
id 标示属性 和数据库中的主键对应
name 属性的名称
column 列的名称
-->
<id name="pid" column="pid" length="200" type="java.lang.Long">
<!--
主键的产生器
就该告诉hibernate容器用什么样的方式产生主键
-->
<generator class="increment"></generator>
</id>
<!--
描述一般属性
-->
<property name="pname" column="pname" length="20" type="java.lang.String"></property>
<property name="psex" column="psex" length="10" type="java.lang.String"></property>
</class>
</hibernate-mapping>
4、在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只能连接一个数据库
-->
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property>
<!--
密码
-->
<property name="connection.password">root</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/sh_hibernate
</property>
<!--
作用:根据持久化类和映射文件生成表
validate 只验证,不生成表
create-drop Hibernate启动时生成表,关闭时删除表
create 启动Hibernate容器时生成表
update 启动Hibernate容器时如果表和持久化类不对应,则更改(首选)
-->
<property name="hbm2ddl.auto">update</property>
<!--
显示hibernate内部生成的sql语句
-->
<property name="show_sql">true</property>
<pre name="code" class="html"> <property name="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialec </property>
<!--
加载持久化类的映射文件
-->
<mapping resource="cn/google/hibernate/sh/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
5、在mysql中创建数据库 sh_hibernate
6、建 test 类进行建表测试
public class CreateTable{
@Test
public void testCreateTable(){
Configuration conf = new Configuration(); //配置文件对象
conf.configure(); //加载配置文件
conf.buildSessionFactory(); // 这句会自动在数据库生成表格
}
}
7、测试 添加数据 public class PersonTest{
@Test
public void testSavePerson(){
Configuration conf = new Configuration();
conf.configure();
SessionFactory sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession(); //打开与数据库的链接
Transaction transaction = session.beginTransaction(); //开启事务
Person p = new Person();
p.setPname("上海班长");
p.setPsex("女");
session.save(p); //把数据存数数据库
transaction.commit(); // 事务的提交
session.close(); //关闭与数据库的链接
}
}
8、提取工具类 HibernateUtil
public class HiberanteUtils {
public static SessionFactory sessionFactory;
static{
/**
* 创建configuration对象
*/
Configuration configuration = new Configuration();
configuration.configure();
/**
* 加载 hibernate 的配置文件
*/
sessionFactory = configuration.buildSessionFactory();
}
}
9、优化 测试类
public class PersonTest extends HibernateUtils{
@Test
public void testSavePerson(){
Session session = sessionFactory.openSession(); //打开与数据库的链接
Transaction transaction = session.beginTransaction(); //开启事务
Person p = new Person();
p.setPname("上海班长");
p.setPsex("女");
session.save(p); //把数据存数数据库
transaction.commit(); // 事务的提交
session.close(); //关闭与数据库的链接
}
}