各种数据库在hibernate中的配置
2013-08-14 16:04
246 查看
1. MySql 连接配置
MySql 数据库的 hibernate 连接设置,在 hibernate.cfg.xml 文件中<hibernate-configuration>
<session-factory>
<!-- 各属性的配置 -->
<! —为 true 表示将 Hibernate 发送给数据库的 sql 显示出来 -->
<property name="show_sql">true</property>
<!-- SQL 方言,这边设定的是 MySQL
-->
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- 一次读的数据库记录数 -->
<property name="jdbc.fetch_size">50</property>
<!-- 设定对数据库进行批量删除 -->
<property name="jdbc.batch_size">30</property>
<!-- 驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/dbname?
characterEncoding=gb2312</property>
<!-- 数据库用户名 -->
<property name="connection.username">root</property>
<!-- 数据库密码 -->
<property name="connection.password">root</property>
<! —映射文件 -->
<mapping resource="com/amigo/pojo/User.hbm.xml"/>
<mapping resource="com/amigo/pojo/Org.hbm.xml"/>
</session-factory>
</hibernate-configuration>
上面使用的驱动类是 com.mysql.jdbc.Driver. 需要将 MySql 的连接器 jar 包( eg.
mysql-connector-java-
Dbname 为数据库名字
5.0.4 -bin.jar )加入到 classpath 中。
经过调试修改配置,正确配置如下:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 各属性的配置 --> <!-- 为 true 表示将 Hibernate 发送给数据库的 sql 显示出来 --> <property name="show_sql">true</property> <!-- SQL 方言,这边设定的是 MySQL --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 一次读的数据库记录数 --> <property name="jdbc.fetch_size">50</property> <!-- 设定对数据库进行批量删除 --> <property name="jdbc.batch_size">30</property> <!-- 驱动程序 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:mysql://localhost/mydb?characterEncoding=gb2312</property> <!-- 数据库用户名 --> <property name="connection.username">root</property> <!-- 数据库密码 --> <property name="connection.password">admin</property> <!-- 映射文件 --> <mapping resource="entity/UserInfo.hbm.xml"/> </session-factory> </hibernate-configuration> |
2. Sql Server 连接配置
Sql Server 数据库的 hibernate 在配置文件中配置。连接部分
<!-- 驱动程序 -->
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<!-- JDBC URL -->
<property
name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname</property>
<!-- 数据库用户名 -->
<property name="connection.username">sa</property>
<!-- 数据库密码 -->
<property name="connection.password"></property>
上例的驱动类使用的是 jtds 的驱动类,因此读者需要将 jtds 的 jar 包( eg.
jtds-1.2.jar )加入到 classpath 中
3. Oracle 连接配置
Oracler 数据库的 hibernate 在配置文件中配置。连接部分
<!-- 驱动程序 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:dbname</property>
<!-- 数据库用户名 -->
<property name="connection.username">test</property>
<!-- 数据库密码 -->
<property name="connection.password">test</property>
上例使用的驱动类为: oracle.jdbc.driver.OracleDriver ,开发人员需要将相关的 jar 包( ojdbc14.jar )加入到 classpath 中。
4. DB2 连接配置
DB2 数据库的 hibernate 在配置文件中配置。连接部分
<!-- 驱动程序 -->
<property name="connection.driver_class"> com.ibm.db2.jdbc.app.DB2Driver </property>
<!-- JDBC URL -->
<property
name="connection.url"> jdbc:db2://localhost:5000/sample </property> //sample 为数据库名
<!-- 数据库用户名 -->
<property name="connection.username">admin</property>
<!-- 数据库密码 -->
<property name="connection.password"></property>
上例使用的驱动类为: com.ibm.db2.jdbc.app.DB2Driver ,开发人员需要将相关的 jar 包( db2jcc.jar )加入到 classpath 中。
相关文章推荐
- Hibernate 各种数据库的配置
- 详解在Hibernate中配置数据库方言的作用和好处以及各种数据库的方言连接
- Hibernate与各种数据库连接的配置
- 提供各种数据库在JDBC中的连接方法.及在struts与hibernate中的配置
- Hibernate与各种数据库连接的配置
- Hibernate配置文件中配置各种数据库的driver、URL
- Hibernate与各种数据库连接的配置
- Hibernate 各种数据库的配置
- Hibernate配置文件中配置各种数据库的driver、URL
- 提供各种数据库在JDBC中的连接方法.及在struts与hibernate中的配置
- Hibernate与各种数据库连接 配置
- Hibernate配置文件中配置各种数据库的driver、URL
- Hibernate与各种数据库连接的配置
- 详解在Hibernate中配置数据库方言的作用和好处以及各种数据库的方言连接
- 【Hibernate】Hibernate 各种数据库的配置
- Hibernate与各种数据库连接的配置
- Hibernate配置文件中配置各种数据库的driver、URL
- Hibernate配置文件中配置各种数据库的driver、URL
- 详解在Hibernate中配置数据库方言的作用和好处以及各种数据库的方言连接
- Hibernate与各种数据库连接 配置