前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Object Relational Mapping框架之Hibernate

Object Relational Mapping框架之Hibernate

作者头像
赵小忠
发布2018-01-24 19:03:55
7520
发布2018-01-24 19:03:55
举报
文章被收录于专栏:禁心尽力禁心尽力

hibernate框架简介:

  hibernate框架就是开发中在持久层中应用居多的ORM框架,它对JDBC做了轻量级的封装。

什么是ORM:Object Relational Mapping(对象关系映射),也就是将java中的对象和数据库中的数据表建立映射关系,当我们在操作java中的对象时就可以操作数据库中的表了。

使用hibernate框架开发流程:

hibernate开发需求创建俩个配置文件,一个是映射文件,另一个hibernate的框架配置文件。

创建映射文件,该映射文件一般命名规则是(类名.hbm.xml)。

  1. class标签:建立java类和关系数据表的映射。
  • name:类的全路径。
  • table:数据库中被建立映射的表名称,如果类名和数据库名一致该属性可以省略。
  • catalog:数据库名称。
  1. id标签:建立主键和java类中属性的映射。
  • name:与表中的主键对应的类中的属性名称。
  • column:表中的主键名。
  • length:字段的长度。
  • type:字段的数据类型。
  1. property标签:建立关系表中普通字段和java类中属性的映射。
  • name:与表中除了主键对应的类中的属性名称。
  • column:表中除了主键外的普通字段名称。
  • length:字段的长度。
  • type:字段的数据类型。
  • not-null:非空。
  • unique:唯一。

创建hibernate核心配置文件(2种:hibernate.properties----不能加载映射文件 hibernate.cfg.xml----结构清晰【通常使用这一种】)。

核心配置文件中的内容:

  • 数据库连接的基本参数:
  • hibernate的一些基本属性:
  • 加载映射文件:
代码语言:js
复制
 1 <?xml version="1.0" encoding="UTF-8"?>  2 <!DOCTYPE hibernate-configuration PUBLIC  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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 10         <property name="hibernate.connection.url">jdbc:mysql:///hibernate_crm</property> 11         <property name="hibernate.connection.username">root</property> 12         <property name="hibernate.connection.password">123</property> 13          14         <!-- 配置C3P0连接池: --> 15         <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 16         <!--在连接池中可用的数据库连接的最少数目 --> 17         <property name="c3p0.min_size">5</property> 18         <!--在连接池中所有数据库连接的最大数目  --> 19         <property name="c3p0.max_size">20</property> 20         <!--设定数据库连接的过期时间,以秒为单位,如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 --> 21         <property name="c3p0.timeout">120</property> 22          <!--每3000秒检查所有连接池中的空闲连接 以秒为单位--> 23         <property name="c3p0.idle_test_period">3000</property> 24          25         <!--  26             配置数据库事务的隔离级别: 27             1、read uncommitted (1)  : 脏读,可重复读,虚读三种情况都有可能发生。 28             2、read committed (2)    : 避免脏读,可重复读和虚读有可能发生。 29             3、repeatable read  (4)  : 避免脏读和可重复读,但是虚读有可能发生。 30             4、serializable  (8)     :  以上出现的情况都能解决。 31          --> 32          <property name="hibernate.connection.isolation">4</property> 33           34          <!-- hibernate中设置可以使用与当前线程绑定的session连接对象: --> 35          <property name="hibernate.current_session_context_class">thread</property> 36           37          38         <!-- hibernate的相关属性配置: --> 39         <!-- 必须配置:mysql的方言配置 --> 40         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 41         <!-- 可选配置:显示sql、格式化sql、hbm2ddl等 --> 42         <property name="hibernate.show_sql">true</property> 43         <property name="hibernate.format_sql">true</property> 44         <property name="hibernate.hbm2ddl.auto">update</property> 45          46         <!-- 加载映射文件: --> 47         <mapping resource="com/itheima/domain/Customer.hbm.xml"/> 48         <mapping resource="com/itheima/domain/LinkMan.hbm.xml"/> 49         <mapping resource="com/itheima/domain/User.hbm.xml"/> 50         <mapping resource="com/itheima/domain/Role.hbm.xml"/> 51          52     </session-factory> 53 </hibernate-configuration>
  
 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档