USER

UserForm.java


/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.lxh.myzngt.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * MyEclipse Struts Creation date: 06-30-2007
 * 
 * XDoclet definition:
 * 
 * @struts.form name="userForm"
 */
public class UserForm extends ActionForm {
	/*
	 * Generated fields
	 */

	/** sex property */
	private String sex = "男" ;

	/** usermail property */
	private String usermail;

	/** userques property */
	private String userques;

	/** userid property */
	private String userid;

	/** realname property */
	private String realname;

	/** userans property */
	private String userans;

	/** userpwd property */
	private String userpwd;

	private String confirmpwd;

	private String checkcode;

	/** grade property */
	private String grade;

	/** integral property */
	private String integral;

	/** id property */
	private String id;

	private int type;

	// 1:表示注册功能
	// 2:表示登陆
	// 3:表示忘记密码,确认用户是否存在
	// 4:表示修改密码

	/*
	 * Generated Methods
	 */

	/**
	 * Method validate
	 * 
	 * @param mapping
	 * @param request
	 * @return ActionErrors
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if (type == 1) {
			if (this.userid == null || "".equals(this.userid)) {
				errors.add("userid", new ActionMessage("user.userid.null"));
			}
			if (this.userpwd == null || "".equals(this.userpwd)) {
				errors.add("userpwd", new ActionMessage("user.userpwd.null"));
			} else {
				if (!(this.userpwd.equals(this.confirmpwd))) {
					errors.add("configpwd", new ActionMessage(
							"user.confirmpwd.error"));
				}
			}
			if (this.userques == null || "".equals(this.userques)) {
				errors.add("userques", new ActionMessage("user.userques.null"));
			}
			if (this.userans == null || "".equals(this.userans)) {
				errors.add("userans", new ActionMessage("user.userans.null"));
			}
			if (this.checkcode == null || "".equals(this.checkcode)) {
				errors.add("checkcode", new ActionMessage("checkcode.null"));
			}
		}
		if (type == 2) {
			if (this.userid == null || "".equals(this.userid)) {
				errors.add("userid", new ActionMessage("user.userid.null"));
			}
			if (this.userpwd == null || "".equals(this.userpwd)) {
				errors.add("userpwd", new ActionMessage("user.userpwd.null"));
			}
			if (this.checkcode == null || "".equals(this.checkcode)) {
				errors.add("checkcode", new ActionMessage("checkcode.null"));
			}
		}
		if (type == 3) {
			if (this.userid == null || "".equals(this.userid)) {
				errors.add("userid", new ActionMessage("user.userid.null"));
			}
			if (this.userques == null || "".equals(this.userques)) {
				errors.add("userques", new ActionMessage("user.userques.null"));
			}
			if (this.userans == null || "".equals(this.userans)) {
				errors.add("userans", new ActionMessage("user.userans.null"));
			}
			if (this.checkcode == null || "".equals(this.checkcode)) {
				errors.add("checkcode", new ActionMessage("checkcode.null"));
			}
		}
		if (type == 4) {
			if (this.userid == null || "".equals(this.userid)) {
				errors.add("userid", new ActionMessage("user.userid.null"));
			}
			if (this.userpwd == null || "".equals(this.userpwd)) {
				errors.add("userpwd", new ActionMessage("user.userpwd.null"));
			} else {
				if (!(this.userpwd.equals(this.confirmpwd))) {
					errors.add("configpwd", new ActionMessage(
							"user.confirmpwd.error"));
				}
			}
			if (this.checkcode == null || "".equals(this.checkcode)) {
				errors.add("checkcode", new ActionMessage("checkcode.null"));
			}
		}
		return errors;
	}

	/**
	 * Method reset
	 * 
	 * @param mapping
	 * @param request
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.checkcode = "" ;
	}

	/**
	 * Returns the sex.
	 * 
	 * @return String
	 */
	public String getSex() {
		return sex;
	}

	/**
	 * Set the sex.
	 * 
	 * @param sex
	 *            The sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}

	/**
	 * Returns the usermail.
	 * 
	 * @return String
	 */
	public String getUsermail() {
		return usermail;
	}

