分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

hibernate.zzh

发布时间:2023-09-06 02:13责任编辑:郭大石关键词:暂无标签

1、hibernate是什么

框架是什么:

1、框架是用来提高开发效率的
2、封装好了一些功能,我们需要使用这些功能时,调用即可,不需要再手动实现
3、框架可以理解为一个半成品得项目,只要懂得如何驾驭这些功能即可

2、hibernate框架是什么,dao层框架

3、hibernate的好处

操作数据库的时候,可以以面向对象的方式来完成,不需要书写sql语句(hibernate十款orm框架)

ORM:利用描述对象和数据库表之间映射的元数据,自动把java应用程序中的对象,持久化到关系型数据库的表中。

4、orm分四级

1、dbutils属于1级
2、mybatis数据2级
4、hibernate数据4级:完全面向对象操作数据库

3、hibernate框架的构建

导包——创建数据库,准备表——书写orm元数据(对象与表的映射配置文件)——书写主配置文件——书写代码测试导包:

创建表:

书写orm元数据:①导入约束②实体③orm元数据

①导入约束


②实体③orm元数据


配置ORM元数据

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC ????"-//Hibernate/Hibernate Mapping DTD 3.0//EN" ???"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> ???<!-- 配置表与实体对象的关系 --> ???<!-- package属性:填写一个包名,在元素内部凡是需要书写完整类名的属性,可以直接输入类名 --><hibernate-mapping package="hibernate_day01.domain"> ???<!-- ????class元素:配置实体与表的对应关系 ???????name:完整类名 ???????table:数据库表名 ?????--> ???<class name="Customer" table="cst_customer"> ???????<!-- ????????????id元素:配置主键映射的属性 ???????????name:填写主键对应属性名 ???????????column:填下表中的主键列名 ????????--> ???????<id name="cust_id" column="cust_id"> ???????????<!-- generator:主键生成策略 --> ???????????<generator ></generator> ???????</id> ???????<!-- property元素:除id以外的普通属性映射 ???????????name:填写属性名 ???????????column:填写列名,默认是name的属性值 ???????????not-null:配置该属性是否不能为空,默认值为false ???????????length:配置数据库中列的长度,默认值:使用数据库类型的最大长度 ????????--> ???????<property name="cust_name" column="cust_name"></property> ???????<property name="cust_source" column="cust_source"></property> ???????<property name="cust_industry" column="cust_industry"></property> ???????<property name="cust_level" column="cust_level"></property> ???????<property name="cust_phone" column="cust_phone"></property> ???????<property name="cust_mobile" column="cust_mobile"></property> ???</class></hibernate-mapping>
36
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE hibernate-mapping PUBLIC 
3
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5
  <!-- 配置表与实体对象的关系 -->
6
  <!-- package属性:填写一个包名,在元素内部凡是需要书写完整类名的属性,可以直接输入类名 -->
7
<hibernate-mapping package="hibernate_day01.domain">
8
  <!--
9
   class元素:配置实体与表的对应关系
10
    name:完整类名
11
    table:数据库表名 
12
   -->
13
  <class name="Customer" table="cst_customer">
14
    <!-- 
15
      id元素:配置主键映射的属性
16
      name:填写主键对应属性名
17
      column:填下表中的主键列名
18
     -->
19
    <id name="cust_id" column="cust_id">
20
      <!-- generator:主键生成策略 -->
21
      <generator ></generator>
22
    </id>
23
    <!-- property元素:除id以外的普通属性映射
24
      name:填写属性名
25
      column:填写列名,默认是name的属性值
26
      not-null:配置该属性是否不能为空,默认值为false
27
      length:配置数据库中列的长度,默认值:使用数据库类型的最大长度
28
     -->
29
    <property name="cust_name" column="cust_name"></property>
30
    <property name="cust_source" column="cust_source"></property>
31
    <property name="cust_industry" column="cust_industry"></property>
32
    <property name="cust_level" column="cust_level"></property>
33
    <property name="cust_phone" column="cust_phone"></property>
34
    <property name="cust_mobile" column="cust_mobile"></property>
35
  </class>
36
</hibernate-mapping>

hibernate主配置文件(位于src下)

