Hibernate5.x 基本开发
来源:互联网 发布:知乎 避孕套 父母 编辑:程序博客网 时间:2024/06/14 21:36
简介:
Hibernate 不仅仅管理Java类到数据库表的映射(包括Java数据类型到SQL数据类型的映射),还提供数据查询和获取数据的方法,可以大幅度减少开发时人工使用SQL和JDBC处理数据的时间。
快速配置
(1)导包
部分包在下载的hibernate/lib/required 中(要全导)
下面几个包无或出现错误,可以看:http://blog.csdn.net/fanfan4569/article/details/53043835
(2)总src目录
(3)User.java(开发持久化类,可由POJP+持久化注解组成)
运用注解可以不用下面的User.hbm.xml,而上面User.java只是POJO
(4)hibernate.cfg.xml
(5)User.hbm.xml
(6)HibernateDemo.java
可以写个工具类来方便初始化session
(7)成功效果图
详解过程(对应HibernateDemo.java)
为了使用Hibernate进行持久化操作,通常有如下操作步骤。
①开发持久类
②获取Configuration
③获取SessionFactory
④获取Session,打开事务
⑤用面向对象的方式操作数据库
⑥关闭事务,关闭session
PO与Session的关联关系,PO可有如下三种状态:
(1)瞬态:如果PO实例从未与Session关联过,该PO实例处于瞬态状态
(2)持久化:如果PO实例与Session关联起来,且该实例对应到数据库记录,则该实例处于持久化状态。
(3)脱管:如果PO实例曾经与Session关联过,但因为Session的关闭等原因,PO实例脱离了Session的管理,这种状态被称为脱管状态。
HibernateUtils
//这个项目是使用这个的(但会提示过时了,对于Hibernate5.x)package utils;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * Created by Donald on 2016/11/5. */public class HibernateUtils { private static final Configuration cfg; private static final SessionFactory sessionFactory; //静态代码块实现 static{ //加载核心配置文件 cfg = new Configuration().configure(); sessionFactory = cfg.buildSessionFactory(); } //提供返回本地线程绑的session的方法 public static Session getSessionObject(){ return sessionFactory.getCurrentSession(); } //提供方法返回sessionFactory public static SessionFactory getSessionFactory(){ return sessionFactory; } public static void main(String[] args) { }}
这个是做大作业时候写的(可使用于Hibernate5.X,不提示过时)
//只写了部分,其他参照即可package utils;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.query.Query;final public class HibernateUtil { private static SessionFactory sessionFactory=null; private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>(); private HibernateUtil(){}; static { sessionFactory=new Configuration().configure().buildSessionFactory(); } public static Session openSession(){ return sessionFactory.openSession(); } public static Session getCurrentSession(){ Session session=threadLocal.get(); if(session==null){ session=sessionFactory.openSession(); threadLocal.set(session); } return session; } public static void closeCurrentSession(){ Session s=getCurrentSession(); if(s!=null&& s.isOpen() ){ s.close(); threadLocal.set(null); } } /** * 查找单一 * @param hql * @param parameters * @return */ public static Object uniqueQuery(String hql,String [] parameters){ Session s = null; Object obj = null; try { s = openSession(); Query query = s.createQuery(hql); //先判断是否有参数要绑定 if(parameters != null && parameters.length > 0){ for(int i=0 ; i < parameters.length; i++){ query.setParameter(i, parameters[i]); } } /** * 若使用query.getSingleResult() * 如果查询到的object为null 则会直接异常 */ obj = query.uniqueResult(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); }finally{ if(s!=null&&s.isOpen()){ s.close(); } } return obj; } public static List executeQuery(String hql,String [] parameters){ Session s = null; List list = null; try { s = openSession(); Query query = s.createQuery(hql); //先判断是否有参数要绑定 if(parameters!=null&& parameters.length>0){ for(int i=0;i<parameters.length;i++){ query.setParameter(i, parameters[i]); } } list = query.list(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); // TODO: handle exception }finally{ if(s!=null&&s.isOpen()){ s.close(); } } return list; }}
0 0
- Hibernate5.x 基本开发
- hibernate5.X学习笔记
- Hibernate5.x.x创建SessionFactory
- struts2+spring4.x+hibernate5.x
- hibernate5.x.x 使用mapping resource 不起作用
- Hibernate5
- Hibernate5
- Hibernate5
- Hibernate5.x 使用SchemaExport生成数据表
- Hibernate5.x获取SessionFactory的方法
- Hibernate5.x的相关注解解释。
- Hibernate5.x动态表名实现
- Hibernate5的hibernate.cfg.xml基本配置
- Hibernate5教程之一基本使用详解
- Spring4.X整合hibernate5.X之事物管理
- 快速搭建hibernate5.0.9的开发环境
- Hibernate5联合主键的注解式开发
- hibernate5.x 版本中创建SessionFactory的思路与方法
- PHP Socket实现HTTP GET POST请求发送
- zepto延迟300s
- makefile中"模式规则"的引入和介绍------%:%.cpp
- firstc程序讲解
- js 实现排序算法
- Hibernate5.x 基本开发
- 启动tomcat出错:A child container failed during start
- ubuntu下opencv的IDE——QT
- kaggle比赛集成指南
- Alsa 驱动分析
- Java 下高效的反射工具包 ReflectASM 使用例解
- 如何清除Xcode8打印的系统日志
- 51nod 1449 砝码称重
- Linux启动过程