1. 程式人生 > >Hibernate入門基本部署

Hibernate入門基本部署

name 保存 ava light ges eat connect 目錄 turn

1,建立java工程,導入jar包

  Hibernate依賴jar包,lib/required/*.jar,核心包hibernate3.jar,數據庫驅動包

2,所有jar包的作用

技術分享

3,創建核心配置文件到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>
	<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf-8</property>
	<property name="myeclipse.connection.profile">hibernate1</property>
	<property name="connection.username">root</property>
	<property name="connection.password"></property>
	<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="hibernate.show_sql">true</property>

	<mapping resource="com/lz/javabean/customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

4,創建持久化類Customer.java

package com.lz.javabean;
import java.io.Serializable; @SuppressWarnings("serial") public class Customer implements Serializable{ private Integer id; private String name; private Integer age; private String des; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } public Customer(Integer id, String name, Integer age, String des) { super(); this.id = id; this.name = name; this.age = age; this.des = des; } public Customer() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", des=" + des + "]"; } }

5,創建對應的數據表

create table customer
(
  id              int primary key,
  name        varchar(12),
  age           int,
  des           text
)

6,創建ORM映射配置文件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 package="org.hibernate.tutorial.domain">
	<class name="com.lz.javabean.Customer">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name" column="name"/>
		<property name="age" column="age"/>
		<property name="des" column="des"/>
	</class>
</hibernate-mapping>

7,測試類

package com.lz.test;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.lz.javabean.Customer;
import com.lz.util.SessionFactoryUtil;

public class HibernateTest {
	@Test
	public void addCustomer(){    
                Configuration cfg=new Configuration().configure();
		SessionFactory factory=cfg.buildSessionFactory();
		Session session=factory.openSession();
		session.beginTransaction();
		Customer customer=new Customer(1, "李四", 10, "帥氣");
		session.save(customer);
		session.getTransaction().commit();
		session.close();
	}
}            

成功保存Customer對象

8,其他方法的介紹

get(Customer.class,id);若主鍵id值不存在,返回null,立即執行sql語句

load(Customer.class,id);支持延遲加載,在使用對象時才發出sql語句,使用CGLIB代理,查詢id不存在,拋出對象找不到異常

delete(customer);

update(customer);

Hibernate入門基本部署