Hibernate+Struts2实现简单登录注册

 


	root
	
		jdbc:mysql://localhost:3306/test
	
	
		org.hibernate.dialect.MySQLDialect
	
	MySQL
	root
	
		com.mysql.jdbc.Driver
	
	
	

 


	
	
		
			
			/success.jsp
			error.jsp
		
		
		
			
			success.jsp
			error.jsp
			register.jsp
		
        
	

 


		struts-cleanup
		
			org.apache.struts2.dispatcher.ActionContextCleanUp
		
	

	
		struts-cleanup
		/*
	

	
		struts2
		
			org.apache.struts2.dispatcher.FilterDispatcher
		
	

	
		struts2
		/*
	

 

package com.lian.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	public static final ThreadLocal session = new ThreadLocal();
	
	static {	
		try {
			sessionFactory = new Configuration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	/*
	 * 获得当前session
	 */
	public static Session getSession() throws HibernateException {
		Session s = (Session) session.get();
		if(s ==null || !s.isOpen()){
			s = sessionFactory.openSession();
			session.set(s);
		}
		return s;	
	}
	
	/*
	 * 回滚
	 */
	public static void rollback(Transaction tx) {
		try {
			if(tx != null) {
				tx.rollback();
			}
		} catch (HibernateException e) {
			System.out.println("rollback faild." + e);
		}
	}
	
	/*
	 * 关闭session
	 */
	public static void closeSession() throws HibernateException {
		Session s = session.get();
		session.set(null);
		if(s != null) {
			s.close();
		}
	}
	
}

 

package com.lian.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.lian.bean.User;
import com.lian.util.HibernateUtil;

public class UserDao {

	private Session session;
	private Transaction tx;
	
	public UserDao() {
		session = HibernateUtil.getSession();
	}
	
	/*
	 * 将User对象插入到数据库中
	 * @param user
	 */
	public void create(User user) {
		try {
			tx = session.beginTransaction();
			session.save(user);
			tx.commit();
		} catch (HibernateException e) {
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession();
		}
	}
	
	public void delete(User user) {
		try {
			tx = session.beginTransaction();
			session.delete(user);
			tx.commit();
		} catch (HibernateException e) {
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession();
		}
	}
	
	public User find(int id) {
		User user = null;
		tx = session.beginTransaction();
		user = (User) session.get(User.class, id);
		tx.commit();
		HibernateUtil.closeSession();
		return user;	
	}
	
	public void update(User user) {
		tx = session.beginTransaction();
		session.update(user);
		tx.commit();
		HibernateUtil.closeSession();
	}
	
	public boolean check(User user) {
		tx = session.beginTransaction();
		String sql = "select u.password from User u where u.username='" +user.getUsername()+ "'";
		List list = session.createQuery(sql).list();
		if(!list.isEmpty()) {
			Iterator it = list.iterator();
			while(it.hasNext()) {
				String get = (String) it.next();
				System.out.println(get);
				if(get.equals(user.getPassword())) {
					HibernateUtil.closeSession();
					return true;
				}
			}
		}
		HibernateUtil.closeSession();
		return false;	
	}
	
	
}

 

 


	
		
			
		
		
		
	

 

package com.lian.action;

import com.lian.bean.User;
import com.lian.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private long id;
	private String username;
	private String password;
	
	@Override
	public String execute() throws Exception {
		UserDao userDao = new UserDao();
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		if(userDao.check(user)) {
			return SUCCESS;
		}
		return ERROR;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}

 

 

 

你可能感兴趣的:(Hibernate+Struts2实现简单登录注册)