Hibernate 单例模式与 Query 保存与查询

单例模式

1.生成SessionFactory

public class HibernateUtil {
	private static SessionFactory sf = null;
	private static Configuration cfg = null;
	static {
		try {
			cfg = new Configuration().configure();
			sf = cfg.buildSessionFactory();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static SessionFactory getSessionFactory() {
		return sf;
	}
	public static void closeSessionFactory(){
		sf.close();
	}
}

2.为保证线程安全,使用getCurrentSession()方法创建Session,并需保证每个读写线程有唯一的Session实例,具体代码如下:

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
	
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/firstorm?useSSL=false</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>
	
		<property name="show_sql">true</property>
		<property name="hibernate.current_session_context_class">thread</property>
		<mapping resource="cn/hrbust/pojo/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

Query的保存与查询

1.保存

//保存
	public void testSaveUser() {
		Configuration cfg = null;
		SessionFactory sf = null;
		Session session = null;
		Transaction ts = null;
		User u = new User();
		u.setName("于悦");
		u.setGender("女");
		u.setAge(21);
		u.setBirthday(Date.valueOf("2000-7-1"));
		try {
			
			sf = HibernateUtil.getSessionFactory();
			session = sf.getCurrentSession();
			ts = session.beginTransaction();
			session.save(u);
			ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(ts != null) {
				ts.rollback();
			}
		}finally {
			session.close();
			sf.close();
		}
	}

2.查询

//查询
	public void testQueryUser() {
		Configuration cfg = null;
		SessionFactory sf = null;
		Session session = null;
		Transaction ts = null;
		
		try {
			
			sf = HibernateUtil.getSessionFactory();
			session = sf.getCurrentSession();
			ts = session.beginTransaction();
			Query query = session.createQuery("from User");
			List<User> users = query.List();
			for(int i = 0; i < users.size(); i++) {
				User u = users.get(i);
				System.out.printIn(u.getName() + " " + u.getAge());
			}
			
			ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(ts != null) {
				ts.rollback();
			}
		}finally {
			session.close();
			sf.close();
		}
	}

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值