博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate 创建工厂类
阅读量:6872 次
发布时间:2019-06-26

本文共 2195 字,大约阅读时间需要 7 分钟。

package cn.hibernate;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * 创建一个工厂类 用于创建SessionFactory唯一的一个 */public class SessionFactoryUtils {    private static SessionFactory sessionFactory;    // 在静态的代码块中创建这个对象    static {        // 1:创建Configuration对象,用于读取hibernate.cfg.xml文件        Configuration config = new Configuration();        // 默认读取hibernte.cfg.xml        config.configure();        // 2:创建SessionFactory对象        sessionFactory = config.buildSessionFactory();    }    //3:提供一个静态的方法-返回SessionFactory的实例    public static SessionFactory getSessionFatory(){        return sessionFactory;    }}步4:测试是否连接数据库成功 – 获取 Connection对象    @Test    public void test1() {        // 1:获取 SessionFactory        SessionFactory sf = SessionFactoryUtils.getSessionFatory();        // 打开一个新的连接会话        Session session = sf.openSession();//        // 通过doWork获取一个COnnection,则所有在execute里面执行的方法都被Session控制        session.doWork(new Work() {            @Override            public void execute(Connection connection) throws SQLException {                System.err.println("连接是:" + connection);            }        });        session.close();    }

 

package cn.hibernate;

 

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

/**

 * 创建一个工厂类用于创建SessionFactory唯一的一个

 */

publicclass SessionFactoryUtils {

    privatestatic SessionFactory sessionFactory;

    // 在静态的代码块中创建这个对象

    static {

         // 1:创建Configuration对象,用于读取hibernate.cfg.xml文件

         Configuration config = new Configuration();

         // 默认读取hibernte.cfg.xml

         config.configure();

         // 2:创建SessionFactory对象

         sessionFactory = config.buildSessionFactory();

    }

    //3:提供一个静态的方法-返回SessionFactory的实例

    publicstatic SessionFactory getSessionFatory(){

         returnsessionFactory;

    }

}

 

4:测试是否连接数据库成功 获取 Connection对象

    @Test

    publicvoid test1() {

         // 1:获取 SessionFactory

         SessionFactory sf = SessionFactoryUtils.getSessionFatory();

         // 打开一个新的连接会话

         Session session = sf.openSession();//

         // 通过doWork获取一个COnnection,则所有在execute里面执行的方法都被Session控制

         session.doWork(new Work() {

             @Override

             publicvoid execute(Connection connection) throws SQLException {

                  System.err.println("连接是:" + connection);

             }

         });

         session.close();

    }

 

转载于:https://www.cnblogs.com/fujilong/p/5451098.html

你可能感兴趣的文章
(转)C#抽象类和接口对比
查看>>
在树莓派(Raspberry Pi)上编译安装更新版本的Python
查看>>
react 调用 function 的写法 及 解决 react onClick 方法自动执行
查看>>
运行时内存以及垃圾收集器
查看>>
27、通过visual s'tudio 验证 SOCKET编程:搭建一个TCP服务器
查看>>
docker之Dockerfile实践
查看>>
JS堆栈与拷贝
查看>>
P3224 [HNOI2012]永无乡
查看>>
插件就是生产力——那些不能错过的XCode插件们
查看>>
Python打造一个在线G代码生成器
查看>>
ionic开发-怪癖001(http请求 android下无法正常运行)
查看>>
Java实现的基于socket的一次通信
查看>>
Form保存顺序
查看>>
[python]错误检测及异常处理try-except
查看>>
SharePoint 2010 "客户端不支持使用windows资源管理器打开此列表" 解决方法
查看>>
ZOJ-2913 Bus Pass---BFS进阶版
查看>>
合并排序(非哨兵方法)
查看>>
PHP 依赖管理神器 Composer 基本使用
查看>>
sass进阶篇
查看>>
为项目配置logback日志
查看>>