国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

Hibernate總結(jié)

2019-11-11 06:46:22
字體:
供稿:網(wǎng)友

**

hibernate設(shè)計及ORM(摘錄)

** ORM:Object–Relation–Mapping對象關(guān)系映射

對象指的就是java的實體對象; 關(guān)系指的是關(guān)系型數(shù)據(jù)庫。(Oracle、DB2、MySQL、SqlServer) ORM的主要思想就是將程序中的對象和數(shù)據(jù)庫中的數(shù)據(jù)實現(xiàn)自動映射 轉(zhuǎn)換。利用ORM工具,在查詢時,可以自動將記錄封裝成Java對象返 回。在更新、插入操作時,可以將對象自動寫入數(shù)據(jù)表。對于中間的 SQL+JDBC操作細節(jié),完全封裝在工具底層

hibernate幾個架構(gòu) 1)POJO類

public class hibernate_test { public String name; public int age; public String sex; public hibernate_test() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "hibernate_test [name=" + name + ", age=" + age + ", sex=" + sex + "]"; }}

2)hibernate_cfg.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="hibernate_001"> <class name="hibernate_test" table="HIBERNATE_TEST"> <id name="name" type="java.lang.String"> <column name="NAME" /> </id> <PRoperty name="age" type="int"> <column name="AGE" /> </property> <property name="sex" type="java.lang.String"> <column name="SEX" /> </property> </class> </hibernate-mapping>

3)*.hbm.xml

<hibernate-configuration><session-factory> <!--指定連接數(shù)據(jù)庫用的驅(qū)動--> <property name="connection.driver_class"></property> <property name="connection.url"></property> <!--指定連接數(shù)據(jù)庫的用戶名--> <property name="connection.username">用戶名</property> <property name="connection.passWord">密碼</property> <!-- SQL dialect oracle方言--> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- 配置文件地址 --> <mapping resource="xxx"/></session-factory></hibernate-configuration>

使用hibernate的優(yōu)勢 :不再需要大量的sql語句 ,實體類跟數(shù)據(jù)庫之間實現(xiàn)自動映射轉(zhuǎn)換

hibernate調(diào)用API **1.創(chuàng)建 Configuration對象 2.創(chuàng)建session工廠 3.通過工廠創(chuàng)建會話對象 4.獲取事物對象 5.進行操作,提交,結(jié)束及關(guān)閉Session**

Configuration cfg=new Configuration();SessionFactory sf=cfg.configure().buildSessionFactory();Session session=sf.openSession();session.beginTransaction();

常用的ID generator 1.identity 遞增 2.sequence 只限oracle 3.native 跨數(shù)據(jù)庫時使用 4.assigned 用戶自定義id

其他如foreign,sequence,Hilo

hibernate7大關(guān)聯(lián)映射 不多作介紹,詳見:

http://blog.csdn.net/t1012665655/article/details/54574441

hibernate緩存機制

“session緩存”即hibernate的一級緩存,sessionfactory即二級緩存,同時二級緩存適合存放 :常量數(shù)據(jù),幾乎不再修改的數(shù)據(jù),非并發(fā)訪問的數(shù)據(jù)

詳見:

http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html

Hibernate查詢(摘錄)

*a.HQL查詢

