这篇文章主要讲解:
1>.对Hibernate使用的一些简单封装;
· 2>.在单元测试中,使用Hibernate的封装的工具进行增删改查的测试
1.目录结构展示
2.代码展示
2.0 配置文件 hibernate.cfg.xml
com.mysql.jdbc.Driver jdbc:mysql:///test root root org.hibernate.dialect.MySQLDialect true true
2.1 pojo 类
User
package cn.vincent.pojo;import java.io.Serializable;public class User implements Serializable { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + id; 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; User other = (User) obj; if (age != other.age) return false; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
User.hbm.xml 即User的映射
2.2 util 工具类
package cn.vincent.util;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 HibernateUtil { private static Configuration cfg; private static ServiceRegistry registry; private static SessionFactory factory; private static ThreadLocalsession; static{ //初始化 cfg=new Configuration().configure(); registry=new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()) .build(); factory=cfg.buildSessionFactory(registry); session=new ThreadLocal<>(); } //获取连接 public static Session getSession(){ if(session.get()==null){ session.set(factory.openSession()); } return session.get(); } //释放资源 public static void close(){ if(session.get()!=null){ session.get().close(); session.set(null); } } }
2.3 单元测试
HibernateTest
package cn.vincent.hibernate;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import cn.vincent.pojo.User;import cn.vincent.util.HibernateUtil;public class HibernateTest { //根据id查询单个对象 @Test public void testGetById(){ Session session = HibernateUtil.getSession(); User user=(User)session.get(User.class, 2); System.out.println(user.getId()+"----"+user.getName()+"----"+user.getAge()); HibernateUtil.close(); } //添加数据 @Test public void testSave(){ Session session=null; Transaction tx =null; try { session = HibernateUtil.getSession(); User user= new User(); user.setName("金庸"); user.setAge(80); //开启事务 tx = session.beginTransaction(); //返回 被保存数据的id session.save(user); //提交事务 tx.commit(); System.out.println("保存数据成功"); } catch (Exception e) { e.printStackTrace(); //回滚事务 if(tx!=null) tx.rollback(); System.out.println("保存数据失败"); }finally{ HibernateUtil.close(); } } //删除数据 @Test public void testDelete(){ Session session=null; Transaction tx =null; try { session = HibernateUtil.getSession(); //开启事务 tx = session.beginTransaction(); User user =(User)session.get(User.class, 4); session.delete(user); //提交事务 tx.commit(); System.out.println("删除数据成功"); } catch (Exception e) { e.printStackTrace(); //回滚事务 if(tx!=null) tx.rollback(); System.out.println("删除数据失败"); }finally{ HibernateUtil.close(); } } //更新 @Test public void testUpdate(){ Session session=null; Transaction tx =null; try { session = HibernateUtil.getSession(); //开启事务 tx = session.beginTransaction(); User user =(User)session.get(User.class, 4); user.setName("本拉登"); session.update(user); //提交事务 tx.commit(); System.out.println("更新数据成功"); } catch (Exception e) { e.printStackTrace(); //回滚事务 if(tx!=null) tx.rollback(); System.out.println("更新数据失败"); }finally{ HibernateUtil.close(); } } //查询所有 @Test public void testFindAll(){ Session session=null; try { session = HibernateUtil.getSession(); Listlist = session.createCriteria(User.class).list(); for(User u:list){ System.out.println(u); } } catch (Exception e) { e.printStackTrace(); }finally{ HibernateUtil.close(); } }}
3.单元测试运行效果
运行单元测试
如下图: