Hibernate 连接MySQL/SQLServer/Oracle数据库的hibernate.cfg.xml文件

时间:2021-09-02 21:44:59

用Hibernate配置连接数据库可以方便我们对POJO的操作,节省了很多时间和代码。下面就分别说明连接不同数据库需要在hibernate.cfg.xml做的配置。

需要数据库驱动包可以点击这里下载:数据库Jar包下载地址:http://pan.baidu.com/s/1jGKEEY6  密码:okq0

1、Hibernate连接MySQL数据库的hibernate.cfg.xml

 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 配置JDBC连接属性 -->
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/basehibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property> <!-- 自动建表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 自动提交,不加的话可能会出现insert之后数据库无数据 -->
<property name="connection.autocommit">true</property> <!-- 使用Hibernate Annotation的POJO类 -->
<mapping class="com.basehibernate.pojo.Department" />
<mapping class="com.basehibernate.pojo.Employee" />
</session-factory>
</hibernate-configuration>

2、Hibernate连接Oracle数据库的hibernate.cfg.xml

 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 配置JDBC连接属性 -->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:chanshuyi
</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="connection.username">csy</property>
<property name="connection.password">csy</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- oracle特有的提交更改 -->
<property name="defaultAutoCommit">true</property>
<!-- 自动建表 -->
<property name="hbm2ddl.auto">auto</property>
<property name="show_sql">true</property>
<!-- 自动提交,不加的话可能会出现insert之后数据库无数据 -->
<property name="connection.autocommit">true</property> <!-- 使用Hibernate Annotation的POJO类 -->
<mapping class="com.xinpinv.pojo.Product" />
<mapping class="com.xinpinv.pojo.BitInfo" />
</session-factory>
</hibernate-configuration>

3、Hibernate连接Oracle数据库的hibernate.cfg.xml

(MARK 待写)