liferay开发(二)开发详细举例--例二

来源:互联网 发布:直播截屏软件 编辑:程序博客网 时间:2024/06/05 06:37

 

例二

       有关数据库相连的例子。(例一中的部分不动)

1.    ext-impl/src中新建3个包,分别为com.ext.portlet.hello.beanscom.ext.portlet.hello.managercom.ext.portlet.hello.persistence,其中,在beans中加入类HelloBeans,在manager中加入类HelloUtil,在persistence中加入类HelloPersistence,HelloPersistenceImpl

2.    HelloBeans中写入

package com.ext.portlet.hello.beans;

 

public class HelloBeans {

    private int id=0;

    private String name="";

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

 

}

HelloUtil中写入:

package com.ext.portlet.hello.manager;

 

 

import java.util.List;

import com.ext.portlet.hello.persistence.HelloPersistence;

import com.liferay.portal.SystemException;

 

public class HelloUtil {

     private static HelloUtil _util;

     private HelloPersistence service=null;

     private static final String _UTIL = HelloUtil.class.getName();

      

    private static HelloUtil _getUtil() {

           if (_util == null) {

              _util = (HelloUtil)com.liferay.portal.kernel.bean.BeanLocatorUtil.locate(_UTIL);

           }

           return _util;

    }

 

    public static HelloPersistence getService() {

           return _getUtil().service;

    }

 

    public void setService(HelloPersistence service) {

           this.service = service;

    }

    public static List getAll()throws SystemException{

       return getService().getAll();

    }

}

HelloPersistence中写入

package com.ext.portlet.hello.persistence;

 

import java.util.List;

import com.liferay.portal.SystemException;

 

public interface HelloPersistence {

    public List getAll() throws SystemException;

 

}

HelloPersistenceImpl中写入:

package com.ext.portlet.hello.persistence;

 

import java.util.List;

 

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import com.liferay.portal.SystemException;

import com.liferay.portal.service.persistence.BasePersistence;

 

public class HelloPersistenceImpl extends BasePersistence

implements HelloPersistence{

    public List getAll()throws SystemException{

       Session session = null;

       try{

           session = openSession();

           StringBuffer str = new StringBuffer();

           str.append("From com.ext.portlet.hello.beans.HelloBeans");

           Query q = session.createQuery(str.toString());

           q.setCacheable(true);

           return q.list();

       }catch(HibernateException he){

           throw new SystemException(he);

       }finally{

           closeSession(session);

       }

    }

 

}

修改action里的内容,将HelloAction修改:

package com.ext.portlet.hello.action;

 

import com.ext.portlet.hello.beans.HelloBeans;

import com.ext.portlet.hello.manager.HelloUtil;

import com.liferay.portal.struts.PortletAction;

import java.util.ArrayList;

import java.util.List;

import javax.portlet.PortletConfig;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

import javax.portlet.WindowState;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

 

public class HelloAction extends PortletAction {

    public ActionForward render(ActionMapping mapping, ActionForm form,

           PortletConfig config, RenderRequest req, RenderResponse res)

           throws Exception {

       if (req.getWindowState().equals(WindowState.NORMAL)) {

           return mapping.findForward("portlet.ext.hello.view");

       } else {

           List hello = HelloUtil.getAll();

           List reports = new ArrayList();

           for (int i = 0; i < hello.size(); i++) {

              reports.add(((HelloBeans) hello.get(i)).getName());

              req.setAttribute("reports", reports);

           }

           return mapping.findForward("portlet.ext.hello.view_reports");

       }

    }

 

}

3.    添加配置文件内容:

       ext-impl/src/META-INF中的ext-hbm.xml中添加信息

    <class name="com.ext.portlet.hello.beans.HelloBeans" table="hello">

    <id name="id" column="id" type="int">

        <generator class="native"></generator>

    </id>

     <property name="name" column="name" type="com.liferay.util.dao.hibernate.StringType"></property>

    </class>

    ext-impl/src/META-INF中的ext-spring.xml中添加信息

               <bean id="com.ext.portlet.hello.manager.HelloUtil" class="com.ext.portlet.hello.manager.HelloUtil" lazy-init="true">

       <property name="service">                                        

          <ref bean="com.ext.portlet.hello.service.transaction"/>

       </property>

    </bean>

    <bean id="com.ext.portlet.hello.service.transaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">

        <property name="transactionManager">

           <ref bean="liferayTransactionManager" />

       </property>

       <property name="target">

           <ref bean="com.ext.portlet.hello.service.impl" />

       </property>

       <property name="transactionAttributes">

           <props>

              <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>

              <prop key="search*">PROPAGATION_REQUIRED</prop>

              <prop key="deleteValue">PROPAGATION_REQUIRED,-com.liferay.portal.PortalException,-com.liferay.portal.SystemException</prop>

              <prop key="askValue">PROPAGATION_REQUIRED,-com.liferay.portal.PortalException,-com.liferay.portal.SystemException</prop>

           </props>

       </property>

    </bean>

    <bean id="com.ext.portlet.hello.service.impl" class="com.ext.portlet.hello.persistence.HelloPersistenceImpl" lazy-init="true">

        <property name="dataSource">

           <ref bean="liferayDataSource" />

       </property>

       <property name="sessionFactory">

           <ref bean="liferaySessionFactory" />

       </property>

    </bean>

4.    在数据库中新建表hello,列名为id,name,分别为int,varchar,自己添加几个数据。

       这样就完成了。

 

原创粉丝点击