標籤:setting roo 筆記 eclips apply factory doc 會話 driver
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&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;}@Overridepublic 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; @Beforepublic 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();}@Afterpublic void destory(){transaction.commit();//提交事務session.close();//關閉會話sessionFactory.close();//關閉會話工廠}@Testpublic void testSavestudent(){Student s1 = new Student(1, "張三丰", "男", new Date(), "武當山");session.save(s1);}}
Hibernate單表映射學習筆記之一——hibernalnate開發環境配置