1、Hibernate入門案例剖析:程序員
①建立實體類Student 並重寫toString方法sql
public class Student { private Integer sid; private Integer age; private String name; public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [sid=" + sid + ", age=" + age + ", name=" + name + "]"; } }
② 建立學生對象 並賦值數據庫
③引入jar包session
④ 構建大配置<hibernate.cfg.xml>oracle
可分爲如下步驟:app
1.鏈接數據庫的語句ide
2.sql方言工具
3.可省的配置(show_sql、format_sql 取值爲true)測試
4.讓程序生成底層數據表(hbm2ddl.auto) update/create。create是每次將數據表刪除後,從新建立ui
5.關聯小配置
<mapping resource="cn/happy/entity/Student.hbm.xml" />
關鍵代碼以下:
<?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> <!-- Database connection settings 數據庫鏈接設置--> <!-- 驅動類 --> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <!-- url地址 --> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl3</property> <property name="connection.username">wj</property> <property name="connection.password">9090</property> <!-- SQL dialect (SQL 方言) --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!--在控制檯打印後臺的SQL語句 --> <property name="show_sql">true</property> <!-- 格式化顯示SQL --> <!-- <property name="format_sql">true</property> --> <!-- 自動生成student表 --> <property name="hbm2ddl.auto">update</property> <!-- 關聯小配置 --> <mapping resource="cn/happy/entity/Student.hbm.xml" /> <!-- <mapping class="cn.happy.entity.Grade"/> --> </session-factory> </hibernate-configuration>
⑤ 構建小配置(Student.hbm.xml)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.entity"> <class name="Student" table="STUDENT"> <id name="sid" column="SID"> <!-- 主鍵生成策略:native: native:若是後臺是Oracle 後臺是MySQL,自動應用自增 assigned:程序員給主鍵賦值 uuid:32位的16進制數 sequence native --> <generator class="assigned"> <param name="sequence">SEQ_NUM</param> </generator> </id> <!-- <version name="version"></version> --> <property name="name" type="string" column="NAME"/> <property name="age"/> </class> </hibernate-mapping>
⑥ 工具類HibernateUtil、構建私有靜態的Configuration、SessionFactory對象、定義返回session以及關閉session的方法
private static Configuration cf=new Configuration().configure(); private static SessionFactory sf=cf.buildSessionFactory(); //方法返回session public static Session getSession(){ return sf.openSession(); } //關閉Session public static void CloseSession(){ getSession().close(); }
⑦測試類【增刪改查】 使用標記After、Before可簡化代碼
package cn.happy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; import cn.happy.entity.Student; import cn.happy.util.HibernateUtil; public class Test1 { Session session; Transaction tx; @After public void afterTest(){ tx.commit(); HibernateUtil.CloseSession(); } @Before public void initData(){ session=HibernateUtil.getSession(); tx=session.beginTransaction(); } /* * get方法查詢 */ @Test public void getData(){ Student stu=(Student)session.get(Student.class, 3); System.out.println(stu); } /* * 增長 */ @Test public void addData(){ Student stu=new Student(); stu.setSid(12); stu.setAge(11); stu.setName("李小龍1"); //讀取大配置文件 獲取鏈接信息 Configuration cfg=new Configuration().configure(); //建立SessionFactory SessionFactory fa=cfg.buildSessionFactory(); //加工Session Session se=fa.openSession(); Transaction tx = se.beginTransaction(); //保存 se.save(stu); //事務提交 tx.commit(); se.close(); System.out.println("Save ok!"); } /* * 刪除 */ @Test public void delData(){ Session session=HibernateUtil.getSession(); Student stu=new Student(); stu.setSid(2); Transaction tx=session.beginTransaction(); session.delete(stu); tx.commit(); HibernateUtil.CloseSession(); System.out.println("del ok!"); } /* * 修改 */ @Test public void updateData(){ Session session=HibernateUtil.getSession(); Student stu=(Student)session.load(Student.class,3); stu.setName("呵呵"); Transaction tx=session.beginTransaction(); session.update(stu); tx.commit(); HibernateUtil.CloseSession(); System.out.println("update ok!"); } }