本文共 5773 字,大约阅读时间需要 19 分钟。
package com.mycompany.demo.bean;public class Forum { private int fid; private String name; public Forum() { super(); } public Forum(String name) { super(); this.name = name; } public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + fid; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Forum other = (Forum) obj; if (fid != other.fid) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }}
This class contains the forum detail.
package com.mycompany.demo.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HbnUtil { private static SessionFactory sessionFactory; public static Session getSession(){ if(sessionFactory == null || sessionFactory.isClosed()){ sessionFactory = new Configuration().configure().buildSessionFactory(); } return sessionFactory.getCurrentSession(); }}
org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/b_shequ_two root update true true thread org.hibernate.c3p0.internal.C3P0ConnectionProvider
package com.mycompany.demo.bean;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.SQLQuery;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.criterion.Order;import org.junit.Before;import org.junit.Test;import com.mycompany.demo.util.HbnUtil;public class ManageForum { private Session session; @Before public void init(){ session = HbnUtil.getSession(); } /* * 添加数据 */ @Test public void testSave(){ try { session.beginTransaction(); for(int i = 0;i < 10;i++){ Forum forum = new Forum("forum"+i); session.save(forum); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 查询所有-SQL */ @Test public void testQueryForSql(){ try { session.beginTransaction(); String sql = "SELECT * FROM hnsq_forum"; Listforums = session.createSQLQuery(sql).addEntity(Forum.class).list(); for (Forum forum : forums) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 查询所有-HQL */ @Test public void testQueryForHQL(){ try { session.beginTransaction(); String hql = "FROM Forum"; List forums = session.createQuery(hql).list(); for (Forum forum : forums) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 查询所有-QBC */ @Test public void testQueryForQBC(){ try { session.beginTransaction(); List forums = session.createCriteria(Forum.class).list(); for (Forum forum : forums) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (HibernateException e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 对查询结果进行排序-SQL */ @Test public void testOrderForSQL(){ try { session.beginTransaction(); String sql = "SELECT * FROM hnsq_forum ORDER BY fid desc"; List forums = session.createSQLQuery(sql).addEntity(Forum.class).list(); for (Forum forum : forums) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 对查询结果进行排序-HQL */ @Test public void testOrderForHQL(){ try { session.beginTransaction(); String hql = "FROM Forum ORDER BY fid desc"; List forums = session.createQuery(hql).list(); for (Forum forum : forums) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 对查询结果进行排序-QBC */ @Test public void testOrderForQBC(){ try { session.beginTransaction(); List forums = session.createCriteria(Forum.class) .addOrder(Order.desc("fid")).list(); for (Forum forum : forums) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (HibernateException e) { session.getTransaction().rollback(); e.printStackTrace(); } } }
'
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1911338
转载地址:http://phvta.baihongyu.com/