博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate 1、Hello Hibernate
阅读量:6941 次
发布时间:2019-06-27

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

所使用到的jar 包:

1、创建实体类

public class User {	private Integer id;	private String name;	private String address;	public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getAddress() {		return address;	}	public void setAddress(String address) {		this.address = address;	}	}

2、配置映射文件

3、配置hiberante.cfg.xml; 详细 

org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8
root
true
true
update

4、测试用例:

public class Conn {	public void addUser(){		Configuration cfg=new Configuration();		cfg.configure();		ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();		SessionFactory sf=cfg.buildSessionFactory(sr);				Session s=sf.openSession();		Transaction trans=s.beginTransaction();		User user=new User();		user.setName("汤姆");		user.setAddress("北海");		s.save(user);		trans.commit();		s.close();	}}

代码优化:

新建一个类名字是HibernateSessionFactory:

import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class HibernateSessionFactory {	private static final ThreadLocal
threadLocal = new ThreadLocal
(); private static org.hibernate.SessionFactory sessionFactory; private static Configuration configuration = new Configuration(); private static ServiceRegistry serviceRegistry; static { try { configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private HibernateSessionFactory() { } public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } public static void rebuildSessionFactory() { try { configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; } public static Configuration getConfiguration() { return configuration; }}
优化过以后代码:

public void addUser(){		Session s=HibernateSessionFactory.getSession();		Transaction trans=s.beginTransaction();		User user=new User();		user.setName("汤姆11");		user.setAddress("北海");		s.save(user);		trans.commit();		s.close();	}
你可能感兴趣的文章
Fluwx:让在Flutter中使用微信SDK成为可能
查看>>
《Groovy极简教程》第3章 Groovy基本语法
查看>>
百度贴吧 | 通用抓图脚本
查看>>
Window下Pothos SDR开发环境搭建
查看>>
如何增加博客访问量
查看>>
浅谈Java字符串(操作)
查看>>
精读《React 的多态性》
查看>>
JQuery实现注册表单验证
查看>>
solr7安装(1)
查看>>
我为NET狂~群福利:逆天书库
查看>>
UNIX文件I/O
查看>>
说说React组件的State
查看>>
央视会玩,2017年春晚或推出VR直播
查看>>
c#扩展方法的使用
查看>>
Xamarin android 调用Web Api(ListView使用远程数据)
查看>>
always on 集群
查看>>
CentOS下LAMP一键yum安装脚本
查看>>
[20180403]关于时区问题.txt
查看>>
满足各种需求,德阳人民医院Wi-Fi覆盖选择飞鱼星
查看>>
疯狂剁手之后 平台帮了谁又肥了谁?
查看>>