标签:
测试项目:spring4_Struts2_Hibernate4
一、创建HibernateSessionFactory
package com.chen.utils; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateSessionFactory { private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); //private static String configfile = "/hibernate.cfg.xml"; private static Configuration configuration = new Configuration().configure(); private static SessionFactory sessionFactory; static { try { StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder(); ServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (HibernateException e) { e.printStackTrace(); } } private HibernateSessionFactory() { } public static Session getSession() { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } public static void closeSesssion() { Session session = threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void setSessionFactory(SessionFactory sessionFactory) { HibernateSessionFactory.sessionFactory = sessionFactory; } public static Configuration getConfiguration() { return configuration; } public static void setConfiguration(Configuration configuration) { HibernateSessionFactory.configuration = configuration; } }
二、在测试类进行测试
package com.chen.test; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.chen.utils.HibernateSessionFactory; import com.chen.vo.News; public class HibernateTest { //获取session @Test public void sessionTest(){ Session session = HibernateSessionFactory.getSession(); System.out.println(session); } //查询News的列表信息 @Test public void listNews(){ Session session = HibernateSessionFactory.getSession(); Query query = session.createQuery("from News"); List<?> list = query.list(); System.out.println(list); } //通过id查询指定的News @Test public void findById(){ Session session = HibernateSessionFactory.getSession(); Query query = session.createQuery("from News where id=?"); //设置参数 query.setParameter(0, 1); //单值检索 News n = (News) query.uniqueResult(); System.out.println(n); } //添加News @Test public void saveNews(){ Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); News n=new News(); n.setTitle("title3"); n.setContent("content3"); session.saveOrUpdate(n);; tx.commit(); session.close(); } //修改News @Test public void updateNews(){ Session session = HibernateSessionFactory.getSession(); //先获取指定id的News Query query = session.createQuery("from News where id=?"); query.setParameter(0, 5); News n = (News) query.uniqueResult(); //再进行修改 Transaction tx = session.beginTransaction(); n.setContent("update_content5"); session.saveOrUpdate(n); tx.commit(); session.close(); System.out.println(n); } //删除News @Test public void deteleNews(){ Session session = HibernateSessionFactory.getSession(); Query query = session.createQuery("from News where id=?"); query.setParameter(0, 4); News n = (News) query.uniqueResult(); Transaction tx = session.beginTransaction(); session.delete(n); tx.commit(); session.close(); } }
小结:“查”的实现是通过查询hql语句实现的。
附:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- c3p0连接池 --> <hibernate-configuration> <session-factory> <!-- 显示实际操作数据库时的SQL --> <property name="show_sql">true</property> <!-- SQL方言,这边设定的是Oracle --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!--驱动程序:Oracle数据库的配置 --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- JDBC URL --> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:myorcl</property> <!-- 数据库用户名 --> <property name="connection.username">scott</property> <!-- 数据库密码 --> <property name="connection.password">root</property> <!-- 指定连接池里最小连接数 --> <property name="hibernate.cp30.minsize">5</property> <!-- 指定连接池里的最大连接数 --> <property name="hibernate.c3p0.maxsize">20</property> <!-- 指定连接池里的超时时常 --> <property name="hibernate.cp30.timeout">1800</property> <!-- 指定连接池里最大缓存多少个Statement对象 --> <property name="hibernate.cp30.max_statements">50</property> <!-- 根据需要自动创建数据库表 --> <property name="hbm2ddl.auto">update</property> <!-- 对象与数据库表格映像文件 --> <mapping resource="com/chen/vo/News.hbm.xml"/> </session-factory> </hibernate-configuration>
标签:
原文地址:http://www.cnblogs.com/wql025/p/4855939.html