关闭

struts2.0+spring2.0+hibernate3.2实现增删改查CURD

标签: hibernatestrutsspringloginexceptionuser
473人阅读 评论(0) 收藏 举报

1、UserDAO.java

package com.lei.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.lei.shi.Login;

public class UserDAO {

private HibernateTemplate hibernateTemplate;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
   this.hibernateTemplate = hibernateTemplate;
}
/*
* 用户登录
*/
public Login Login(Login l) {
   String queryString = "select * from Login l where l.name='"
     + l.getName() + "' and l.password='" + l.getPassword() + "'";
   Session session = null;
   List list = null;
   try {
    session = hibernateTemplate.getSessionFactory().openSession();
    SQLQuery query = session.createSQLQuery(queryString);
    list = query.list();
    if (list != null && list.size() == 1) {
     Object[] o = (Object[]) list.get(0);
     Login login = new Login();
     login.setName(o[1].toString());
     login.setPassword(o[2].toString());
     return login;
    }
   } catch (HibernateException e) {
    e.printStackTrace();
   } finally {
    session.close();
   }
   return null;
}

/*
* 保存用户
*/
public void saveUser (Login l) {
   Session session = null;
   Transaction tx = null;
   try {
    session = hibernateTemplate.getSessionFactory().openSession();
    tx = session.beginTransaction();
    tx.begin();
    session.save(l);
    tx.commit();
   } catch (HibernateException e) {
    e.printStackTrace();
   } finally {
    session.close();
   }
}
/*
* 查询所有的用户
*/
public List<Login> findUser () {
   Session session = null;
   List<Login> userlist = null;
   try {
    session = hibernateTemplate.getSessionFactory().openSession();
    userlist = session.createQuery("from Login l order by l.id asc").list();
   } catch (HibernateException e) {
    e.printStackTrace();
   } finally {
    session.close();
   }
   return userlist;
}
/*
* 删除一个用户
*/
public void deleteUser (Login login ){
   hibernateTemplate.delete(login);
}

public Login findById (long id) {
   Login user = null;
   List<Login> userlist = hibernateTemplate.find("from Login l where l.id="+id);
   if (userlist!=null&&userlist.size()>0) {
    user = userlist.get(0);
   }
   return user;
}
}
2、LoginDAO.java代码

package com.lei.dao;

public class LoginDAO {
private UserDAO userDAO;

public UserDAO getUserDAO() {
   return userDAO;
}

public void setUserDAO(UserDAO userDAO) {
   this.userDAO = userDAO;
}

}
3、UserManager.java

package com.lei.manager;

import java.util.List;

import com.lei.dao.UserDAO;
import com.lei.shi.Login;

public class UserManager {

private UserDAO userDAO;

public void setUserDAO(UserDAO userDAO) {
   this.userDAO = userDAO;
}

public void saveUser (Login l) {
   userDAO.saveUser(l);
}

public List<Login> findUser () {
   return userDAO.findUser();
}

public void deleteUser (Login login ){
   userDAO.deleteUser(login);
}

public Login findById (long id) {
   return userDAO.findById(id);
}
}
4、BaseAction.java

package com.lei.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.lei.dao.LoginDAO;
import com.opensymphony.xwork2.Action;

public abstract class BaseAction implements Action {
public LoginDAO loginDAO() {
   ServletContext servletContext = ServletActionContext.getServletContext();
   WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
   return (LoginDAO) webApplicationContext.getBean("loginDAO");
}

public HttpServletRequest request() {
   return ServletActionContext.getRequest();
}

public HttpSession session() {
   return ServletActionContext.getRequest().getSession();
}

public ServletContext application() {
   return ServletActionContext.getServletContext();
}

public HttpServletResponse response() {
   return ServletActionContext.getResponse();
}
}
5、LoginAction.java

package com.lei.action;

import com.lei.shi.Login;

public class LoginAction extends BaseAction{
    private Login user = new Login();

    public Login getUser() {
        return user;
    }

    public void setUser(Login user) {
        this.user = user;
    }

    public String execute() throws Exception {
   
    Login resultUser=this.loginDAO().getUserDAO().Login(user);
        if(resultUser!=null){
            this.session().setAttribute("user", resultUser);
        }
        return SUCCESS;
    }
}
6、UserAction.java

package com.lei.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.lei.manager.UserManager;
import com.lei.shi.Login;

public class UserAction extends BaseAction {

private Login user = new Login();

    public Login getUser() {
        return user;
    }

    public void setUser(Login user) {
        this.user = user;
    }

private UserManager userManager;

public void setUserManager(UserManager userManager) {
   this.userManager = userManager;
}

public String saveuser() throws Exception {
   userManager.saveUser(user);
   return SUCCESS;
}

public String updateuser() throws Exception {
   userManager.saveUser(user);
   return SUCCESS;
}

public String finduser() throws Exception {
   List<Login> userlist = userManager.findUser();
   HttpServletRequest request = ServletActionContext.getRequest();
   request.setAttribute("userlist", userlist);
   return SUCCESS;
}

public String edituser() throws Exception {
   HttpServletRequest request = ServletActionContext.getRequest();
   String edit = request.getParameter("edit");
   long id = 0l;
   if (!"".equals(edit)&&edit!=null) {
    id = Long.parseLong(edit);
   }
   Login user = userManager.findById(id);
   request.setAttribute("user", user);
   return SUCCESS;
}

public String deleteuser() throws Exception {
   HttpServletRequest request = ServletActionContext.getRequest();
   String del = request.getParameter("del");
   long id = 0l;
   if (!"".equals(del)&&del!=null) {
    id = Long.parseLong(del);
   }
   Login user = userManager.findById(id);
   if (user!=null)
    userManager.deleteUser(user);
   return SUCCESS;
}
public String execute() throws Exception {
   return null;
}
}
7、Login.hbm.xml映射文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.lei.shi.Login" table="LOGIN" schema="SHILEI">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="10" scale="0" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="20" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="20" />
        </property>
    </class>
</hibernate-mapping>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:781次
    • 积分:14
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档