自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在Liferay中使用Hibernate

開發(fā) 后端
Liferay代表了完整的J2EE應(yīng)用,使用了Web、EJB以及JMS等技術(shù),特別是其前臺界面部分使用Struts 框架技術(shù),基于XML的portlet配置文件可以自由地動態(tài)擴(kuò)展。這里將為大家介紹如何在Liferay中使用Hibernate。

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

具體步驟如下

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

Java代碼

  1. < ?xml version="1.0" encoding="UTF-8"?>      
  2. < !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">      
  3.      
  4. < hibernate-configuration>      
  5.    < session-factory>      
  6.      < property name="Hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property>      
  7.      < property name="Hibernate.connection.password">luser< /property>      
  8.      < property name="Hibernate.connection.url">jdbc:mysql://localhost:3306/lportal< /property>      
  9.      < property name="Hibernate.connection.username">luser< /property>      
  10.      < property name="Hibernate.dialect">org.hibernate.dialect.MySQLDialect< /property>      
  11.      < property name="Hibernate.show_sql">true< /property>      
  12.      < mapping resource="example.hbm.xml"/>      
  13.    < /session-factory>      
  14. < /hibernate-configuration>     
  15.  
  16. < ?xml version="1.0" encoding="UTF-8"?> 
  17. < !DOCTYPE Hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  18.  
  19. < hibernate-configuration>   
  20.   < session-factory>   
  21.     < property name="Hibernate.connection.driver_class">com.mysql.jdbc.Driver< /property> 
  22.     < property name="Hibernate.connection.password">luser< /property> 
  23.     < property name="Hibernate.connection.url">jdbc:mysql://localhost:3306/lportal< /property> 
  24.     < property name="Hibernate.connection.username">luser< /property> 
  25.     < property name="Hibernate.dialect">org.hibernate.dialect.MySQLDialect< /property> 
  26.     < property name="Hibernate.show_sql">true< /property> 
  27.     < mapping resource="example.hbm.xml"/> 
  28.   < /session-factory> 
  29. < /hibernate-configuration> 

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

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

Java代碼

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

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

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

Java代碼

  1. //查詢      
  2. public YourPOJO getByPrimaryKey(final long yourId) {      
  3.         return (YourPOJO ) this.hibernateTemplate.execute(new HibernateCallback() {      
  4.             public Object doInHibernate(Session session) throws HibernateException, SQLException {      
  5.                  Criteria criteria = session.createCriteria(YourPOJO .class)      
  6.                          .add(Restrictions.eq("yourId", yourId));      
  7.                 return criteria.uniqueResult();      
  8.              }      
  9.          });      
  10.      }      
  11. //創(chuàng)建      
  12.     public void create(final YourPOJO pojo) {      
  13.         this.hibernateTemplate.save(pojo);      
  14.      }      
  15. //修改      
  16.     public void update(final YourPOJO pojo) {      
  17.         this.hibernateTemplate.saveOrUpdate(pojo);      
  18.      }      
  19. //刪除      
  20.     public void delete(final YourPOJO pojo) {      
  21.         this.hibernateTemplate.delete(pojo);      
  22.      }    

【編輯推薦】

  1. Hibernate基礎(chǔ)(2)
  2. Hibernate基礎(chǔ)(1)
  3. eclipse下生成HibernateDAO中的幾個方法
  4. hibernate中update與saveOrUpdate的區(qū)別
  5. Hibernate模糊查詢參數(shù)化的問題
責(zé)任編輯:book05 來源: 百度博客
相關(guān)推薦

2009-06-10 09:58:32

Oracle sequHibernate

2009-06-30 16:52:30

DetchedCrit

2011-08-10 09:31:41

Hibernateunion

2009-06-30 16:55:19

2009-09-23 13:04:58

HibernateOracle sequ

2023-11-17 12:04:39

GORM并發(fā)

2013-12-13 17:21:14

Lua腳本語言

2023-04-12 15:25:09

Bytrace鴻蒙

2011-06-15 10:08:01

Qt CVS

2010-04-29 17:51:40

Unix工具

2023-07-04 16:36:03

Linuxcd 命令

2023-07-23 19:26:18

Linuxcat 命令

2017-04-05 13:17:26

LinuxShellvi

2018-03-26 14:25:55

KubernetesSkaffold命令

2021-03-22 08:45:30

異步編程Java

2012-04-19 12:58:26

TitaniumJSS

2009-06-16 09:06:37

JavaMailJSP

2013-05-14 10:13:06

WindowsLinux操作系統(tǒng)

2023-08-12 15:05:26

Linuxcp 命令

2022-06-23 09:47:50

混沌工程系統(tǒng)Kubernetes
點贊
收藏

51CTO技術(shù)棧公眾號