用Hibernate配置连接数据库可以方便我们对POJO的操作,节省了很多时间和代码。下面就分别说明连接不同数据库需要在hibernate.cfg.xml做的配置。
需要数据库驱动包可以点击这里下载:数据库Jar包下载地址:http://pan.baidu.com/s/1jGKEEY6 密码:okq0
1、Hibernate连接MySQL数据库
1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <!-- Generated by MyEclipse Hibernate Tools. --> 6 <hibernate-configuration> 7 <session-factory> 8 <!-- 配置JDBC连接属性 --> 9 <property name="myeclipse.connection.profile"> 10 com.mysql.jdbc.Driver 11 </property> 12 <property name="connection.url"> 13 jdbc:mysql://localhost:3306/basehibernate 14 </property> 15 <property name="connection.username">root</property> 16 <property name="connection.password">sa</property> 17 <property name="connection.driver_class"> 18 com.mysql.jdbc.Driver 19 </property> 20 <property name="dialect"> 21 org.hibernate.dialect.MySQLDialect 22 </property> 23 24 <!-- 自动建表 --> 25 <property name="hbm2ddl.auto">update</property> 26 <!-- 自动提交,不加的话可能会出现insert之后数据库无数据 --> 27 <property name="connection.autocommit">true</property> 28 29 <!-- 使用Hibernate Annotation的POJO类 --> 30 <mapping class="com.basehibernate.pojo.Department" /> 31 <mapping class="com.basehibernate.pojo.Employee" /> 32 </session-factory> 33 </hibernate-configuration>
2、Hibernate连接Oracle数据库
1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <!-- Generated by MyEclipse Hibernate Tools. --> 6 <hibernate-configuration> 7 <session-factory> 8 <!-- 配置JDBC连接属性 --> 9 <property name="connection.driver_class"> 10 oracle.jdbc.driver.OracleDriver 11 </property> 12 <property name="connection.url"> 13 jdbc:oracle:thin:@localhost:1521:chanshuyi 14 </property> 15 <property name="myeclipse.connection.profile">oracle</property> 16 <property name="connection.username">csy</property> 17 <property name="connection.password">csy</property> 18 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> 19 20 <!-- oracle特有的提交更改 --> 21 <property name="defaultAutoCommit">true</property> 22 <!-- 自动建表 --> 23 <property name="hbm2ddl.auto">auto</property> 24 <property name="show_sql">true</property> 25 <!-- 自动提交,不加的话可能会出现insert之后数据库无数据 --> 26 <property name="connection.autocommit">true</property> 27 28 <!-- 使用Hibernate Annotation的POJO类 --> 29 <mapping class="com.xinpinv.pojo.Product" /> 30 <mapping class="com.xinpinv.pojo.BitInfo" /> 31 </session-factory> 32 </hibernate-configuration>
3、Hibernate连接Oracle数据库
(MARK 待写)