ssh增删改查案例
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public User getUser() { return user; }
public void setUser(User user) {
er = user; }
Transaction tx = session.beginTransaction(); //打开事务 try{
//执行 action 和 result invocation.invoke(); //提交事务 mit(); }catch(Exception ex){ ex.printStackTrace(); tx.rollback();//回滚事务 }finally{ //关闭 session HibernateSessionFactory.closeSession(); }
public HibernateDao getDao() { return dao; }
public void setDao(HibernateDao }
public String update() throws Exception{ System.out.println("********"+user); dao.update(user); return "update"; } public String deleteById() throws Exception{
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
public class OpenSessionInterceptor extends AbstractInterceptor{
@Override public String intercept(ActionInvocation invocation) throws Exception { Session session = HibernateSessionFactory.getSession();
ssh 增删改查案例
action: package com.votoon.action;
import java.util.List;
import com.votoon.dao.HibernateDao; import er;
public class ListAction { private List<User> list; private HibernateDao dao; public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public HibernateDao getDao() {
return null; } }
实体类(pojo): package com.votoon.pojo; import java.io.Serializable; public class User implements Serializable{ private int id; private String name; private int age; private int salary;
public User findById(int id) throws Exception { // TODO Auto-generated method stub User user
= (User)this.getHibernateTemplate().get(User.class, id); return user; }
* pattern, see {@link /42.html }. */ public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
public class ImplHibernateDao extends HibernateDaoSupport implements HibernateDao {
public void add(User user) throws Exception { // TODO Auto-generated method stub this.getHibernateTemplate().save(user);
}
public List<User> findAll() throws Exception {
// TODO Auto-generated method stub String hql = "from User"; List<User> list = this.getHibernateTemplate().find(hql); return list; }
public void update(User user) throws Exception { // TODO Auto-generated method stub this.getHibernateTemplate().update(user);
}
}
拦截器(interceptor): package com.votoon.interceptor; import org.hibernate.Session; import org.hibernate.Transaction; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import com.votoon.util.HibernateSessionFactory;
System.out.println(user); dao.add(user); return "add"; } public String init() throws Exception{ user = dao.findById(id); return "success"; }
} 实现类: package com.votoon.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupp ort;
import er;
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getSalary() { return salary; } public void setSalary(int salary) {
dao.deleteById(id); return "delete";
} public String view() throws Exception{ user = dao.findById(id); return "view"; } public String add() throws Exception{
return dao; } public void setDao(HibernateDao dao) { this.dao = dao; } public String execute(){ try { list = dao.findAll(); return "success"; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "error"; } }
this.salary = salary; }
}
工具类(util hibernate 自动生成): package com.votoon.util; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session