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

📄 usersdaoimpl.java

📁 基于SSH的实现用户注册与登入功能。非常适合SSH初学者的入门。其中的设计模式可以应用到任何项目中。
💻 JAVA
字号:
package com.yuanchung.test.dao.users.impl;
import org.springframework.web.context.support.*;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.yuanchung.test.dao.users.UsersDao;
import com.yuanchung.test.exception.myException;
import com.yuanchung.test.model.users.User;
import com.yuanchung.test.util.CONSTANTS;

public class usersDaoImpl extends HibernateDaoSupport implements UsersDao {
	
	public User getUser(String username) {
		//System.out.println(username);
		List listuser;
		Iterator itr = null;
		listuser = getHibernateTemplate().find(
				"from User u where u.username=?", username);
		if (listuser != null && listuser.size() > 0) {
			itr = listuser.iterator();
			return (User) itr.next();
		}
		return null;
	}

	public User getUser(String username, String password) throws myException {

		String[] paramUserList = new String[2];
		paramUserList[0] = username;
		paramUserList[1] = password;
		List listuser;
		List listusername;
		Iterator itr = null;
		try {
			listusername = getHibernateTemplate().find(
					"from User u where u.username=?", username);

			if (listusername != null && listusername.size() > 0) {
				
				listuser = getHibernateTemplate().find(
						"from User u where u.username=? and u.password=?",
						paramUserList);
				if (listuser != null && listuser.size() > 0) {
					itr = listuser.iterator();
					return (User) itr.next();
				} else {
					throw new myException(CONSTANTS.ACCOUNTPASSERROR);
				}

			} else {

				throw new myException(CONSTANTS.LOGINNOUSERERROR);
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			// throw new SelfException(Constants.FINDEXCEPTION);
			return null;
		}
	}

	public void register(String username, String password) {

        User user = new User(username, password);
		try {
			getHibernateTemplate().save(user);
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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