必选属性配置(5个):
1、数据库驱动
2、数据库url
3、数据库连接用户名
4、数据库连接密码
5、数据库方言:不同的数据库,比如Oracle,MSSQL,MySQL,它们的SQL会有少量的差别,内置函数也会有点不同,比如limit,在Oracle里面就不能用,
可选属性配置:
元数据引入配置:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC ???"-//Hibernate/Hibernate Configuration DTD 3.0//EN" ???"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> ???<session-factory> ???????<!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect ???????#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect ???????#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect ???????#hibernate.connection.driver_class com.mysql.jdbc.Driver ???????#hibernate.connection.url jdbc:mysql:///test ???????#hibernate.connection.username gavin ???????#hibernate.connection.password --> ???????????????<!-- 数据库驱动 --> ???????<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ???????<!-- 数据库名 --> ???????<property name="hibernate.connection.url">jdbc:mysql:///hibernate_01</property> ???????<!-- 数据库连接用户名 --> ???????<property name="hibernate.connection.username">root</property> ???????<!-- 数据库连接密码 --> ???????<property name="hibernate.connection.password">123</property> ???????<!-- 数据库方言 ????????????不同的数据库中,sql语法略有区别,指定方言可以让hibernate框架在生成sql语句时,针对数据库的方言生成 ???????????sql99标准:DDL 定义语言 库表的增删改查 ????????????????????DCL 控制语言 事务权限 ????????????????????DML 操纵语言 增删改查 ???????????注意:mysql选择方言时,选择最短的数据库方言 ???????????????????--> ???????<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> ???????????????????????<!-- #hibernate.show_sql true ???????????#hibernate.format_sql true --> ???????<!-- 将hibernate生成的sql语句打印到控制台 --> ???????<property name="hibernate.show_sql">true</property> ???????<!-- 将hibernate生成的sql语句格式化(语法缩进) --> ???????<property name="hibernate.format_sql">true</property> ???????<!-- auto schema export 自动导出表结构 自动建表 --> ???????<!-- ???????????#hibernate.hbm2ddl.auto create-drop 自动建表 每次框架运行结束都会删除所有表 ???????#hibernate.hbm2ddl.auto create 自动建表 每次框架运行都会创建新的表,以前表将会被覆盖,数据会丢失 ???????#hibernate.hbm2ddl.auto update(推荐使用) 自动生成表,如果已存在不会再生成,如果表有改动,则执行改动 ???????#hibernate.hbm2ddl.auto validate 校验,不自动生成,每次启动会校验数据库表是否正确。校验失败--> ???????<property name="hibernate.hbm2ddl.auto"></property> ???????<!-- 引入orm元数据 ???????????路径书写:填写src下的路径 ????????--> ???????<mapping resource="hibernate_day01/domain/Customer.hbm.xml"/> ???????<!-- 指定hibernate操作数据库时的隔离级别 ???????????#hibernate.connection.isolation 1|2|4|8 ???????????0001 1 读未提交 ???????????0010 2 读已提交 ???????????0100 4 可重复读 ???????????1000 8 串行化 ????????--> ????????<property name="hibernate.connection.isolation">4</property> ???</session-factory></hibernate-configuration>
59
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
<hibernate-configuration>
6
  <session-factory>
7
    <!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect
8
    #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
9
    #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
10
    #hibernate.connection.driver_class com.mysql.jdbc.Driver
11
    #hibernate.connection.url jdbc:mysql:///test
12
    #hibernate.connection.username gavin
13
    #hibernate.connection.password -->
14
    
15
    <!-- 数据库驱动 -->
16
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
17
    <!-- 数据库名 -->
18
    <property name="hibernate.connection.url">jdbc:mysql:///hibernate_01</property>
19
    <!-- 数据库连接用户名 -->
20
    <property name="hibernate.connection.username">root</property>
21
    <!-- 数据库连接密码 -->
22
    <property name="hibernate.connection.password">123</property>
23
    <!-- 数据库方言 
24
      不同的数据库中,sql语法略有区别,指定方言可以让hibernate框架在生成sql语句时,针对数据库的方言生成
25
      sql99标准:DDL 定义语言 库表的增删改查
26
           DCL 控制语言 事务权限
27
           DML 操纵语言 增删改查
28
      注意:mysql选择方言时,选择最短的数据库方言      
29
    -->
30
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
31
    
32
    
33
    <!-- #hibernate.show_sql true
34
      #hibernate.format_sql true -->
35
    <!-- 将hibernate生成的sql语句打印到控制台 -->
36
    <property name="hibernate.show_sql">true</property>
37
    <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
38
    <property name="hibernate.format_sql">true</property>
39
    <!-- auto schema export 自动导出表结构 自动建表 -->
40
    <!--  
41
    #hibernate.hbm2ddl.auto create-drop 自动建表 每次框架运行结束都会删除所有表
42
    #hibernate.hbm2ddl.auto create 自动建表 每次框架运行都会创建新的表,以前表将会被覆盖,数据会丢失
43
    #hibernate.hbm2ddl.auto update(推荐使用) 自动生成表,如果已存在不会再生成,如果表有改动,则执行改动
44
    #hibernate.hbm2ddl.auto validate 校验,不自动生成,每次启动会校验数据库表是否正确。校验失败-->
45
	
	
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved