hibernate入门(二)
第一个只是写完了查询和添加
这个补上 删除和修改
习惯性动作,整个项目中内容加载,这次不加载jar包信息,因为和上篇的一样,xml配置信息也相同,省去,本文章可运行
从struts的配置文件action开始
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="hibernate" extends="struts-default"> <action name="save" class="com.test.action.PersonAction" method="save"> <result name="success">/listAll.jsp</result> </action> <action name="getPerson" class="com.test.action.PersonAction" method="getPerson"> <result name="success">/getPerson.jsp</result> </action> <action name="deletePerson" class="com.test.action.PersonAction" method="deletePerson"> <result name="success">/listAll.jsp</result> </action> <action name="updatePPerson" class="com.test.action.PersonAction" method="getPerson"> <result name="success">/updatePerson.jsp</result> </action> <action name="updatePerson" class="com.test.action.PersonAction" method="updatePerson"> <result name="success">/listAll.jsp</result> </action> </package> </struts>
2.action文件
package com.test.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.hibernate.model.Person; import com.hibernate.persistence.DBPerson; import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport { private int id; private String username; private String password; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } // 完成用户增加的操作 public String save() throws Exception { Person person = new Person(); person.setUsername(username); person.setPassword(password); person.setAge(age); java.sql.Date registerDate = new java.sql.Date(new java.util.Date() .getTime()); person.setRegisterdate(registerDate); DBPerson.save(person); //将person对象存到数据库中 List<Person> list = DBPerson.listAll(); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("list", list); return SUCCESS; } public String getPerson() throws Exception { System.out.println("getPerson"); Person person = DBPerson.getPersonById(id); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("person", person); return SUCCESS; } public String deletePerson() throws Exception { DBPerson.removePerson(id); List<Person> list = DBPerson.listAll(); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("list", list); return SUCCESS; } public String updatePerson() throws Exception { Person person = DBPerson.getPersonById(id); person.setPassword(password); person.setAge(age); DBPerson.updatePerson(person); List<Person> list = DBPerson.listAll(); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("list", list); return SUCCESS; } }
3.编写dao层文件
package com.hibernate.persistence; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.hibernate.model.Person; import com.hibernate.util.HibernateUtil; public class DBPerson { /** * 创建新的用户 */ public static void save(Person person) { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); //开启事务 try { session.save(person); tx.commit(); } catch(Exception ex) { System.out.println("增加用户异常发生!"); if(null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } } /** * 查询出所有用户 */ @SuppressWarnings("unchecked") public static List<Person> listAll() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); //开启事务 List<Person> list = null; try { Query query = session.createQuery("from Person"); //hql语句,Hibernate查询语句 list = (List<Person>)query.list(); tx.commit(); } catch(Exception ex) { System.out.println("增加用户异常发生!"); if(null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } return list; } public static Person getPersonById(Integer id) { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); //开启事务 Person person = null; try { //Object get(Class clazz, Serializable id) person = (Person)session.get(Person.class, id); tx.commit(); } catch(Exception ex) { System.out.println("增加用户异常发生!"); if(null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } return person; } public static void removePerson(Integer id) { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); //开启事务 try { Person person = (Person)session.get(Person.class,id); session.delete(person); tx.commit(); } catch(Exception ex) { System.out.println("增加用户异常发生!"); if(null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } } public static void updatePerson(Person person) { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); //开启事务 try { session.update(person); tx.commit(); } catch(Exception ex) { System.out.println("增加用户异常发生!"); if(null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } } }
上篇文章中就改了这三个文件。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。