ssh增删改查案例

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
public class ImplHibernateDao extends HibernateDaoSupport implements HibernateDao {
public void add(User user) throws Exception { // TODO Auto-generated method stub this.getHibernateTemplate().save(user);
Transaction tx = session.beginTransaction(); //打开事务 try{
//执行 action 和 result invocation.invoke(); //提交事务 tx.commit(); }catch(Exception ex){ ex.printStackTrace(); tx.rollback();//回滚事务 }finally{ //关闭 session HibernateSessionFactory.closeSession(); }
* pattern, see {@link http://hibernate.org/42.html }. */ public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
dao.deleteById(id); return "delete";
} public String view() throws Exception{ user = dao.findById(id); return "view"; } public String add() throws Exception{
public class OpenSessionInterceptor extends AbstractInterceptor{
@Override public String intercept(ActionInvocation invocation) throws Exception { Session session = HibernateSessionFactory.getSession();
System.out.println(user); dao.add(user); return "add"; } public String init() throws Exception{ user = dao.findById(id); return "success"; }
* #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
}
dao:
接口:package com.votoon.dao;
import java.util.List;
import com.votoon.pojo.User;
public interface HibernateDao { public void deleteById(int id)throws Exception; public User findById(int id)throws Exception; public List<User> findAll()throws Exception; public void update(User user)throws Exception; public void add(User user)throws Exception;
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 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; } public int getSalary() { return salary; } public void setSalary(int salary) {
ssh 增删改查案例
action: package com.votoon.action;
import java.util.List;
import com.votoon.dao.HibernateDao; import com.votoon.pojo.User;
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() {
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public User getUser() { return user; }
public void setUser(User user) {
this.user = user; }
}
public void deleteById(int id) throws Exception { // TODO Auto-generated method stub User user = new User(); user.setId(id); this.getHibernateTemplate().delete(user);
public HibernateDao getDao() { return dao; }
public void setDao(HibernateDao dao) { this.dao = dao; }
public String update() throws Exception{ System.out.println("********"+user); dao.update(user); return "update"; } public String deleteById() 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"; } }
}
package com.votoon.action;
import com.votoon.dao.HibernateDao; import com.votoon.pojo.User;
public class UserAction { private int id; private User user; private HibernateDao dao;
* the location of the configuration file for the current session.
*/
private
static
String
CONFIG_FILE_LOCATION
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;
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
} 实现类: package com.votoon.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupp ort;
import com.votoon.pojo.User;
}
public List<User> findAll() throws Exception {
Biblioteka Baidu
// TODO Auto-generated method stub String hql = "from User"; List<User> list = this.getHibernateTemplate().find(hql); return list; }
public User findById(int id) throws Exception { // TODO Auto-generated method stub User user
= (User)this.getHibernateTemplate().get(User.class, id); return user; }
相关文档
最新文档