好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Hibernate连接三种数据库的配置文件

hibernate连接数据库的配置文件为hibernate.cfg.xml,下面列出了连接三种数据库(sql server、oracle、mysql)时,hibernate.cfg.xml的必要配置。

连接mysql的配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<?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" >

<hibernate-configuration>

   <session-factory>

     <!-- 配置数据库驱动 -->

     <property name= "connection.driver_class" >

       com.mysql.jdbc.driver

     </property>

     <!-- 配置数据库连接url -->

     <property name= "connection.url" >

       jdbc:mysql: //localhost:3306/mysqldb

     </property>

     <!-- 数据库user -->

     <property name= "connection.username" >root</property>

     <!-- 数据库user密码 -->

     <property name= "connection.password" >admin</property>

     <!-- 配置jdbc内置连接池 -->

     <property name= "connection.pool_size" > 1 </property>

     <!-- 配置数据库方言 -->

     <property name= "dialect" >

       org.hibernate.dialect.mysqldialect

     </property>

     <!-- 输出运行时生成的sql语句 -->

     <property name= "show_sql" > true </property>

     <!-- 配置对输出的sql语句进行格式化 -->

     <property name= "format_sql" > true </property>

     <!-- 配置映射文件 -->

     <mapping resource= "com/model/user.hbm.xml" />

   </session-factory>

</hibernate-configuration>

连接oracle的配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<?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" >

<hibernate-configuration>

   <session-factory>

     <!-- 配置数据库的驱动类 -->

     <property name= "connection.driver_class" >

       oracle.jdbc.driver.oracledriver

     </property>

     <!-- 配置数据库的连接路径 -->

     <property name= "connection.url" >

       jdbc:oracle:thin: @127 .0. 0.1 : 1521 :dbsql

     </property>

     <!-- 配置数据库的连接用户名 -->

     <property name= "connection.username" >personnel_manage</property>

     <!-- 配置数据库的连接密码,密码为空时也可以省略该行配置代码 -->

     <property name= "connection.password" >mwq</property>

     <!-- 配置数据库使用的方言 -->

     <property name= "dialect" >

       org.hibernate.dialect.oracledialect

     </property>

     <!-- 配置在控制台显示sql语句 -->

     <property name= "show_sql" > true </property>

     <!-- 配置对输出的sql语句进行格式化 -->

     <property name= "format_sql" > true </property>

     <!-- 配置在输出的sql语句前面添加提示信息 -->

     <property name= "use_sql_comments" > true </property>

     <!-- 配置持久化类映射文件 -->

     <mapping resource= "com/chen/entity/users.hbm.xml" />

   </session-factory>

</hibernate-configuration>

连接sql server配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?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" >

  <hibernate-configuration>

    <session-factory>

    <!--配置数据库jdbc驱动-->

    <property name= "hibernate.connection.url" >jdbc:microsoft:sqlserver: //127.0.0.1:1433;databasename=db_manpower</property>

    <!--配置数据库连接url-->

    <property name= "hibernate.connection.driver_class" >com.microsoft.jdbc.sqlserver.sqlserverdriver</property>

    <!--配置数据库用户名-->

    <property name= "hibernate.connection.username" >sa</property>

    <!--配置数据库密码-->

    <property name= "hibernate.connection.password" />

    <!--输出运行时生成的sql语句-->

    <property name= "show_sql" >ture</property>

    <!--配置数据库方言-->

    <property name= "hibernate.dialect" >org.hibernate.dialect.sqlserverdialect</property>

    <!--配置连接池个数-->

    <property name= "hibernate.jdbc.batch_size" > 16 </property>

    <!--列出映射文件-->

    <mapping resource= "com/chen/entity/users.hbm.xml" />

    </session-factory>

  </hibernate-configuration>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

查看更多关于Hibernate连接三种数据库的配置文件的详细内容...

  阅读:10次