Hibernate入門基本部署

來源:互聯網
上載者:User

標籤:使用   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入門基本部署

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.