Hibernate Query LanguageHQL與SQL語句結(jié)構(gòu)相似,SQL語句是面向數(shù)據(jù)表和字段進行查詢,而HQL語句是面向Hibernate映射過來的對象進行查詢,因此HQL被稱為是一種面向?qū)ο蟛樵冋Z言HQL和SQL共同點: --都支持select,from,where,order by,having,group by等子句。 --都支持運算符表達式,例如+,-,*,/,>,<等 --都支持in,not in,between and,like等過濾條件關(guān)鍵字 --都支持分組函max,min,sum,avg,countHQL和SQL不同點: --HQL是大小寫敏感的,類名和屬性名嚴格區(qū)分大小寫 --HQL不支持select * 寫法 --HQL不支持join...on...中的on子句,因為join...on發(fā)生在多表操作,而 Hibernate中對于有關(guān)系的多張表已將將關(guān)聯(lián)映射寫在了xxx.hbm.xml 中,在查詢時會自動加上join..on --HQL不支持表名和字段名public class TestHQL { Configuration cfg=new Configuration(); SessionFactory sFactory=cfg.configure().buildSessionFactory(); //@Test public void save(){ Session session=sFactory.openSession(); session.beginTransaction(); //設(shè)置10個板塊 for (int i = 0; i < 10; i++) { Categroy categroy=new Categroy(); categroy.setName("c"+i); session.save(categroy); } //設(shè)10個主題 for (int i = 0; i <10; i++) { Categroy c=new Categroy(); c.setId(1); Topic topic=new Topic(); topic.setCategroy(c); topic.setName("t"+i); session.save(topic); } //設(shè)10個對應(yīng)回復 /*for (int i = 0; i < 10; i++) { Topic t=new Topic(); t.setId(1); Msg msg=new Msg(); msg.setMess("m"+i); msg.setTopic(t); session.save(msg); }*/ session.getTransaction().commit(); session.close(); } //@Test public void Test01(){ Session session=sFactory.openSession(); session.beginTransaction(); Query query=session.createQuery("from Categroy"); List<Categroy> categroys=query.list(); for(Categroy c:categroys){ System.out.println(c.getName()); } session.getTransaction().commit(); session.close(); } /** *查詢多個屬性,動態(tài)構(gòu)建一個對象,必須要有有參,無參構(gòu)造器 * */ // @Test public void Test02(){ Session session=sFactory.openSession(); session.beginTransaction(); String sql="select new Topic(id,name,categroy) from Topic"; Query query=session.createQuery(sql); List<Topic> topics=query.list(); for(Topic c:topics){ System.out.println(c); } session.getTransaction().commit(); session.close(); } /** * 對象查詢,條件查詢 * 參數(shù)的順序從0開始 */ // @Test public void test03(){ Session session = sFactory.openSession(); String hql = "select * from Dept where name=?"; Query query = session.createQuery(hql); query.setString(0,"hfz"); List<Topic> topics = query.list(); for (Topic d :topics) { System.out.println(d); } session.getTransaction().commit(); session.close(); } /** * 對象查詢,條件查詢 */ //@Test public void test04(){ Session session = sFactory.openSession(); String hql = "select * from Dept where name=? and id=?"; Query query = session.createQuery(hql); query.setString(0,"hfz").setInteger(1,1).list(); List<Topic> topics = query.list(); for (Topic d :topics) { System.out.println(d); } session.getTransaction().commit(); session.close(); } /** * 查詢所有板塊信息 * 分頁查詢 * setFirstResult();確定查詢的起點 * setMaxResult();確定查詢的條數(shù) */ @Test public void test05(){ Session session = sFactory.openSession(); int currentPage = 5; int pageSize = 5; List<Categroy> categroys = session.createQuery("from Categroy"). setFirstResult((currentPage-1)*pageSize). setMaxResults(pageSize).list(); for (Categroy c : categroys) { System.out.println(c.getName()); } session.getTransaction().commit(); session.close(); } }

當然,現(xiàn)在與之類似的mybatis也用的越來越多,倆者的對比也是沒玩沒了,到最后無非還是那句話,能抓老鼠的貓就是一只好貓 引用mybatis作者的話即:


If you are starting a new project and you’re in full control of your object model and database design, Hibernate is a good choice of O/R tool. If you are accessing any 3rd party databases (e.g. vendor supplied), or you’re working with a legacy database, or even just a really poorly designed database, then an O/R mapper might not be capable of handling the situation. That’s were an SQL Mapper comes in handy



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙陵县| 托克托县| 广西| 大名县| 尼勒克县| 邓州市| 乐业县| 武功县| 三明市| 铁岭市| 石城县| 弋阳县| 鲁甸县| 大新县| 得荣县| 洞口县| 宁安市| 五大连池市| 新建县| 琼中| 石城县| 锦屏县| 邵阳市| 延川县| 佛教| 鄱阳县| 新乡县| 独山县| 临洮县| 堆龙德庆县| 恭城| 廉江市| 曲周县| 黔南| 小金县| 绥宁县| 峨眉山市| 修水县| 通山县| 四子王旗| 闽侯县|