1.创建项目,项目名称hibernatedemo4,目录结构如图所示
2.在项目中创建lib目录存储jar文件,目录结构如图所示
3.在src目录中创建实体Bean Forum,包名(com.mycompany.demo.bean),如图所示
4.实体Bean Forum的内容如下
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; }}
5.在src目录中创建实体Bean Forum的映射文件Forum.hbm.xml,包名(com.mycompany.demo.bean),如图所示
6.映射文件Forum.hbm.xml的内容如下
This class contains the forum detail.
7.在src目录中创建工具类 HbnUtil,包名(com.mycompany.demo.util),如图所示
8.工具类 HbnUtil的内容如下
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(); }}
9.在src目录中创建Hibernate的配置文件hibernate.cfg.xml,如图所示
10.Hibernate的配置文件hibernate.cfg.xml的内容如下
org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/b_shequ_two root update true true thread
11.在项目中创建test目录存储测试文件,文件名称ManageForum,包名(com.mycompany.demo.bean),目录结构如图所示
12.ManageForum测试类的内容如下
package com.mycompany.demo.bean;import java.util.List;import org.hibernate.Query;import org.hibernate.SQLQuery;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.mycompany.demo.util.HbnUtil;public class ManageForum { /* * 查询数据 */ @Test public void testCreateCriteria(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 List lists = session.createCriteria(Forum.class).list(); for (Object object : lists) { Forum forum = (Forum) object; System.out.println(forum.getFid() + "=>" + forum.getName()); } //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 查询数据 */ @Test public void testCreateSQLQuery(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 SQLQuery query = session.createSQLQuery("SELECT * FROM hnsq_forum"); List lists = query.addEntity(Forum.class).list(); for (Object object : lists) { Forum forum = (Forum) object; System.out.println(forum.getFid() + "=>" + forum.getName()); } //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 查询数据 */ @Test public void testCreateQuery(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Query query = session.createQuery("FROM Forum"); List lists = query.list(); for (Object object : lists) { Forum forum = (Forum) object; System.out.println(forum.getFid() + "=>" + forum.getName()); } //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 删除数据 */ @Test public void testDelete(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = new Forum(); forum.setFid(56); session.delete(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 修改数据 */ @Test public void testUpdate(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = new Forum("update"); forum.setFid(56); session.update(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 保存数据 JPA中的方法 */ @Test public void testPersist(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = new Forum("persist"); session.persist(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 保存数据 hibernate中的方法 */ @Test public void testSave(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = new Forum("save"); session.save(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 查询不到数据时,抛出异常 */ @Test public void testLoad(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = session.load(Forum.class, 60); System.out.println(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } System.out.println("异常了"); e.printStackTrace(); } } /* * 查询不到数据时,返回null */ @Test public void testGet(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = session.get(Forum.class, 60); System.out.println(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } e.printStackTrace(); } } /* * saveOrupdate()根据是否具有id来判断执行保存或修改操作 */ @Test public void testSaveOrUpdate(){ //1.获取Session Session session = HbnUtil.getSession(); Transaction transaction = null; try { //2.开启事务 transaction = session.beginTransaction(); //3.执行操作 Forum forum = new Forum("saveOrUpdate"); forum.setFid(59); session.saveOrUpdate(forum); //4.提交事务 transaction.commit(); } catch (Exception e) { //5.回滚事务 if(transaction != null){ transaction.rollback(); } e.printStackTrace(); } }}
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1911157