struts2+hibernate

2010-06-07  曾晨 

addvipdata.jsp的代码如下:

<%@ page language="java" contenttype="text/html; charset=gbk" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>

<%@ page import="com.xiebing.hibernate.vipservice" %>

<%@ page import="java.util.list" %>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">

<html:html locale="true">

<head>

   <title>vip example</title>

   <html:base/>

</head>

<body bgcolor="white">

   <h3>vip example</h3>

   <html:errors/>

   <%

      /*

       * 这里将产生一个vipdata的集合

       */

      list viplist = vipservice.getinstance().getvipdatalist();

      request.setattribute("vipdatas", viplist);

   %>

   <p>列出 <code>vipdata</code>表中的所有信息.</p>

   <table border=1>

   <logic:iterate id="element" name="vipdatas" scope="request" type="com.xiebing.hibernate.vipdata" >

   <tr>

      <td><bean:write name="element" property="vipname" /></td>

      <td><bean:write name="element" property="viptitle" /></td>

   </tr>

   </logic:iterate>

   </table>

   <p>新增加一条数据:</p>   

   <html:form action="addvipdata.do" method="post">

      <table border=1>

         <tr><td>name:</td><td><html:text property="vipname" /></tr>

         <tr><td>description:</td><td><html:text property="viptitle" /></td></tr>

      </table><br/>

      <html:submit/>

   </html:form>

</body>

</html:html>阿飞发送多幅发送给扶绥多给

此时加粗的部分会提示有问题(先不管他),因为还没有创建类:com.xiebing.hibernate.vipservice

7、接下来就创建类:com.xiebing.hibernate.vipservice。其主要就是完成我们的增删改查等具体业务逻辑的处理。

代码如下:

vipservice.java

package com.xiebing.hibernate;

import java.util.list;

import net.sf.hibernate.hibernateexception;

import net.sf.hibernate.objectnotfoundexception;

import net.sf.hibernate.query;

import net.sf.hibernate.session;

import com.xiebing.hibernate.vipdata;

/**

 * 通过hibernate来操作数据库的业务逻辑类

 *

 */

public class vipservice

{

       private static vipservice instance = null;

       private vipservice()

       {

       }

       /**

        * 得到vipservice的单态实例

        * @return <code>vipservice</code> singleton.

        */

       public static synchronized vipservice getinstance()

       {

              if (instance == null)

              {

                     instance = new vipservice();

              }

              return instance;

       }

       /**

        * 通过id来获得vipdata

        * @param id

        * @return vipdata

        */

       public vipdata getvipdata(long id)

       {

              session session = null;

              try

              {

                     //通过sessionfactory创建session实例

                     session = sessionfactory.currentsession();

                     //调用load方法加载数据

                     return (vipdata) session.load(vipdata.class, id);

              }

              catch (objectnotfoundexception onfe)

              {

                     return null;

              }

              catch (hibernateexception e)

              {

                     system.err.println("hibernate exception" + e.getmessage());

                     throw new runtimeexception(e);

              }

              finally

              {

                     if (session != null)

                     {

                            try

                            {

                                   //关闭session

                                   session.close();

                            }

                            catch (hibernateexception e)

                            {

                                   system.err.println("hibernate exception" + e.getmessage());

                                   throw new runtimeexception(e);

                            }

                     }

              }

       }

       /**

        * 更新记录

        *

        * @param vipdata

        */

       public void updatevipdata(vipdata vipdata)

       {

              session session = null;

              try

              {

                     session = sessionfactory.currentsession();

                     //执行update操作

                     session.update(vipdata);

                     session.flush();

              }

              catch (hibernateexception e)

              {

                     system.err.println("hibernate exception" + e.getmessage());

                     throw new runtimeexception(e);

              }

              finally

              {

                     if (session != null)

                     {

                            try

                            {

                                   session.close();

                            }

                            catch (hibernateexception e)

                            {

                                   system.err.println("hibernate exception" + e.getmessage());

                                   throw new runtimeexception(e);

                            }

                     }

              }

       }

       /**

        * 得到所有的记录

        *

        * @return 记录的列表

        */

       public list getvipdatalist()

       {

              session session = null;

              try

              {

                     session = sessionfactory.currentsession();

                     //创建一条hql查询

                     query query =

                            session.createquery(

                                   "select vipdata from com.xiebing.hibernate.vipdata vipdata order by vipdata.vipname");

                     return query.list();

              }

              catch (hibernateexception e)

              {

                     system.err.println("hibernate exception" + e.getmessage());

                     throw new runtimeexception(e);

              }

              finally

              {

                     if (session != null)

                     {

                            try

                            {

                                   session.close();

                            }

                            catch (hibernateexception e)

                            {

                                   system.err.println("hibernate exception" + e.getmessage());

                                   throw new runtimeexception(e);

                            }

                     }

              }

       }

       /**

        * 添加一条新的记录

        * @param data

        */

       public void addvipdata(vipdata data)

       {

              session session = null;

              try

              {

                     session = sessionfactory.currentsession();

                     session.save(data);

                     session.flush();

              }

              catch (hibernateexception e)

              {

                     system.err.println("hibernate exception" + e.getmessage());

                     throw new runtimeexception(e);

              }

              finally

              {

                     if (session != null)

                     {

                            try

                            {

                                   session.close();

                            }

                            catch (hibernateexception e)

                            {

                                   system.err.println("hibernate exception" + e.getmessage());

                                   throw new runtimeexception(e);

                            }

                     }

              }

       }

}

8、顺便简单说一下工具自动生成的sessionfactory的代码,sessionfactory.java

package com.xiebing.hibernate;

import net.sf.hibernate.hibernateexception;

import net.sf.hibernate.session;

import net.sf.hibernate.cfg.configuration;

public class sessionfactory {

    private static string config_file_location = "/hibernate.cfg.xml";

    /** 保持session对象为单态

     * 初始化一个threadlocal对象

     *  */

    private static final threadlocal threadlocal = new threadlocal();

    private static final configuration cfg = new configuration();

    private static net.sf.hibernate.sessionfactory sessionfactory;

    /**

     * returns the threadlocal session instance.

     *

     *  @return session

     *  @throws hibernateexception

     */

    public static session currentsession() throws hibernateexception {

           //多线程情况下共享数据库链接是不安全的。threadlocal保证了每个线程都有自己的s(数据库连接)

        session session = (session) threadlocal.get();

        if (session == null) {

            if (sessionfactory == null) {

                try {

                    cfg.configure(config_file_location);

                    sessionfactory = cfg.buildsessionfactory();

                }

                catch (exception e) {

                    system.err.println("%%%% error creating sessionfactory %%%%");

                    e.printstacktrace();

                }

            }

            //创建一个数据库连接实例

            session = sessionfactory.opensession();

            //保存该数据库连接sthreadlocal

            threadlocal.set(session);

        }

        return session;

    }

    /**

     *  close the single hibernate session instance.

     *

     *  @throws hibernateexception

     */

    public static void closesession() throws hibernateexception {

        session session = (session) threadlocal.get();

        threadlocal.set(null);

        if (session != null) {

            session.close();

        }

    }

    /**

     * default constructor.

     */

    private sessionfactory() {

    }

}

312°/3120 人阅读/0 条评论 发表评论

登录 后发表评论