Hibernate 配置

时间:2021-09-09 03:31:42
package com.shuyinghengxie.doudou;

import static org.junit.Assert.*;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test; public class TEST01 { //测试 Hibernate连接
@Test
public void test()
{
//获取配置文件
Configuration cfg = new Configuration().configure() ; //注册配置
ServiceRegistry sr = new StandardServiceRegistryBuilder().
applySettings(cfg.getProperties()).build(); //获取SessionFactory
SessionFactory sf = cfg.buildSessionFactory(sr) ; //System.out.println(sf); sf.close(); //关闭,释放资源
} }