1. 程式人生 > >Hibernate單表映射學習筆記之一——hibernalnate開發環境配置

Hibernate單表映射學習筆記之一——hibernalnate開發環境配置

pass ransac over 構造方法 參數 會話 signed rate ets

  1、什麽是ORM?

  Object/Relationship Mapping:對象/關系映射

  2、寫SQL語句不好之處:

  (1)不同數據庫使用的SQL語法不同(PL/SQL、T/SQL)

  (2)同樣的功能在不同的數據庫中有不同的實現方式(分頁SQL)

  (3)過分依賴SQL語句對程序的移植和拓展不利

  3、Hibernate

  (1)ORM框架技術

  (2)對JDBC進行了非常輕量的對象封裝

 技術分享

  4、其他ORM框架技術

  (1)Mybatis(前身為iBatis)

  (2)Toplink(現為Oracle As Toplink)

  (3)EJB:本身就是JAVAEE規範

  5、所需工具:

  (1) Hibernate 核心包;

  (2) Hibernate eclipse plugin;

  6、創建Hibernate項目步驟:

  (1)導入核心包及數據庫驅動:

  技術分享

  (2)創建配置文件;

  技術分享

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"
> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property
> <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="Student.hbm.xml"/> </session-factory> </hibernate-configuration>

  (3)創建持久化類;

package hibernate001;
//學生類

import java.util.Date;
public class Student{
//持久化類的設計原則
	//1、公有的類
	//2、提供共有的不帶參數的默認的構造方法
	//3、屬性私有
	//4、屬性setter/getter封裝
	
	private int sid;//學號
	private String name;//姓名
	private String gender;//性別
	private Date birthday;//出生日期
	private String address;//地址

	
	public Student(){
		
	}


	public Student(int sid, String name, String gender, Date birthday, String address) {
		this.sid = sid;
		this.name = name;
		this.gender = gender;
		this.birthday = birthday;
		this.address = address;
	}


	public int getSid() {
		return sid;
	}


	public void setSid(int sid) {
		this.sid = sid;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getGender() {
		return gender;
	}


	public void setGender(String gender) {
		this.gender = gender;
	}


	public Date getBirthday() {
		return birthday;
	}


	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}


	public String getAddress() {
		return address;
	}


	public void setAddress(String address) {
		this.address = address;
	}


	@Override
	public String toString() {
		return "Student [sid=" + sid + ", name=" + name + ", gender=" + gender + ", birthday=" + birthday + ", address="
				+ address + "]";
	}
	
}

  

  (4)創建對象——關系映射文件;

技術分享

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-4-14 17:17:44 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="hibernate001.Student" table="STUDENT">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
    </class>
</hibernate-mapping>

  (5)通過Hibernate API編寫訪問數據庫代碼:

  我用的是Juntil4這個測試類

package hibernate001;

import org.hibernate.Transaction;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//測試類
public class StudentTest {
	
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction; 
	@Before
	public void init(){
		//穿件配置對象
		Configuration configuration = new Configuration().configure();
		//創建服務註冊對象
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		//創建會話對象
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		//打開會話
		session = sessionFactory.openSession();
		//打開事務
		transaction = session.beginTransaction();
	}
	@After
	public void destory(){
		transaction.commit();//提交事務
		session.close();//關閉會話
		sessionFactory.close();//關閉會話工廠
	}
	@Test
	public void testSavestudent(){
		Student s1 = new Student(1, "張三豐", "男", new Date(), "武當山");
		session.save(s1);
	}
}

  

Hibernate單表映射學習筆記之一——hibernalnate開發環境配置