在Liferay中使用Hibernate

开发 后端
Liferay代表了完整的J2EE应用,使用了Web、EJB以及JMS等技术,特别是其前台界面部分使用Struts 框架技术,基于XML的portlet配置文件可以自由地动态扩展。这里将为大家介绍如何在Liferay中使用Hibernate。

Liferay是一个开源的portal框架,它目前的基础架构是基于Struts,Spring和Hibernate的。我们在Liferay上开发应用的时候,在需要使用数据库的时候可以方便的使用Spring中提供的对Hibernate的支持。

具体步骤如下

1, 在你的ext开发环境的/ext/ext-web/docroot/WEB-INF/目录下创建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,用于创建Session。

然后就可以进行数据库操作了。

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. //创建      
  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基础(2)
  2. Hibernate基础(1)
  3. eclipse下生成HibernateDAO中的几个方法
  4. hibernate中update与saveOrUpdate的区别
  5. Hibernate模糊查询参数化的问题
责任编辑:book05 来源: 百度博客
相关推荐

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

2013-12-13 17:21:14

Lua脚本语言

2023-04-12 15:25:09

Bytrace鸿蒙

2023-11-17 12:04:39

GORM并发

2011-06-15 10:08:01

Qt CVS

2010-04-29 17:51:40

Unix工具

2023-07-04 16:36:03

Linuxcd 命令

2017-04-05 13:17:26

LinuxShellvi

2023-07-23 19:26:18

Linuxcat 命令

2022-06-23 09:47:50

混沌工程系统Kubernetes

2009-06-16 09:06:37

JavaMailJSP

2021-03-22 08:45:30

异步编程Java

2012-04-19 12:58:26

TitaniumJSS

2013-05-14 10:13:06

WindowsLinux操作系统

2018-03-26 14:25:55

KubernetesSkaffold命令

2023-08-12 15:05:26

Linuxcp 命令
点赞
收藏

51CTO技术栈公众号