	/**
	 * Set the usermail.
	 * 
	 * @param usermail
	 *            The usermail to set
	 */
	public void setUsermail(String usermail) {
		this.usermail = usermail;
	}

	/**
	 * Returns the userques.
	 * 
	 * @return String
	 */
	public String getUserques() {
		return userques;
	}

	/**
	 * Set the userques.
	 * 
	 * @param userques
	 *            The userques to set
	 */
	public void setUserques(String userques) {
		this.userques = userques;
	}

	/**
	 * Returns the userid.
	 * 
	 * @return String
	 */
	public String getUserid() {
		return userid;
	}

	/**
	 * Set the userid.
	 * 
	 * @param userid
	 *            The userid to set
	 */
	public void setUserid(String userid) {
		this.userid = userid;
	}

	/**
	 * Returns the realname.
	 * 
	 * @return String
	 */
	public String getRealname() {
		return realname;
	}

	/**
	 * Set the realname.
	 * 
	 * @param realname
	 *            The realname to set
	 */
	public void setRealname(String realname) {
		this.realname = realname;
	}

	/**
	 * Returns the userans.
	 * 
	 * @return String
	 */
	public String getUserans() {
		return userans;
	}

	/**
	 * Set the userans.
	 * 
	 * @param userans
	 *            The userans to set
	 */
	public void setUserans(String userans) {
		this.userans = userans;
	}

	/**
	 * Returns the userpwd.
	 * 
	 * @return String
	 */
	public String getUserpwd() {
		return userpwd;
	}

	/**
	 * Set the userpwd.
	 * 
	 * @param userpwd
	 *            The userpwd to set
	 */
	public void setUserpwd(String userpwd) {
		this.userpwd = userpwd;
	}

	/**
	 * Returns the grade.
	 * 
	 * @return String
	 */
	public String getGrade() {
		return grade;
	}

	/**
	 * Set the grade.
	 * 
	 * @param grade
	 *            The grade to set
	 */
	public void setGrade(String grade) {
		this.grade = grade;
	}

	/**
	 * Returns the integral.
	 * 
	 * @return String
	 */
	public String getIntegral() {
		return integral;
	}

	/**
	 * Set the integral.
	 * 
	 * @param integral
	 *            The integral to set
	 */
	public void setIntegral(String integral) {
		this.integral = integral;
	}

	/**
	 * Returns the id.
	 * 
	 * @return String
	 */
	public String getId() {
		return id;
	}

