体会真正的Struts开发(2期)4

类别:Java 点击:0 评论:0 推荐:

我们修改配置文件Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration

    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/jdbc/mysql</property>

        <property name="show_sql">false</property>

        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

        <mapping resource="com/yjsoft/hbm/UserInfo.hbm.xml"/>

    </session-factory>

 

</hibernate-configuration>

修改web.xml添加

<servlet>

        <servlet-name>testConnectionPooler</servlet-name>

        <servlet-class>com.test.web.TestHBMServlet</servlet-class>

        <load-on-startup>2</load-on-startup>

   </servlet>

测试的Servlet如下

package com.test.web;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

 

import net.sf.hibernate.HibernateException;

import net.sf.hibernate.MappingException;

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.cfg.Configuration;

 

import org.apache.log4j.Logger;

 

import com.yjsoft.hbm.UserInfo;

/**

 * @author yanghx

 *

 * 更改所生成类型注释的模板为 窗口 > 首选项 > Java > 代码生成 > 代码和注释

 */

public class TestHBMServlet extends HttpServlet {

    private static Logger log = Logger.getLogger(TestHBMServlet.class);

    public void init() throws ServletException {

        super.init();

        Configuration cfg = null;

        SessionFactory sf = null;

        //SchemaExport pTemp = null;

        Session s = null;

        try {

            //cfg = new Configuration();

            cfg = new Configuration().configure("/hibernate.cfg.xml");

            //cfg.addClass(UserInfo.class);

        } catch (MappingException e1) {

            e1.printStackTrace();

            log.error("映射错误:" + e1);

            return;

        } catch (HibernateException e) {

            // TODO 自动生成 catch 块

            e.printStackTrace();

            log.error("映射错误:" + e);

            return;

        }

本文地址:http://com.8s8s.com/it/it16700.htm