HibernateUtil工具类

工具类:
 
          
1
import java.util.Properties;
2
3
import org.hibernate.Session;
4
import org.hibernate.SessionFactory;
5
import org.hibernate.cfg.Configuration;
6
import org.hibernate.service.ServiceRegistry;
7
import org.hibernate.service.ServiceRegistryBuilder;
8
9
public class HibernateUtil {
10
    public static SessionFactory sessionFactory=null;
11
    
12
    static{
13
        Configuration cfg=new Configuration().configure();
14
        ServiceRegistryBuilder serviceRegistryBuilder=new ServiceRegistryBuilder();
15
        Properties properties=cfg.getProperties();
16
        serviceRegistryBuilder.applySettings(properties);
17
        ServiceRegistry serviceRegistry=serviceRegistryBuilder.buildServiceRegistry();
18
        sessionFactory=cfg.buildSessionFactory(serviceRegistry);
19
        
20
    }
21
    public static Session getSession(){
22
        Session session=sessionFactory.openSession();
23
        return session;
24
    }
25
}

你可能感兴趣的:(hibernate)