1、环境配置
(1)在myeclipse下新建一普通Java Project。
(2)新建lib文件夹,并将hibernate和mysql所需jar包导入lib文件夹,并build path。
2、在mysql数据库中建表
sql语句如下:
use test;
create table employee
(id int NOT NULL auto_increment,
name VARCHAR(10),
email varchar(20),
PRIMARY KEY(id)
)charset=utf8;
3、创建持久化类(POJO)
在com.sdust.www.domain包下创建Employee类
package com.sdust.www.domain;
public class Employee
{
private Integer id;
private String name;
private String email;
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 getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Employee(String name, String email)
{
super();
this.name = name;
this.email = email;
}
public Employee()
{}
@Override
public String toString()
{
return "Employee [id=" + id + ", name=" + name + ", email=" + email
+ "]";
}
}
4、创建对象关系映射文件
在com.sdust.www.domain包下创建Employee.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sdust.www.domain">
<class name="Employee" table="employee">
<!-- id用于指定主键属性 -->
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<!-- 其它属性的配置 -->
<property name="name" type="java.lang.String">
<column name="name" not-null="false"></column>
</property>
<property name="email" type="java.lang.String">
<column name="email" not-null="false"></column>
</property>
</class>
</hibernate-mapping>
5、创建Hibernate配置文件
在src目录下创建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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<!--<property name="connection.pool_size">1</property>-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/sdust/www/domain/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、通过Hibernate API编写访问数据库代码
package com.sdust.www.view;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.sdust.www.domain.Employee;
public class TestMain
{
public static void main(String[] args)
{
//1、创建SessionFactory对象
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2、创建session对象(相当于jdbc connection)
Session session = sessionFactory.openSession();
//3、开启事务
Transaction transaction = session.beginTransaction();
//4、执行保存操作
Employee employee = new Employee();
employee.setName("whc");
employee.setEmail("whc@163.com");
session.save(employee);
//5、提交事务
transaction.commit();
//6、关闭session
session.close();
//7、关闭sessionFactory对象
sessionFactory.close();
}
}
7、查看结果
程序运行完成后,可在mysql可看到employee表中增加了一条记录。