hibernate系列三:hibernate工具类及使用
来源:互联网 发布:mac文件夹属性隐藏 编辑:程序博客网 时间:2024/06/03 04:26
在项目开发过程中,通常使用工具类来管理SessionFactory和Session,代码如下所示。
package com.obtk.utils;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = "/hibernate.cfg.xml";static { try {configuration.configure(configFile);sessionFactory = configuration.buildSessionFactory();} catch (Exception e) {System.err.println("%%%% Error Creating SessionFactory %%%%");e.printStackTrace();} } private HibernateUtil() { } //获得session对象 public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get();if (session == null || !session.isOpen()) {if (sessionFactory == null) {rebuildSessionFactory();}session =sessionFactory.openSession();threadLocal.set(session);} return session; } //重新建立session工厂public static void rebuildSessionFactory() {try {configuration.configure(configFile);sessionFactory = configuration.buildSessionFactory();} catch (Exception e) {System.err.println("%%%% Error Creating SessionFactory %%%%");e.printStackTrace();}}//关闭session public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } }public static SessionFactory getSessionFactory() {return sessionFactory;}public static void setConfigFile(String configFile) {HibernateUtil.configFile = configFile;sessionFactory = null;}public static Configuration getConfiguration() {return configuration;}}
==========================================
用hibernateUtil工具类改造前面的程序如下:
package com.obtk.test;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import com.obtk.entitys.UserEntity;import com.obtk.utils.HibernateUtil;public class TestHql01 {public static void main(String[] args) {Session session=null;String hqlSql="from UserEntity";try {//获得数据库链接session=HibernateUtil.getSession();//获得一个查询接口Query qy=session.createQuery(hqlSql);//得到结果List<UserEntity> userList=qy.list();for(UserEntity user : userList){System.out.println(user.getUserName()+","+user.getEmail());}} catch (HibernateException e) {e.printStackTrace();}finally{HibernateUtil.closeSession();}}}
阅读全文
0 0
- hibernate系列三:hibernate工具类及使用
- hibernate基础及使用工具创建
- hibernate使用注解时的工具类
- Hibernate 安装及使用
- 【Hibernate总结系列】使用举例
- Hibernate工具类
- Hibernate工具类
- Hibernate工具类
- Hibernate 的工具类
- Hibernate工具类
- HIbernate工具类
- Hibernate工具类模版
- Hibernate 工具类
- hibernate的工具类
- Hibernate工具类
- Hibernate的工具类
- Hibernate工具类
- Hibernate的工具类
- 通过六个题目彻底掌握String笔试面试题
- 缓存行
- 【转】linux下杀死进程(kill)的N种方法
- Cron表达式详解
- Osaka操作系统实现过程中遇到的问题
- hibernate系列三:hibernate工具类及使用
- java自学笔记3:java中的继承
- android 发送屏蔽Emoji表情
- 在github上快速创建文件夹以及生成链接的详尽步骤
- 入门到放弃node系列统一声明
- 关于使用thinkPHP3.2使用union会出现分页不对的问题
- 基于openwrt系统的小米路由器创建自启动脚本
- Java HashMap Learning
- SparkSQL read Elasticsearch ClassNotFoundException