package com.hibernate.dao.impl;

import java.util.List;

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

import com.hibernate.dao.UserDao;
import com.hibernate.po.User;

/*
 * 实现类
 */
public class UserDaoImpl implements UserDao {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	// 登录
	public User canLogin(int id, String password) {
		User user = null;
		Session session = sessionFactory.openSession();
		String hql = "from User u where u.id=" + id + " and u.password='"
				+ password + "'";
		Query query = session.createQuery(hql);
		user = (User) query.uniqueResult();
		session.close();
		return user;
	}

	// 查询个人信息
	public User querydetail(int id) {
		User user = null;
		Session session = sessionFactory.openSession();
		String hql = "from User u where u.id=" + id;
		System.out.println(hql);
		Query query = session.createQuery(hql);
		user = (User) query.uniqueResult();
		session.close();
		return user;
	}

	// 修改个人信息详情
	public boolean updatedetail(int id, String nickname, int age, String sex,
			String mobile, String address) {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			String hql = "update User u set u.nickname='" + nickname
					+ "',u.age=" + age + ",u.sex='" + sex + "',u.mobile='"
					+ mobile + "',u.address='" + address + "' where u.id=" + id;
			Query query = session.createQuery(hql);
			int i = query.executeUpdate();
			if (i == 1) {
				tx.commit();
				return true;
			}
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();// 回滚事务
				return false;
			}
			e.printStackTrace();
		} finally {
			session.close();// 关闭Session,释放资源
		}
		return false;
	}

	// 修改个人账户
	public boolean updateaccount(int id, String password, String nickname) {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			String hql = "update User u set u.id=" + id + ",u.password='"
					+ password + "' where u.nickname='" + nickname + "'";
			Query query = session.createQuery(hql);
			int i = query.executeUpdate();
			if (i == 1) {
				tx.commit();
				return true;
			}
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();// 回滚事务
				return false;
			}
			e.printStackTrace();
		} finally {
			session.close();// 关闭Session,释放资源
		}
		return false;
	}

	// 修改昵称相关字段
	public void updateothers(String oldname, String newname) {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			String hql1 = "update Email e set e.receivers='" + newname
					+ "' where e.receivers='" + oldname + "'";
			String hql2 = "update Email e set e.sender='" + newname
					+ "' where e.sender='" + oldname + "'";
			String hql3 = "update Vocation v set v.nickname='" + newname
					+ "' where v.nickname='" + oldname + "'";
			String hql4 = "update Vocation v set v.examname='" + newname
					+ "' where v.examname='" + oldname + "'";
			tx = session.beginTransaction();
			Query query1 = session.createQuery(hql1);
			query1.executeUpdate();
			tx.commit();
			tx = session.beginTransaction();
			Query query2 = session.createQuery(hql2);
			query2.executeUpdate();
			tx.commit();
			tx = session.beginTransaction();
			Query query3 = session.createQuery(hql3);
			query3.executeUpdate();
			tx.commit();
			tx = session.beginTransaction();
			Query query4 = session.createQuery(hql4);
			query4.executeUpdate();
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();// 回滚事务
			}
			e.printStackTrace();
		} finally {
			session.close();// 关闭Session,释放资源
		}
	}

	// 查询全部账户列表
	@SuppressWarnings("unchecked")
	public List<User> getaccountsList() {
		List<User> accountlist = null;
		Session session = sessionFactory.openSession();
		String hql = "from User";
		Query query = session.createQuery(hql);
		accountlist = query.list();
		session.close();
		return accountlist;
	}

	// 添加账户
	public boolean addaccount(User user) {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(user);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();// 回滚事务
				return false;
			}
			e.printStackTrace();
		} finally {
			session.close();// 关闭Session,释放资源
		}
		return false;
	}
}
最近下载更多
Jhhhhh  LV6 2022年11月30日
cccccc12  LV8 2022年5月19日
wanglinddad  LV54 2022年4月5日
15281682932  LV9 2020年7月7日
wyp2530800765  LV4 2020年3月11日
阿三在中国  LV7 2020年3月9日
houliukou  LV37 2020年2月17日
1848245462  LV2 2019年11月20日
zhf929  LV2 2019年10月29日
tianaihua  LV1 2019年9月30日
最近浏览更多
WBelong  LV7 2023年12月27日
bananmike77 2023年10月7日
暂无贡献等级
carloscarlos  LV1 2023年9月14日
8战魂5无双8  LV43 2023年8月20日
zhy1989wz  LV6 2023年7月31日
zhaoka 2023年5月30日
暂无贡献等级
Skeet1111  LV3 2023年5月30日
immeng 2023年5月22日
暂无贡献等级
xiquyiyuan  LV10 2023年3月31日
2716804680  LV9 2023年3月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友