Hibernate-----核心配置文件、映射文件

主要讲解核心配置文件和映射文件的编写。

代码在文末

   --------------------------------------------------------------------------------------------------------------

1.核心配置文件

两种方式配置:

Hibernate-----核心配置文件、映射文件_第1张图片

Hibernate-----核心配置文件、映射文件_第2张图片


Hibernate-----核心配置文件、映射文件_第3张图片

Hibernate-----核心配置文件、映射文件_第4张图片

Hibernate-----核心配置文件、映射文件_第5张图片



下面分别是上面3个可选配置的具体应用:

1.1 控制台打印

一般在写程序的时候多数会用到主要为了测试sql语句是否正常生成。

为了显示sql更清楚,可以使用格式化显示。

Hibernate-----核心配置文件、映射文件_第6张图片

Hibernate-----核心配置文件、映射文件_第7张图片


1.2 整合C3P0连接池

不在使用默认的hibernate的连接池,而是自己进行设置。


Hibernate-----核心配置文件、映射文件_第8张图片


1.3 自动建表

该属性有4个参数。

Hibernate-----核心配置文件、映射文件_第9张图片


Hibernate-----核心配置文件、映射文件_第10张图片

一般用于测试;


Hibernate-----核心配置文件、映射文件_第11张图片

create基本不会被使用;


所以一般常用的就是update。

如果你对javabean进行了更改,增加一个属性,但是该属性在表中的列不存在。当你设置update后,一运行。

在数据库的表中会自动出现一个你新设置的列。

如果你修改了表的某些结构,当你运行出现不一致的时候,也会自动的修改表。

注意:他会检查表和类的结构是否一致,如果不一致会更新表的结构。而不是更新我们的javabean。


validate:用于验证数据库的表和javabean是否一致,如果不一致会报错。如图。


Hibernate-----核心配置文件、映射文件_第12张图片

2 配置文件(.hbm.xml)

主要是3个字段:class、id、column、

Hibernate-----核心配置文件、映射文件_第13张图片



Hibernate-----核心配置文件、映射文件_第14张图片

Hibernate-----核心配置文件、映射文件_第15张图片


Hibernate-----核心配置文件、映射文件_第16张图片



    ----------------------------------------------------------------------------------------------------------------------------

代码:

转载自:https://blog.csdn.net/wangchuanqi1234/article/details/51131285

Hibernate配置文件有两种形式:XML与properties 

个人建议使用XML,因为properties中不能配置关联的映射文件,在后续的实现中会带来一些没必要的编码;

XML(hibernate.cfg.xml)文件详解:

[html]  view plain  copy
  1. xml version="1.0" encoding="GBK"?>  
  2.   
  3.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.           
  9.         <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>  
  10.           
  11.         <property name="connection.url">jdbc:mysql://localhost/数据库名property>  
  12.           
  13.         <property name="connection.username">rootproperty>  
  14.           
  15.         <property name="connection.password">32147property>  
  16.           
  17.         <property name="hibernate.c3p0.max_size">20property>  
  18.           
  19.         <property name="hibernate.c3p0.min_size">1property>  
  20.           
  21.         <property name="hibernate.c3p0.timeout">5000property>  
  22.           
  23.         <property name="hibernate.c3p0.max_statements">100property>  
  24.         <property name="hibernate.c3p0.idle_test_period">3000property>  
  25.         <property name="hibernate.c3p0.acquire_increment">2property>  
  26.         <property name="hibernate.c3p0.validate">trueproperty>  
  27.           
  28.         <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialectproperty>  
  29.           
  30.         <property name="hbm2ddl.auto">updateproperty>  
  31.           
  32.         <property name="show_sql">trueproperty>  
  33.           
  34.         <property name="hibernate.format_sql">trueproperty>  
  35.           
  36.         <mapping resource="映射文件路径/News.hbm.xml"/>  
  37.     session-factory>  
  38. hibernate-configuration>  

properties(hibernate.properties)文件详解

