1、内容: hibernate 也是一个经典的[数据访问中间件] 开源框架。
2、hibernate核心组件
SessionFactory[整个数据的操作]重量级组件
Session[对数据库的一次业务操作] 轻量级
3、ORM(对象关系映射): 是一种数据访问层 解决方案、用它开源很好的移植到不同数据库平台。
通过 对象模型 操作 数据库关系模型
4、hibernate配置
配置SessionFactory
开发大致步骤:
(参考文档路径)\hibernate-distribution-3.6.8.Final-dist\hibernate-distribution-3.6.8.Final\documentation\manual\zh-CN\pdf
(配置文件路径)
hibernate3.jar\org\hibernate - ZIP 压缩文件, 解包大小为 9,174,527 字节
1、配置 hibernate.cfg.xml

<?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>
<!--Database connection settings -->
<!--连接数据库-->
<property name="connection.driver_class"
>oracle.jdbc.driver.OracleDriver</property>
<!--连接URL-->
<property name="connection.url"
>jdbc:oracle:thin:@localhost:1521:orcl</property>
<!--帐号-->
<property name="connection.username"
>scott</property>
<!--密码-->
<property name="connection.password"
>tiger</property>
<!--SQL dialect 方言(hiernate可以将对对象模型的操作转为对oracle实际的SQL)-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--Enable Hibernate's automatic session context management -->
<!--将通过当前sessionFactory得到的session会被绑定到当前的线程-提高性能-->
<property name="current_session_context_class">thread</property>
<!--Echo all executed SQL to stdout -->
<!--将hibernate转成的SQL语句-显示到控制台->
<property name="show_sql">true</property>
<mapping resource="com/it/bean/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
com/it/bean/UserInfo.hbm.xml
(文件路径)hibernate3.jar\org\hibernate - ZIP 压缩文件, 解包大小为 9,174,527 字节
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.it.bean">
<class name="UserInfo" table="userInfo">
<id name="user_id" column="user_id">
<!-- 主键生成策略 -->
<generator class="assigned"></generator>
</id>
<property name="user_pwd" column="user_pwd"></property>
<property name="user_sex" column="user_sex"></property>
2、创建SessionFactory
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
}
3、创建Session
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
}catch(Exception e){
e.printStackTrace();
}
}
}
4、创建Transaction(事务处理对象)
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
}catch(Exception e){
e.printStackTrace();
}
}
}
5、开启事务
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
}catch(Exception e){
e.printStackTrace();
}
}
}
6、提交事务
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
//提交
tx.commit();
}catch(Exception e){
e.printStackTrace();
}
}
}
7、执行操作
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
UserInfo u = new UserInfo("9989","sd","23");
//执行U对象 操作 保存
Session.save(u); -------添加
//session.delete(u); ------删除
//UserInfo u1 = (UserInfo)session.get(UserInfo.class,"1001"); ------查询
//System.out.println(u1.getUser_pwd());
//u1.setUser_sex("weinihaom?-sidashu"); ----修改
// Query query = session.createQuery("from UserInfo"); //query.list(); -------查询2
//提交
tx.commit();
}catch(Exception e){
e.printStackTrace();
}
}
}
8、异常处理块,事务回滚
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
UserInfo u = new UserInfo("9989","sd","23");
//执行U对象 操作 保存
Session.save(u);
//提交
tx.commit();
}catch(Exception e){
e.printStackTrace();
//事务回滚
tx.rollback();
}
}
}
封装---工具包
package com.it.dao.util;
/**
*单例模式
*/
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionFactoryUtils {
private static SessionFactory sessionFactory;
static{
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
基础包(BaseDAO)
package com.it.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.it.dao.util.SessionFactoryUtils;
public class BaseDAO<T>{
/**
*查询2
*/
public List<T> find(String hql,String...params){
Session session = null;
Transaction tx = null;
List<T> list = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
Query query =session.createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setString(i, params[i]);
}
list=query.list();
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
return list;
}
public List<T> find(String hql){
Session session = null;
Transaction tx = null;
List<T> list = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
Query query =session.createQuery(hql);
list=query.list();
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
return list;
}
public void add(Object o){
Session session = null;
Transaction tx = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.save(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
/**
*查询
*/
public T get(Class<T> clz,String OID){
Session session = null;
Transaction tx = null;
T t=null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
t=(T) session.get(clz, OID);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
return t;
}
/**
*删除
*/
public void delete(Class<T> clz,String OID){
Session session = null;
Transaction tx = null;
Object o =get(clz,OID);
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.delete(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
/**
*修改
*/
public void update(Object o){
Session session = null;
Transaction tx = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.update(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
public void delete(Object o){
Session session = null;
Transaction tx = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.delete(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
}
userDAO
package com.it.dao;
import java.util.List;
import com.it.bean.UserInfo;
public class UserDAO extends BaseDAO<UserInfo>{
/**
*查询
*/
public List<UserInfo> findUsers(UserInfo user){
String hql = "from UserInfo user where user.user_id like ? and user.user_pwd like ? and user.user_sex like ? ";
String []params={"%"+user.getUser_id()+"%","%"+user.getUser_pwd()+"%","%"+user.getUser_sex()+"%"};
return super.find(hql,params);
}
/**
*查询2
*/
public List<UserInfo> findAllUsers(){
String hql = "from UserInfo";
return super.find(hql);
}
public void add(UserInfo user){
super.add(user);
}
public void delete(String id){
super.delete(UserInfo.class, id);
}
public void delete(UserInfo user){
super.delete(user);
}
public UserInfo get(String user_id){
return super.get(UserInfo.class,user_id);
}
}
package com.it.test;
import java.util.List;
import com.it.bean.UserInfo;
import com.it.dao.BaseDAO;
import com.it.dao.UserDAO;
public class Test {
public static void main(String[] args) {
UserDAO dao = new UserDAO();
//UserInfo u = new UserInfo("1","1","1");
//UserInfo u = new UserInfo("1","1","");
List<UserInfo> users = dao.findUsers(u);
for (UserInfo user : users) {
System.out.println(user.getUser_pwd());
}
}
}
- 三大框架之hibernate入门
hibernate入门 1.orm hibernate是一个经典的开源的orm[数据访问中间件]框架 ORM( Object Relation Mapping)对象关 ...
- .net core 基于Dapper 的分库分表开源框架(core-data)
一.前言 感觉很久没写文章了,最近也比较忙,写的相对比较少,抽空分享基于Dapper 的分库分表开源框架core-data的强大功能,更好的提高开发过程中的效率: 在数据库的数据日积月累的积累下,业务 ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图的数据操作
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图实体类映射 下一篇:Farseer.net轻量级ORM开源 ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:表的数据操作
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射 下一篇:Farseer.net轻量级ORM开源框 ...
- Farseer.net轻量级开源框架 入门篇:添加数据详解
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 分类逻辑层 下一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 ...
- Farseer.net轻量级开源框架 入门篇:修改数据详解
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 ...
- Farseer.net轻量级开源框架 入门篇:删除数据详解
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 ...
- Farseer.net轻量级开源框架 入门篇:查询数据详解
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: Where条 ...
- PDF.NET SOD 开源框架红包派送活动 && 新手快速入门指引
一.框架的由来 快速入门 有关框架的更多信息,请看框架官方主页! 本套框架的思想是借鉴Java平台的Hibernate 和 iBatis 而来,兼有ORM和SQL-MAP的特性,同时还参考了后来.N ...
随机推荐
- timus_1007_bfs
图像编码 题目描述: 有这样一副图,它有黑白像素,黑像素的坐标在1-10之间.有很多种方法来编码这个图.例如下面的图: 一种表示方法是只描述黑像素,并按x坐标的增序描述,如果x相同,则按y的增序描述, ...
- Java多线程系列--“JUC集合”06之 ConcurrentSkipListSet
概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...
- Docker的学习--介绍和安装
什么是 Docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Lin ...
- [python]进阶学习之阅读代码
起因 最近在公司的任务是写一些简单的运营工具,因为是很小的工具,所以就用了github上面的一个开源项目flask-admin,可以省去很多的事情. 但是,这个开源项目是个人维护的项目,所以文档相对简 ...
- Node.js系列基础学习----安装,实现Hello World, REPL
Node.js基础学习 简介 简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台.Node.js是一 ...
- 简单的描述Java中的构造函数,及访问修饰符
作为一个Java 初学者,对Java的理解可能有些片面,甚至有些错误的理解,对于观看此处的您,希望您选择性观看!!! 访问修饰符: 1.常用访问修饰符: public 共有的 private 私有的 ...
- ORLEANS REMOTE DEPLOYMENT
Orleans Remote Deployment Table of Contents Overview: 1 Prerequisites. 2 Deployment Steps. 2 Orleans ...
- jquery的promise实践--连续加载图片
在javascript设计模式实践之代理模式--图片预加载中用代理模式实现了图片预加载功能. 现在就更进一步,完成一个能够一张一张的连续图片加载的功能. 功能: 1.一张一张加载图片. 2.加载错误, ...
- The Web server is configured to not list the contents of this directory.
部署一个ASP.NET MVC网站至一个全新的服务器Windows Server 2008 R2, 数据为MS SQL Server 2014 64bit Expression版本. 运行时,它第一次 ...
- 基于C#的MongoDB数据库开发应用(3)--MongoDB数据库的C#开发之异步接口
在前面的系列博客中,我曾经介绍过,MongoDB数据库的C#驱动已经全面支持异步的处理接口,并且接口的定义几乎是重写了.本篇主要介绍MongoDB数据库的C#驱动的最新接口使用,介绍基于新接口如何实现 ...