数据源+hibernate

发布时间:2016-12-11 10:23:23 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"数据源+hibernate",主要涉及到数据源+hibernate方面的内容,对于数据源+hibernate感兴趣的同学可以参考一下。

 终于配成功了,好东西放上来。 在server。xml加入下面代码,用来配置数据源   <Context            docBase="G:ycackwebappshibernate"            path="/hibernate"            reloadable="true">          <Resource name="jdbc/mysql" type="javax.sql.DataSource"/>          <ResourceParams name="jdbc/mysql">     <parameter>        <name>url</name>        <value>jdbc:mysql://127.0.0.1:3306/test</value>      </parameter>      <parameter>        <name>driverClassName</name>        <value>com.mysql.jdbc.Driver</value>      </parameter>      <parameter>        <name>maxWait</name>        <value>5000</value>      </parameter>      <parameter>        <name>maxActive</name>        <value>4</value>      </parameter>      <parameter>        <name>password</name>        <value>itrader</value>      </parameter>      <parameter>        <name>maxIdle</name>        <value>10</value>      </parameter>      <parameter>        <name>username</name>        <value>root</value>      </parameter>      <parameter>         <name>factory</name>         <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>       </parameter>     </ResourceParams>    jsp中测试代码如下: <%Connection conn=null;Statement stmt=null;ResultSet rs=null;ResultSetMetaData md=null;try...{Context initCtx=new InitialContext();DataSource ds=(DataSource)initCtx.lookup("java:comp/env/jdbc/mysql");if(ds!=null)...{out.println("DataSource");out.println(ds.toString());conn=ds.getConnection();stmt=conn.createStatement();out.println("aa");rs=stmt.executeQuery("select * from employee");md=rs.getMetaData();out.println("<table border=1>");out.println("<tr>");for(int i=0;i<md.getColumnCount();i++)...{out.println("<td>"+md.getColumnName(i+1)+"</td>");}while(rs.next())...{out.println("<tr>");out.println("<td>"+rs.getInt(1)+"</td>");out.println("<td>"+rs.getString(2)+"</td>");out.println("<td>"+rs.getString(3)+"</td>");out.println("<td>"+rs.getDouble(4)+"</td>");out.println("</tr>");}out.println("</table>");conn.close();}}catch(Exception e)...{out.println(e.toString());System.out.println(e.toString());}%> 配置hibernate.cfg.xml,Account.hbm.xml。(放在WEB-INF------classes文件夹下) 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-3.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">org.hibernate.dialect.PostgreSQLDialect</property>        <!-- Mapping files -->        <mapping resource="Account.hbm.xml"/>    </session-factory></hibernate-configuration>   Account.hbm.xml <?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>        <class name="query.Account"             table="account"            discriminator-value="C">                                <id name="username">                  </id>        <property name="password"/></class> </hibernate-mapping>   实例化一个类Account.java对映你数据库中的一个表 package query;public class Account ...{    private String username;    private String password;    public String getUsername()...{    return username;    }    public String getPassword()...{    return password;    }    public void setUsername(String username)...{    this.username=username;    }    public void setPassword(String password)...{    this.password=password;    }} 最后就是HibernateUtil.java package classmate;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class HibernateUtil ...{    private static Log log = LogFactory.getLog(HibernateUtil.class);    private static final SessionFactory sessionFactory;    static ...{        try ...{            //建立对应的session工厂            sessionFactory = new Configuration().configure().buildSessionFactory();        } catch (Throwable ex) ...{            ex.printStackTrace();            throw new ExceptionInInitializerError(ex);        }    }    public static final ThreadLocal session = new ThreadLocal();        //获得session对象的方法    public static Session currentSession() throws HibernateException ...{        Session s = (Session) session.get();        //打开一个新的session        if (s == null||!s.isOpen()) ...{            s = sessionFactory.openSession();            session.set(s);        }        return s;    }    //关闭session对象的方法    public static void closeSession() throws HibernateException ...{        Session s = (Session) session.get();        session.set(null);        if (s != null)            s.close();    }}

上一篇:开源MVC框架 beyard.NET使用
下一篇:《WINDOWS核心编程》读书笔记--第2章:Unicode

相关文章

相关评论