您的位置:首页 > 其它

Hibernate第一个例子

2014-05-18 17:45 459 查看
本文所有内容都来自精通Hibernate(孙卫琴)一书。

关系数据库学的重要理论之一就是:不要给表的主键赋予任何业务的含义,这样可以提高系统的可维护性。

POJO:plain old java object

业务数据的不同表现形式:

域模型:内存中的实体域对象及其联系

关系数据模型:数据库表及其参照关系

对象持久化方式:

JDBC,ORM(Hibernate,JPA等),主动域对象模式,JDO,CMP。

Hibernate是数据库与java应用之间的桥梁,负责java对象与关系数据之间的映射。

使用Hibernate的步骤:

创建配置文件

创建持久化类

创建数据库表

创建映射文件

通过HibernateAPI编写访问数据库的java代码

在java应用中,当持久化一个对象时不应该依赖Hibernate或数据库进行数据验证,这样会降低效率,应该在表现层或业务逻辑层验证。

第一个Hibernate应用:

配置文件hibernate.properties放在classes根目录下:

hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class=com.microsoft.sqlserver.jdbc.SQLServerDriver
hibernate.connection.url=jdbc:sqlserver://localhost:1433;databasename=hibernate
hibernate.connection.username=sa
hibernate.connection.password=sa
hibernate.show_sql=true


持久化类:

package model;

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;

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;
}

}


编译后放在classes/model下,并在那里添加映射文件Customer.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="model.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"/>

</class>

</hibernate-mapping>


写sql语句并在SQLServer2005数据库中执行:

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'hibernate')
DROP DATABASE hibernate
GO

CREATE DATABASE hibernate  ON
(
NAME = 'hibernate_Data',
FILENAME = 'E:\tools\study\SQLServer\MSSQL10.MSSQLSERVER\MSSQL\DATA\hibernate_Data.MDF' ,
SIZE = 5,
FILEGROWTH = 10%
)
LOG ON
(
NAME = 'hibernate_Log',
FILENAME = 'E:\tools\study\SQLServer\MSSQL10.MSSQLSERVER\MSSQL\DATA\hibernate_Log.LDF' ,
SIZE = 5,
FILEGROWTH = 10%
)
COLLATE Chinese_PRC_CI_AS
GO

use hibernate;

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 image,
BIRTHDAY date
);


通过HibernateAPI编程:

package model;

import javax.servlet.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.io.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;

public class BusinessService{
public static SessionFactory sessionFactory;

/** 初始化Hibernate,创建SessionFactory实例 */
static{
try{
// 根据默认位置的Hibernate配置文件的配置信息,创建一个Configuration实例
Configuration config = new Configuration();
//加载Customer类的对象-关系映射文件
config.addClass(Customer.class);
/* 创建SessionFactory实例,它代表一个数据源,将之前Configuration实例中的配置信息复制给它,
之后Configuration实例和它就没有关系了,可以用于创建另外的SessionFactory实例*/
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
}

/** 查询所有的Customer对象,然后调用printCustomer()方法打印Customer对象信息 */
public void findAllCustomers(ServletContext context,PrintWriter out) throws Exception{
Session session = sessionFactory.openSession(); //创建一个会话
Transaction tx = null;
try {
tx = session.beginTransaction(); //开始一个事务
Query query=session.createQuery("from Customer as c order by c.name asc");
List customers=query.list();
for (Iterator it = customers.iterator(); it.hasNext();) {
printCustomer(context,out,(Customer) it.next());
}

tx.commit(); //提交事务

}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}

/** 持久化一个Customer对象 */
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();
}
}

/** 按照OID加载一个Customer对象,然后修改它的属性 */
public void loadAndUpdateCustomer(Long customer_id,String address){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();

Customer c=(Customer)session.get(Customer.class,customer_id);
c.setAddress(address);
tx.commit();

}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}

/**删除Customer对象 */
public void deleteCustomer(Customer customer){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(customer);
tx.commit();

}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}

/** 选择向控制台还是Web网页输出Customer对象的信息 */
private void printCustomer(ServletContext context,PrintWriter out,Customer customer)throws Exception{
if(context!=null)
printCustomerInWeb(context,out,customer);
else
printCustomer( out,customer);
}

