创建Hibernate应用
创建WEB-INF/classes/Hibernate.properties
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
1234
</property>
<property name="show_sql">true</property>
<mapping resource="mypack/Customer.hbm.xml" />
<mapping resource="mypack/Order.hbm.xml" />
<mapping resource="mypack/Dictionary.hbm.xml" />
</session-factory>
</hibernate-configuration>
创建持久化类
把表字段封装进该类,getXX(),setXX(),
package mypack;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;
public class Customer implements Serializable {
private Long id;
private String name;
private String email;
private String password;
private int phone;
private String address;
private char sex;
private boolean married;
private String description;
private byte[] image;
private Date birthday;
private Timestamp registeredTime;
public Customer(){}
public Long getId(){
return id;
}
private void setId(Long 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 String getPassword(){
return password;
}
public void setPassword(String password){
this.password =password ;
}
public int getPhone(){
return phone;
}
public void setPhone(int phone){
this.phone =phone ;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address =address ;
}
public char getSex(){
return sex;
}
public void setSex(char sex){
this.sex =sex ;
}
public boolean isMarried(){
return married;
}
public void setMarried(boolean married){
this.married =married ;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description =description ;
}
public byte[] getImage() {
return this.image;
}
public void setImage(byte[] image) {
this.image = image;
}
public Date getBirthday() {
return this.birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Timestamp getRegisteredTime() {
return this.registeredTime;
}
public void setRegisteredTime(Timestamp registeredTime) {
this.registeredTime = registeredTime;
}
}
创建数据库及表
drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB;
create table CUSTOMERS (
ID bigint not null primary key,
NAME varchar(15) not null,
EMAIL varchar(128) not null,
PASSWORD varchar(8) not null,
PHONE int ,
ADDRESS varchar(255),
SEX char(1) ,
IS_MARRIED bit,
DESCRIPTION text,
IMAGE blob,
BIRTHDAY date,
REGISTERED_TIME timestamp
);
创建对象-关系映射文件
Customer.hbm.xml,放在Customer类的旁边
<?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="mypack.Customer" table="CUSTOMERS">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="string" not-null="true" />
<property name="email" column="EMAIL" type="string" not-null="true" />
<property name="password" column="PASSWORD" type="string" not-null="true"/>
<property name="phone" column="PHONE" type="int" />
<property name="address" column="ADDRESS" type="string" />
<property name="sex" column="SEX" type="character"/>
<property name="married" column="IS_MARRIED" type="boolean"/>
<property name="description" column="DESCRIPTION" type="text"/>
<property name="image" column="IMAGE" type="binary"/>
<property name="birthday" column="BIRTHDAY" type="date"/>
<property name="registeredTime" column="REGISTERED_TIME" type="timestamp"/>
</class>
</hibernate-mapping>
Hibernate API来执行数据库语句
public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration()
.setNamingStrategy( new MyNamingStrategy() )
.configure(); //加载hibernate.cfg.xml文件中配置的信息
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
}
public Customer loadCustomer(long customer_id){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Customer customer=(Customer)session.get(Customer.class,new Long(customer_id));
tx.commit();
return customer;
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void saveCustomer(Customer customer){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(customer);
tx.commit();
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void loadAndUpdateCustomer(long customerId) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Customer customer=(Customer)session.get(Customer.class,new Long(customerId));
customer.setDescription("A lovely customer!");
tx.commit();
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void updateCustomer(Customer customer){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(customer);
tx.commit();
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void saveDictionary(Dictionary dictionary) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(dictionary);
tx.commit();
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}