標籤:使用 nbsp type jdb javabean doc oid arch primary
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}@Overridepublic 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 {@Testpublic 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入門基本部署