/** 把Customer对象的信息输出到控制台,如DOS 控制台*/
private void printCustomer(PrintWriter out,Customer customer)throws Exception{
byte[] buffer=customer.getImage();
FileOutputStream fout=new FileOutputStream("photo_copy.gif");
fout.write(buffer);
fout.close();

out.println("------以下是"+customer.getName()+"的个人信息------");
out.println("ID: "+customer.getId());
out.println("口令: "+customer.getPassword());
out.println("E-Mail: "+customer.getEmail());
out.println("电话: "+customer.getPhone());
out.println("地址: "+customer.getAddress());
String sex=customer.getSex()=='M'? "男":"女";
out.println("性别: "+sex);
String marriedStatus=customer.isMarried()? "已婚":"未婚";
out.println("婚姻状况: "+marriedStatus);
out.println("生日: "+customer.getBirthday());
out.println("自我介绍: "+customer.getDescription());

}

/** 把Customer对象的信息输出到动态网页 */
private void printCustomerInWeb(ServletContext context,PrintWriter out,Customer customer)throws Exception{
//保存照片
byte[] buffer=customer.getImage();
String path=context.getRealPath("/");
FileOutputStream fout=new FileOutputStream(path+"photo_copy.gif");
fout.write(buffer);
fout.close();

out.println("------以下是"+customer.getName()+"的个人信息------"+"<br>");
out.println("ID: "+customer.getId()+"<br>");
out.println("口令: "+customer.getPassword()+"<br>");
out.println("E-Mail: "+customer.getEmail()+"<br>");
out.println("电话: "+customer.getPhone()+"<br>");
out.println("地址: "+customer.getAddress()+"<br>");
String sex=customer.getSex()=='M'? "男":"女";
out.println("性别: "+sex+"<br>");
String marriedStatus=customer.isMarried()? "已婚":"未婚";
out.println("婚姻状况: "+marriedStatus+"<br>");
out.println("生日: "+customer.getBirthday()+"<br>");
out.println("自我介绍: "+customer.getDescription()+"<br>");
out.println("<img src='photo_copy.gif' border=0><p>");
}
public void test(ServletContext context,PrintWriter out) throws Exception{

Customer customer=new Customer();
customer.setName("Tom");
customer.setEmail("tom@yahoo.com");
customer.setPassword("1234");
customer.setPhone(55556666);
customer.setAddress("Shanghai");
customer.setSex('M');
customer.setDescription("I am very honest.");

//设置Customer对象的image属性,它是字节数组,存放photo.gif文件中的二进制数据
//photo.gif文件和BusinessService.class文件位于同一个目录下
InputStream in=this.getClass().getResourceAsStream("photo.gif");
byte[] buffer = new byte[in.available()];
in.read(buffer);
customer.setImage(buffer);
//设置Customer对象的birthday属性,它是java.sql.Date类型
customer.setBirthday(Date.valueOf("1980-05-06"));

saveCustomer(customer);

findAllCustomers(context,out);
loadAndUpdateCustomer(customer.getId(),"Beijing");
findAllCustomers(context,out);
deleteCustomer(customer);
}

public static void main(String args[]) throws Exception {
new BusinessService().test(null,new PrintWriter(System.out,true));
sessionFactory.close();
}
}


完成,执行main()即可。当然,要一些jar包,我把整个工程都上传,jar包都在lib下了:Hibernate例子的代码和jar包

也可以在web应用中执行,hello.jsp:

<html locale="true">
<head>
<title>hello.jsp</title>
</head>
<body bgcolor="white">
<jsp:forward page="db" />
</body>
</html>


web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">

<servlet>
<servlet-name>DBServlet</servlet-name>
<servlet-class>model.DBServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>DBServlet</servlet-name>
<url-pattern>/db</url-pattern>
</servlet-mapping>

</web-app>


DBServlet:

package model;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class DBServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
try{
response.setContentType("text/html;charset=GBK");
new BusinessService().test(this.getServletContext(),response.getWriter());
}catch(Exception e){e.printStackTrace();}
}
}


启动tomcat访问hello.jsp即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: