日期:2014-05-16  浏览次数:20566 次

struts2.X心得4--struts2与hibernate连接数据库案例分析

以下只列举出主要的类文件以及struts2重要的知识类或配置,感兴趣的朋友可以留言,本人讲项目源码发过去
 
package www.csdn.action;
 
import java.util.List;
 
importwww.csdn.domain.Admin;
importwww.csdn.service.AdminService;
importwww.csdn.service.AdminServiceImpl;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class AdminAction extendsActionSupport{
 
       /**
        *
        */
       privatestatic final long serialVersionUID = -8739112121666773546L;
      
       privateString adminName;
       privateString adminPassword;
       privateAdmin entity;
       privateList<Admin> list;
       privateint id;
      
       privateAdminService service = new AdminServiceImpl();
      
       publicAdmin getEntity() {
              returnentity;
       }
      
       publicList<Admin> getList() {
              returnlist;
       }
 
       publicvoid setId(int id) {
              this.id= id;
       }
 
       publicvoid setAdminName(String adminName) {
              this.adminName= adminName;
       }
      
       publicString getAdminName() {
              returnadminName;
       }
 
       publicvoid setAdminPassword(String adminPassword) {
              this.adminPassword= adminPassword;
       }
             
       //登录方法
       publicString login(){
              entity= service.login(adminName, adminPassword);
              if(entity!= null){
                     returnSUCCESS;
              }else{
                     returnLOGIN;                    
              }
       }
       //查找所有方法
       publicString list(){
              list= service.getAllObjects(Admin.class);
              if(list!= null){
                     returnSUCCESS;
              }else{
                     returnERROR;                   
              }
       }
       //删除方法
       publicString delete(){
              entity= service.getObjectById(Admin.class, id);
              service.deleteObject(entity);
                     returnSUCCESS;
       }
       //按id查询方法
       publicString find(){
              entity= service.getObjectById(Admin.class, id);
                     returnSUCCESS;
       }
       //增加方法
       publicString save(){
              entity= new Admin(adminName, adminPassword);
              service.saveObject(entity);
                     returnSUCCESS;
       }
       //更新方法
       publicString update(){
              entity= service.getObjectById(Admin.class, id);
              entity.setAdminPassword(adminPassword);
              service.updateObject(entity);
                     returnSUCCESS;
       }
}
 
package www.csdn.dao;
 
import java.util.List;
 
import org.hibernate.Session;
import org.hibernate.Transaction;
 
import www.csdn.domain.Admin;
import www.csdn.utils.BaseHibernateDao;
import www.csdn.utils.HibernateSessionFactory;
 
/***
 *dao实现层,这里将事务的操作跟关闭session的操作放到了dao层,实际开发中的规范是应该放在事务层,
 * 因为事务处理属于事务
 */
public class AdminDaoImpl extendsBaseHibernateDao implements AdminDao {
 
       @Override
       publicvoid deleteObject(Admin entity) {
              Transactiontx = null;
              try{
                     Sessionsession = getSession();
                     tx= session.beginTransaction();
                     session.delete(entity);
                     tx.commit();
              }catch (Exception e) {
                     tx.rollback();
                     thrownew RuntimeException("删除所有错误"+e);
              }finally {
                     HibernateSessionFactory.closeSession();
              }
       }
 
       @Override
       publicvoid deleteObjectById(Integer id) {
              Transactiontx = null;
              try{
                     Sessionsession = getSession();
                     tx= session.beginTransaction();
                     session.delete(id);
                     tx.commit();
              }catch (Exception e) {
                     tx.rollback();
                     thrownew RuntimeException("根据id错误"+e);