其他分享
首页 > 其他分享> > 2021-05-24

2021-05-24

作者:互联网

Hibernate-Query 保存和查询

创建单例模式

package Dao;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//SessionFactory单态模式
public class HibernateUtil {
	private static Configuration cfg = null;
	private static SessionFactory sf = null;
	//静态代码块
	static {  //在类加载的时候只能执行一次
		try {
			cfg = new Configuration().configure();
			sf = cfg.buildSessionFactory();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获得session工厂
	public static SessionFactory getSessionFactory() {
		return sf;
	}
	//关闭工厂
	public static void closeSessionFactory() {
		sf.close();
	}
	
}



	}
}

创建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>

保存

public void testSavaUser() {
		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-10-24"));
		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();
		}
	}
}


查询

public void testQueryUser() {
		SessionFactory sf=null;
		Session session =null;
		Transaction ts=null;
		
		try {
			sf=HibernateUtil.getSessionFactory();//SessionFactory单态模式
			session =sf.getCurrentSession(); //保证每个读写线程有唯一的session实例
			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.println(u.getName()+"  "+u.getGender()+"  "+u.getAge()+"  "+u.getBirthday());
//			}
            //遍历方法二
			for(User u:users) {  
				System.out.println(u.getName()+"  "+u.getGender()+"  "+u.getAge()+"  "+u.getBirthday());
			}
			
			ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(ts!=null) {
				ts.rollback();
			}
		}
	}


标签:24,05,ts,SessionFactory,session,2021,catch,null,sf
来源: https://blog.csdn.net/Coffee_or_Milk/article/details/117229860