[html]  view plain  copy
  1. ## MySQL  
  2. #方言  
  3. hibernate.dialect org.hibernate.dialect.MySQLDialect  
  4. hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect  
  5. hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect  
  6. #驱动  
  7. hibernate.connection.driver_class com.mysql.jdbc.Driver  
  8. #数据库地址  
  9. hibernate.connection.url jdbc:mysql://127.0.0.1/datdabseName  
  10. #用户名  
  11. hibernate.connection.username root  
  12. #密码  
  13. hibernate.connection.password 12345  
  14. #是否在控制台输出sql语句  
  15. hibernate.show_sql true/false  
  16. #设置当创建sessionfactory时,是否根据映射文件自动建立数据库表。 create-drop:表示关闭sessionFactory时,将drop刚建的数据库表。该属性可以是update/create-drop/create  
  17. hibernate.hbm2ddl.auto update/create-drop/create  
  18.   
  19. ###########################  
  20. ### C3P0 Connection Pool C3P0连接池###  
  21. ###########################  
  22. #连接池最大链接数  
  23. hibernate.c3p0.max_size 2  
  24. #连接池最小连接数  
  25. hibernate.c3p0.min_size 2  
  26. #连接池连接的超时时长  
  27. hibernate.c3p0.timeout 5000  
  28. #缓存statements 的数量  
  29. hibernate.c3p0.max_statements 100  
  30. hibernate.c3p0.idle_test_period 3000  
  31. hibernate.c3p0.acquire_increment 2  
  32. hibernate.c3p0.validate true/false  
  33.   
  34.   
  35. ############  
  36. ### JNDI (java naming directory interface)Java命名目录接口###  
  37. ###当无需hibernate自己管理数据源而是直接访问容器管理数据源 使用JNDI  
  38. ############  
  39. #指定数据源JNDI名字  
  40. hibernate.connection.datasource dddd  
  41. #文件系统下  
  42. hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory  
  43. hibernate.jndi.url file:/  
  44.   
  45. #网络  
  46. #指定JND InitialContextFactory 的实现类,该属性也是可选的。如果JNDI与Hibernate持久化访问的代码处于同一个应用,无需指定该属性  
  47. hibernate.jndi.class com.ibm.websphere.naming.WsnInitialContextFactory  
  48. #指定JNDI提供者的URL,该属性可选 如果JNDI与Hibernate持久化访问的代码处于同一个应用,无需指定该属性  
  49. hibernate.jndi.url iiop://localhost:900/  
  50.   
  51. #指定链接数据库用户名  
  52. hibernate.connection.username  root  
  53. #指定密码  
  54. hibernate.connection.password  1111  
  55. #指定方言  
  56. hibernate.dialect org.hibernate.dialect.MySQLDialect  
  57.   
  58. #######################  
  59. ### Transaction API   事务属性说明###  
  60. #######################  
  61.   
  62. #指定是否在事务结束后自动关闭session   
  63. hibernate.transaction.auto_close_session true/false  
  64. #指定session是否在事务完成后自动将数据刷新到底层数据库  
  65. hibernate.transaction.flush_before_completion true/false  
  66.   
  67. ## 指定hibernate所有的事务工厂的类型,该属性必须是TransactionFactory的直接或间接子类  
  68.   
  69. hibernate.transaction.factory_class org.hibernate.transaction.JTATransactionFactory  
  70. hibernate.transaction.factory_class org.hibernate.transaction.JDBCTransactionFactory  
  71.   
  72. ## 该属性值是一个JNDI名,hibernate将使用JTATTransactionFactory从应用服务器中取出JTAYserTransaction  
  73.   
  74. jta.UserTransaction jta/usertransaction  
  75. jta.UserTransaction javax.transaction.UserTransaction  
  76. jta.UserTransaction UserTransaction  
  77.   
  78. ## 该属性值为一个transactionManagerLookup类名,当使用JVM级别的缓存时,或在JTA环境中使用hilo生成器策略时,需要该类  
  79.   
  80. hibernate.transaction.manager_lookup_class org.hibernate.transaction.JBossTransactionManagerLookup  
  81. hibernate.transaction.manager_lookup_class org.hibernate.transaction.WeblogicTransactionManagerLookup  
  82. hibernate.transaction.manager_lookup_class org.hibernate.transaction.WebSphereTransactionManagerLookup  
  83. hibernate.transaction.manager_lookup_class org.hibernate.transaction.OrionTransactionManagerLookup  
  84. hibernate.transaction.manager_lookup_class org.hibernate.transaction.ResinTransactionManagerLookup  

二、映射文件详解

由于篇幅,这里值详细对最基础配置进行说明

[html]  view plain  copy
  1. xml version="1.0"?>  
  2.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">      

你可能感兴趣的:(Hibernate)