分享web开发知识

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

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

hibernate

发布时间:2023-09-06 01:09责任编辑:顾先生关键词:暂无标签

Hibernate体系结构

Hibernate架构包括许多对象持久对象,会话工厂,事务工厂,连接工厂,会话,事务等。

hibernate架构中有4层Java应用层,hibernate框架层,反手api层和数据库层。

Hibernate体系结构的要素

要创建第一个hibernate应用程序,我们必须知道Hibernate架构的元素。 它们如下:

会话工厂(SessionFactory)

SessionFactoryConnectionProvider的会话和客户端工厂。 它拥有数据的二级缓存(可选)。org.hibernate.SessionFactory接口提供了工厂方法来获取Session的对象。

会话(Session)
Session对象提供应用程序和存储在数据库中的数据之间的接口。 它是一个短生命周期的对象并包装JDBC连接。 它是事务,查询和标准的工厂。 它拥有一级缓存(强制性)数据。 org.hibernate.Session接口提供插入,更新和删除对象的方法。 它还提供了事务,查询和标准的工厂方法。

事务(Transaction)
事务对象指定工作的原子单位,它是一个可选项。 org.hibernate.Transaction接口提供事务管理的方法。

连接提供者(ConnectionProvider)

它是一个JDBC连接工厂。 它从DriverManagerDataSource抽象出来的应用程序。 它是一个可选项。

事务工厂(TransactionFactory)

它是一个事务工厂,是一个可选项。

Hibernate快速入门:

1.项目结构如下:

1.

2.在对应的lib下引入相应的hibernate的jar包和依赖包,这里使用的是hibernate3,所需jar如图:

3.创建持久化类User类

package com.example.hibernate;import java.util.Date;public class User {private String id;private String username;private String password;private Date createTime;private Date expireTime;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String userName) {this.username = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getExpireTime() {return expireTime;}public void setExpireTime(Date expireTime) {this.expireTime = expireTime;}}

 4.创建持久化类的映射文件User.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC ????"-//Hibernate/Hibernate Mapping DTD 3.0//EN" ???"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> ???<hibernate-mapping> ???<class name="com.example.hibernate.User"> ???????<id name="id"> ???????????<generator class="uuid"/> ???????</id> ???????<property name="username"/> ???????<property name="password"/> ???????<property name="createTime"/> ???????<property name="expireTime"/> ???</class></hibernate-mapping>

5.创建对应的数据库驱动hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC ???"-//Hibernate/Hibernate Configuration DTD 3.0//EN" ???"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> ???<session-factory > ???????<!-- mysql数据库驱动 --> ???????<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ???????<!-- mysql数据库名称 --> ???????<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8</property> ???????<!-- 数据库的登陆用户名 --> ???????<property name="hibernate.connection.username">root</property> ???????<!-- 数据库的登陆密码 --> ???????<property name="hibernate.connection.password">123456</property> ???????<!-- 方言:为每一种数据库提供适配器,方便转换 --> ?????????<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> ??????????????????????<mapping resource="com/example/hibernate/User.hbm.xml"/> ???????????????</session-factory> ???????</hibernate-configuration>
注意:在使用mysql数据库时,由于设置了安全模式所以需要加上useSSL=false,解除安全模式,这样才能完成数据库的修改
如果需要将数据库连接更换为c3p0,dbcp等可以参照:http://blog.csdn.net/qiaqia609/article/details/9456489/
6.创建持久化类对应的数据库表ExoprtDB
package com.example.hibernate;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * 将hbm生成ddl * @author BCH * */public class ExoprtDB { ???public static void main(String[] args) { ???????//默认读取hibernate.cfg.xml文件 ???????Configuration cfr = new Configuration().configure(); ???????????????SchemaExport export = new SchemaExport(cfr); ???????export.create(true, true); ???}}

7.创建:Client 通过对象将对象的数据储存到数据库中

package com.example.hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Client { ???public static void main(String[] args) { ???????//读取配置文件 ???????Configuration cfg = new Configuration().configure(); ???????????????SessionFactory factory = cfg.buildSessionFactory(); ???????????????Session session = null; ???????try{ ???????????session = factory.openSession(); ???????????//开启事务 ???????????session.beginTransaction(); ???????????????????????User user = new User(); ???????????user.setUsername("用户名"); ???????????user.setPassword("123"); ???????????user.setCreateTime(new Date()); ???????????user.setExpireTime(new Date()); ???????????????????????session.save(user); ???????????//提交事务 ???????????session.getTransaction().commit(); ???????????????????}catch(Exception e){ ???????????e.printStackTrace(); ???????????//回滚事务 ???????????session.getTransaction().rollback(); ???????}finally{ ???????????if(session != null){ ???????????????if(session.isOpen()){ ???????????????????//关闭session ???????????????????session.close(); ???????????????} ???????????} ???????} ???}}

总结:在操作整个hibernate的过程中,基本没有操作JDBC,对于数据的存储都是直接操作对象实现的

映射关系:实体类对应数据库表的表,实体类的属性对应数据库表的字段

补充:

1.创建持久化类的一些原则;

(1):实现默认的无参构造方法

(2):提供一个标识

(3):为每个属性提供一个getter和setter方法

(4):最好不要用final来修饰类,因为使用了fianl关键字,那么延迟加载(load)将会无效

2.Hibernate的一些主键生成策略

User.hbm.xml配置文件中有一个标签:<generator>。它表示的是主键生成策略。主键生成策略必须配置,用来为该持久化类的实例生成唯一的标识。

  uuid: 用一个128-bit的UUID算法生成字符串类型的标识符,这在一个网络中是唯一的(使用了IP地址),UUID被编码为一个32位16进制数字的字符串,它的生成是由Hibernate生成,一般不会重复UUID包含:IP地址,JVM的启动时间,系统时间和一个计算器值。

 identity:MySql数据库中自增长

   Sequence:Oracle数据库自增长

    native:根据底层数据库能力选择identity、sequence中的一个。

   assigned:手动分配主键.<generator>没有指定时的默认生成策略

foreign:使用另外一个相关联的对象的标示符。通常和<one-to-one>配合使用

 

hibernate

原文地址:http://www.cnblogs.com/fly-boy/p/7484206.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved