如何在Liferay中使用Hibernate

這篇文章主要介紹“如何在Liferay中使用Hibernate”,在日常操作中,相信很多人在如何在Liferay中使用Hibernate問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”如何在Liferay中使用Hibernate”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

十載的平陽網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整平陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“平陽網(wǎng)站設(shè)計(jì)”,“平陽網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

Liferay是一個(gè)開源的portal框架,它目前的基礎(chǔ)架構(gòu)是基于Struts,Spring和Hibernate的。我們?cè)贚iferay上開發(fā)應(yīng)用的時(shí)候,在需要使用數(shù)據(jù)庫的時(shí)候可以方便的使用Spring中提供的對(duì)Hibernate的支持。

具體步驟如下

1, 在你的ext開發(fā)環(huán)境的/ext/ext-web/docroot/WEB-INF/目錄下創(chuàng)建classes目錄,降hibernate.cfg.xml文件和你的hbm文件放在這里。
這是hibernate.cfg.xml文件的例子.

Java代碼

< ?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="Hibernate.connection.driver_class">com.MySQL.jdbc.Driver< /property>           < property name="Hibernate.connection.password">luser< /property>           < property name="Hibernate.connection.url">jdbc:mysql://localhost:3306/lportal< /property>           < property name="Hibernate.connection.username">luser< /property>           < property name="Hibernate.dialect">org.hibernate.dialect.MySQLDialect< /property>           < property name="Hibernate.show_sql">true< /property>           < mapping resource="example.hbm.xml"/>         < /session-factory>      < /hibernate-configuration>      < ?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="Hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property>     < property name="Hibernate.connection.password">luser< /property>     < property name="Hibernate.connection.url">jdbc:mysql://localhost:3306/lportal< /property>     < property name="Hibernate.connection.username">luser< /property>     < property name="Hibernate.dialect">org.hibernate.dialect.MySQLDialect< /property>     < property name="Hibernate.show_sql">true< /property>     < mapping resource="example.hbm.xml"/>   < /session-factory> < /hibernate-configuration>

example.hbm.xml是你定義的hbm文件,這里就不贅述了。

2,在你的DAOImpl類中使用Spring提供的HibernateTemplate,

Java代碼

this.hibernateTemplate = new HibernateTemplate(sessionFactory);     this.hibernateTemplate = new HibernateTemplate(sessionFactory);

sessionFactory是org.hibernate.SessionFactory,用于創(chuàng)建Session。

然后就可以進(jìn)行數(shù)據(jù)庫操作了。

Java代碼

//查詢      public YourPOJO getByPrimaryKey(final long yourId) {              return (YourPOJO ) this.hibernateTemplate.execute(new HibernateCallback() {                  public Object doInHibernate(Session session) throws HibernateException, SQLException {                       Criteria criteria = session.createCriteria(YourPOJO .class)                               .add(Restrictions.eq("yourId", yourId));                      return criteria.uniqueResult();                   }               });           }      //創(chuàng)建          public void create(final YourPOJO pojo) {              this.hibernateTemplate.save(pojo);           }      //修改          public void update(final YourPOJO pojo) {              this.hibernateTemplate.saveOrUpdate(pojo);           }      //刪除          public void delete(final YourPOJO pojo) {              this.hibernateTemplate.delete(pojo);           }

到此,關(guān)于“如何在Liferay中使用Hibernate”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

文章題目:如何在Liferay中使用Hibernate
分享地址:http://muchs.cn/article48/ihdpep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、云服務(wù)器、標(biāo)簽優(yōu)化、做網(wǎng)站、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化