⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdao.java

📁 struts+spring+hibernate例子
💻 JAVA
字号:
package com.shop.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.shop.entity.*;

/**
 * A data access object (DAO) providing persistence and search support for User
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.shop.hibernates.User
 * @author MyEclipse Persistence Tools
 */

public class UserDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String SEX = "sex";
	public static final String NICKNAME = "nickname";
	public static final String EMAIL = "email";
	public static final String EMAILSECRET = "emailsecret";
	public static final String MOBILE = "mobile";
	public static final String PHONE = "phone";
	public static final String HOMEPAGE = "homepage";
	public static final String COMEFROM = "comefrom";
	public static final String MSN = "msn";
	public static final String QQ = "qq";
	public static final String SKYPE = "skype";
	public static final String ICQ = "icq";
	public static final String YAHOO = "yahoo";
	public static final String SELFSHOW = "selfshow";
	public static final String SIGNATURE = "signature";
	public static final String USESIGN = "usesign";
	public static final String USEPORTRAIT = "useportrait";
	public static final String PORTRAIT = "portrait";
	public static final String PORTRAITADDRESS = "portraitaddress";
	public static final String PORTRAITWIDTH = "portraitwidth";
	public static final String PORTRAITHEIGHT = "portraitheight";
	public static final String ANSWER = "answer";
	public static final String TOPICNUM = "topicnum";
	public static final String POSTNUM = "postnum";
	public static final String TIPWAVE = "tipwave";
	public static final String RECEIVEEMAIL = "receiveemail";
	public static final String HIDEN = "hiden";
	public static final String STATE = "state";
	public static final String MEDAL = "medal";
	public static final String DOUNUM = "dounum";
	public static final String FORUMPOINT = "forumpoint";
	public static final String PAYPOINT = "paypoint";
	public static final String READACCESS = "readaccess";
	
	private Session session = null;
	private Transaction transaction = null;
	
	public UserDAO() {
		session = getSession();
		transaction = session.beginTransaction();
	}
	
	/**
	 * 获得某用户名的密码。
	 * @param username 用户名
	 * @return 返回该用户名的密码
	 */
	public String getPassword(String username){
		String hql = "select u.password from User u where u.username='"+username+"'";
		Query query = session.createQuery(hql);
			return query.list().get(0).toString();
	}
	
	/**
	 * 检测用户名为username的是否为本站的注册会员。
	 * @param username 用户名
	 * @return 返回注册否。
	 */
	public boolean isRegisterUser(String username){
		String hql = "select u.username from User u where u.username='"+username+"'";
		Query query = session.createQuery(hql);
		if(query!=null && query.list()!=null && query.list().size()>0)
			return true;
		else
			return false;
	}
	
	public User findUserByUsername(String username){
		User user = null;
		List list = findByUsername(username);
		if(list!=null && list.size()>0){
			user = (User)list.get(0);
		}
		return user;
	}
	
	public void save(User transientInstance) {
		log.debug("saving User instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(User persistentInstance) {
		log.debug("deleting User instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public User findById(java.lang.Integer id) {
		log.debug("getting User instance with id: " + id);
		try {
			User instance = (User) getSession().get("tarena.hibernates.User",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(User instance) {
		log.debug("finding User instance by example");
		try {
			List results = getSession()
					.createCriteria("tarena.hibernates.User").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from User as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByNickname(Object nickname) {
		return findByProperty(NICKNAME, nickname);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByEmailsecret(Object emailsecret) {
		return findByProperty(EMAILSECRET, emailsecret);
	}

	public List findByMobile(Object mobile) {
		return findByProperty(MOBILE, mobile);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByHomepage(Object homepage) {
		return findByProperty(HOMEPAGE, homepage);
	}

	public List findByComefrom(Object comefrom) {
		return findByProperty(COMEFROM, comefrom);
	}

	public List findByMsn(Object msn) {
		return findByProperty(MSN, msn);
	}

	public List findByQq(Object qq) {
		return findByProperty(QQ, qq);
	}

	public List findBySkype(Object skype) {
		return findByProperty(SKYPE, skype);
	}

	public List findByIcq(Object icq) {
		return findByProperty(ICQ, icq);
	}

	public List findByYahoo(Object yahoo) {
		return findByProperty(YAHOO, yahoo);
	}

	public List findBySelfshow(Object selfshow) {
		return findByProperty(SELFSHOW, selfshow);
	}

	public List findBySignature(Object signature) {
		return findByProperty(SIGNATURE, signature);
	}

	public List findByUsesign(Object usesign) {
		return findByProperty(USESIGN, usesign);
	}

	public List findByUseportrait(Object useportrait) {
		return findByProperty(USEPORTRAIT, useportrait);
	}

	public List findByPortrait(Object portrait) {
		return findByProperty(PORTRAIT, portrait);
	}

	public List findByPortraitaddress(Object portraitaddress) {
		return findByProperty(PORTRAITADDRESS, portraitaddress);
	}

	public List findByPortraitwidth(Object portraitwidth) {
		return findByProperty(PORTRAITWIDTH, portraitwidth);
	}

	public List findByPortraitheight(Object portraitheight) {
		return findByProperty(PORTRAITHEIGHT, portraitheight);
	}

	public List findByAnswer(Object answer) {
		return findByProperty(ANSWER, answer);
	}

	public List findByTopicnum(Object topicnum) {
		return findByProperty(TOPICNUM, topicnum);
	}

	public List findByPostnum(Object postnum) {
		return findByProperty(POSTNUM, postnum);
	}

	public List findByTipwave(Object tipwave) {
		return findByProperty(TIPWAVE, tipwave);
	}

	public List findByReceiveemail(Object receiveemail) {
		return findByProperty(RECEIVEEMAIL, receiveemail);
	}

	public List findByHiden(Object hiden) {
		return findByProperty(HIDEN, hiden);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByMedal(Object medal) {
		return findByProperty(MEDAL, medal);
	}

	public List findByDounum(Object dounum) {
		return findByProperty(DOUNUM, dounum);
	}

	public List findByForumpoint(Object forumpoint) {
		return findByProperty(FORUMPOINT, forumpoint);
	}

	public List findByPaypoint(Object paypoint) {
		return findByProperty(PAYPOINT, paypoint);
	}

	public List findByReadaccess(Object readaccess) {
		return findByProperty(READACCESS, readaccess);
	}

	public List findAll() {
		log.debug("finding all User instances");
		try {
			String queryString = "from User";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public User merge(User detachedInstance) {
		log.debug("merging User instance");
		try {
			User result = (User) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(User instance) {
		log.debug("attaching dirty User instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(User instance) {
		log.debug("attaching clean User instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -