package cn.itcast.utils; import java.sql.Connection; import java.sql.SQLException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.jdbc.Work; public class HibernateUtils { private static Configuration cfg; private static SessionFactory sessionFactory; private static ThreadLocal<Session> threadLocal; static { cfg = new Configuration();//一个加载器 cfg.configure();//加载默认位置核心配置文件 sessionFactory = cfg.buildSessionFactory();//一个session工厂 threadLocal=new ThreadLocal<Session>(); } public static Session getSession() { Session session=threadLocal.get(); if(session==null) { cfg.configure();//加载默认核心配置文件 session = sessionFactory.openSession();//创建session threadLocal.set(session); session=threadLocal.get(); } return session; } public static void main(String[] args) { Session session = getSession(); session.doWork(new Work() { @Override public void execute(Connection con) throws SQLException { // TODO Auto-generated method stub System.out.println(con); }}); } }
如对本文有疑问, 点击进行留言回复!!
在django中查询获取数据,get, filter,all(),values()操作
如何在IDEA中对 hashCode()和 equals() 利用快捷键快速进行方法重写
springboot集成普罗米修斯(Prometheus)的方法
网友评论