hibernate配置文件中与数据库的两种连接

hibernate学习:hibernate.cfg.xml文件中与数据库的两种连接
第一、hibernate自行管理连接池
编写hibernate配置文件(hibernate.cfg.xml)
<?xml version='1.0' encoding='gbk'?>
<!doctype hibernate-configuration
public "-//hibernate/hibernate configuration dtd//en"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.mysqldialect</property>
<property name="connection.driver_class">com.mysql.jdbc.driver</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernatetest?useunicode=true&characterencoding=gbk
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<mapping resource="com/hibernate/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第二、使用外部服务器提供的数据库连接池(以tomcat为例)分为两步:tomcat会提供经过池处理的jdbc连接(用它内置的dbcp连接池),hibernate通过jndi方式来请求获得jdbc连接。
1) tomcat把连接池绑定到jndi,我们要在tomcat的主配置文件(tomcat/conf/server.xml)中加一个资源声明:
<context path="/mis" docbase="mis" reloadable="true">
<resource name="jdbc/mis" scope="shareable" type="javax.sql.datasource"/>
<resourceparams name="jdbc/mis">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.basicdatasourcefactory</value>
</parameter>
<!-- dbcp database connection settings -->
<parameter>
<name>url</name>
<!--hibernatetest 是mysql中的一个schema-->
<value>jdbc:mysql://localhost:3306/hibernatetest?useunicode=true&characterencoding=gbk</value>
</parameter>
<parameter>
<name>driverclassname</name><value>com.mysql.jdbc.driver</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<!-- dbcp connection pooling options -->
<parameter>
<name>maxwait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxidle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxactive</name>
<value>10</value>
</parameter>
</resourceparams>
</context>
我们在这个例子中要配置的上下文叫做mis,它位于tomcat/webapp/mis目录下。
tomcat现在通过jndi的方式:java:comp/env/jdbc/mis来提供连接。
2)下一步就是配置hibernate。首先hibernate必须知道它如何获得jdbc连接,在这里我们使用基于xml格式的hibernate配置文件(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/mis</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.mysqldialect</property>
<!-- mapping files -->
<mapping resource="com/jason/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值