标签:ring 图片 操作 update number 类继承 ble ace bubuko
使用hibernate测试加载数据、删除数据和修改数据等功能时,针对的是与数据库表user对应的User。
为了简化对其他数据表对应的实体类的持久化操作,可以在项目中创建一个BaseHibernateDAO类,将数据的加载、添加、修改、删除等持久化方法封装其中。
BaseHibernateDAO.java
package com.dao; import java.io.Serializable; import javax.xml.rpc.handler.HandlerInfo; import org.hibernate.Session; import org.hibernate.Transaction; import com.util.HibernateSessionFactory; public class BaseHibernateDAO { protected void add(Object object) { Transaction t = null; Session session = HibernateSessionFactory.getSession(); //获取session try{ //开始事务 t = session.beginTransaction(); //持久化事务 session.save(object); //提交事务 t.commit(); }catch (Exception e) { if(t!=null){ //事务回滚 t.rollback(); } e.printStackTrace(); }finally{ HibernateSessionFactory.closeSession(); } } //加载数据 protected Object get(Class cla, Serializable id) { Object object = null; Session session = HibernateSessionFactory.getSession(); try{ object = session.get(cla, id); }catch (Exception e) { e.printStackTrace(); }finally{ HibernateSessionFactory.closeSession(); } return object; } //删除数据 protected void delete(Object object) { Transaction t = null; Session session = HibernateSessionFactory.getSession(); try { t = session.beginTransaction(); session.delete(object); t.commit(); } catch (Exception e) { if(t!=null){ t.rollback(); } e.printStackTrace(); } finally{ HibernateSessionFactory.closeSession(); } } //修改数据 protected void update(Object object) { Transaction t = null; Session session = HibernateSessionFactory.getSession(); try { t = session.beginTransaction(); session.update(object); t.commit(); } catch (Exception e) { if(t!=null){ t.rollback(); } e.printStackTrace(); } finally{ HibernateSessionFactory.closeSession(); } } }
为了使得数据访问层的代码更加清晰,创建一个接口UserDAO.java,并在接口中定义一些方法
package com.dao; import com.vo.Stu; public interface UserDAO { public void add(Stu stu); public void delete(Stu stu); public Stu get(int id); public void update(Stu stu); }
实现类UserDAOImpl类继承BaseHibernateDAO类(操作数据),实现UserDAO接口(具体实现方法)
package com.dao; import com.vo.Stu; public class UserDAOImpl extends BaseHibernateDAO implements UserDAO { @Override public void add(Stu stu) { // TODO Auto-generated method stub super.add(stu); } @Override public void delete(Stu stu) { // TODO Auto-generated method stub super.delete(stu); } @Override public Stu get(int id) { // TODO Auto-generated method stub return (Stu) super.get(Stu.class, id); } @Override public void update(Stu stu) { // TODO Auto-generated method stub super.update(stu); } }
创建测试类
package com.test; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import com.dao.UserDAO; import com.dao.UserDAOImpl; import com.util.HibernateSessionFactory; import com.vo.Stu; public class OpTest { public static void main(String[] args) { OpTest test = new OpTest(); //test.testAdd(); //test.testDelete(3); test.testUpdate(2); } //添加数据 public void testAdd() { Stu stu = new Stu(); stu.setSage(11); stu.setSname("85693897453"); stu.setSnumber("2016"); UserDAO userDAO = new UserDAOImpl(); userDAO.add(stu); } //删除数据 public void testDelete(int id) { UserDAO userDAO = new UserDAOImpl(); Stu stu = userDAO.get(id); userDAO.delete(stu); } //修改数据 public void testUpdate(int id) { UserDAO userDAO = new UserDAOImpl(); Stu stu = userDAO.get(id); stu.setSname("FUCK U!"); userDAO.update(stu); } }
标签:ring 图片 操作 update number 类继承 ble ace bubuko
原文地址:https://www.cnblogs.com/Roni-i/p/10166663.html