	/**
	 * Set the id.
	 * 
	 * @param id
	 *            The id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	public String getConfirmpwd() {
		return confirmpwd;
	}

	public void setConfirmpwd(String confirmpwd) {
		this.confirmpwd = confirmpwd;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getCheckcode() {
		return checkcode;
	}

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}
}



IUserDAO.java


package org.lxh.myzngt.dao;

import java.util.List;

import org.lxh.myzngt.vo.User;

public interface IUserDAO {
	// 按userid查找用户
	public User queryByUserid(String userid) throws Exception;

	// 用户注册
	public void register(User user) throws Exception;

	// 登陆验证
	public boolean login(User user) throws Exception;

	// 找回密码操作 --> 允许用户修改密码
	public void updateUserpwd(String userid, String userpwd) throws Exception;

	// 根据提示问题、答案、用户名确定此用户是否存在
	public boolean isExists(String userid, String userques, String userans)
			throws Exception;

	// 增加得分
	public void addIntegral(String userid, int itergral) throws Exception;

	// 更新用户
	public void update(User user) throws Exception;

	// 列出全部用户
	public List queryAll(int currentPage, int lineSize) throws Exception;

	// 求出全部的记录数
	public int getAllCount() throws Exception;

	// 删除用户
	public void delete(String userid) throws Exception;

	// 修改用户积分
	public void updateIntegral(String userid, int integral) throws Exception;

}



IUserDAOImpl.java


package org.lxh.myzngt.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.lxh.myzngt.dao.IUserDAO;
import org.lxh.myzngt.vo.User;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class IUserDAOImpl extends HibernateDaoSupport implements IUserDAO {

	public void register(User user) throws Exception {
		super.getSession().save(user);
	}

	public User queryByUserid(String userid) throws Exception {
		User user = null;
		String hql = "FROM User AS u WHERE u.userid=?";
		Query q = super.getSession().createQuery(hql);
		q.setString(0, userid);
		List all = q.list();
		if (all.size() > 0) {
			user = (User) all.get(0);
		}
		return user;
	}

	public boolean isExists(String userid, String userques, String userans)
			throws Exception {
		boolean flag = false;
		String hql = "FROM User AS u WHERE u.userid=? AND u.userques=? AND u.userans=?";
		Query q = super.getSession().createQuery(hql);
		q.setString(0, userid);
		q.setString(1, userques);
		q.setString(2, userans);
		List all = q.list();
		if (all.size() > 0) {
			flag = true;
		}
		return flag;
	}

	public boolean login(User user) throws Exception {
		boolean flag = false;
		String hql = "FROM User AS u WHERE u.userid=? AND u.userpwd=?";
		Query q = super.getSession().createQuery(hql);
		q.setString(0, user.getUserid());
		q.setString(1, user.getUserpwd());
		List all = q.list();
		if (all.size() > 0) {
			flag = true;
			User t = (User) all.get(0);
			user.setGrade(t.getGrade());
		}
		return flag;
	}

	public void updateUserpwd(String userid, String userpwd) throws Exception {
		String hql = "UPDATE User SET userpwd=? WHERE userid=?";
		Query q = super.getSession().createQuery(hql);
		q.setString(0, userpwd);
		q.setString(1, userid);
		q.executeUpdate();
	}

	public void addIntegral(String userid, int itergral) throws Exception {
		String hql = "FROM User AS u WHERE u.userid=?";
		Query q = super.getSession().createQuery(hql);
		q.setString(0, userid);
		User user = (User) q.list().get(0);
		user.setIntegral(user.getIntegral() + itergral);
		user.setGrade(org.lxh.myzngt.util.IntegralGrade.getInstance().getGrade(
				user.getIntegral()));
		hql = "UPDATE User SET integral=?,grade=? WHERE userid=?";
		q = super.getSession().createQuery(hql);
		q.setInteger(0, user.getIntegral());
		q.setInteger(1, user.getGrade());
		q.setString(2, userid);
		q.executeUpdate();
	}

	public void update(User user) throws Exception {
		String hql = "UPDATE User SET userpwd=?,userques=?,userans=?,usermail=?,integral=?,grade=?,sex=?,realname=? where userid=?";
		Query q = super.getSession().createQuery(hql);
		q.setString(0, user.getUserpwd());
		q.setString(1, user.getUserques());
		q.setString(2, user.getUserans());
		q.setString(3, user.getUsermail());
		q.setInteger(4, user.getIntegral());
		q.setInteger(5, user.getGrade());
		q.setString(6, user.getSex());
		q.setString(7, user.getRealname());
		q.setString(8, user.getUserid());
		q.executeUpdate();
	}

	public void delete(String userid) throws Exception {
		String hql = "DELETE FROM User WHERE userid=?";
		Query q = super.getSession().createQuery(hql);
		q.setString(0, userid);
		q.executeUpdate();
	}

	public List queryAll(int currentPage, int lineSize) throws Exception {
		List all = null;
		String hql = "FROM User AS u";
		Query q = super.getSession().createQuery(hql);
		q.setFirstResult((currentPage - 1) * lineSize);
		q.setMaxResults(lineSize);
		all = q.list();
		return all;
	}

	public int getAllCount() throws Exception {
		int count = 0;
		String hql = "SELECT COUNT(u.id) FROM User AS u";
		Query q = super.getSession().createQuery(hql);
		count = (Integer) q.list().get(0);
		return count;
	}

	public void updateIntegral(String userid, int integral) throws Exception {
		String hql = "UPDATE User SET integral=?,grade=? WHERE userid=?";
		Query q = super.getSession().createQuery(hql);
		q.setInteger(0, integral);
		q.setInteger(1, org.lxh.myzngt.util.IntegralGrade.getInstance()
				.getGrade(integral));
		q.setString(2, userid);
		q.executeUpdate();
	}

}

你可能感兴趣的:(DAO,apache,Hibernate,MyEclipse,struts)