package com.lyq.dao;

import java.util.Date;

import org.hibernate.Query;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import com.lyq.persistence.Category;
import com.lyq.persistence.Medicine;
import com.lyq.persistence.User;
import com.lyq.util.HibernateFilter;
/**
 * �û���ݿ������
 * ���ڲ�ѯ�û���ϵͳ��ʼ��
 * @author Li Yong Qiang
 */
public class UserDao extends SupperDao {
	/**
	 * ��ѯ�û�
	 * @param userName
	 * @param password
	 * @return User
	 */
	public User login(String userName,String password){
		User user = null;
		try {
			session = HibernateFilter.getSession();		//��ȡSession����
			session.beginTransaction();					//��������
			//HQL��ѯ���
			String hql = "from User u where u.username=? and u.password=?";
			Query query = session.createQuery(hql)		//����Query����
								.setParameter(0, userName)//��̬��ֵ
								.setParameter(1, password);//��̬��ֵ
			user = (User)query.uniqueResult();			//����User����
			session.getTransaction().commit();			//�ύ����
		} catch (Exception e) {
			e.printStackTrace();						//��ӡ�쳣��Ϣ
			session.getTransaction().rollback();		//�ع�����
		}
		return user;
	}
	/**
	 * ���id��ѯ�û�
	 * @param id
	 * @return User
	 */
	public User loadUser(int id){
		User user = null;
		try {
			session = HibernateFilter.getSession();		//��ȡSession����
			session.beginTransaction();					//��������
			//���id�����û�
			user = (User)session.load(User.class, new Integer(id));
			session.getTransaction().commit();			//�ύ����
		} catch (Exception e) {
			e.printStackTrace();						//��ӡ�쳣��Ϣ
			session.getTransaction().rollback();		//�ع�����
		}
		return user;
	}
	/**
	 * ϵͳ��ʼ�����
	 */
	public void initialization(){
		try {
			Configuration cfg = new Configuration().configure();
			SchemaExport export = new SchemaExport(cfg);
			export.create(true, true);
			session = HibernateFilter.getSession();		//��ȡSession����
			session.beginTransaction();					//��������

			Category c1 = new Category();
			c1.setName("��ð��ҩ");
			c1.setDescription("���θ�ð�����ա�ͷʹ��");
			c1.setCreateTime(new Date());
			
			Category c2 = new Category();
			c2.setName("θ����ҩ");
			c2.setDescription("θ�ס�����ר��ҩ��");
			c2.setCreateTime(new Date());
			
			Category c3 = new Category();
			c3.setName("��ͯ��ҩ");
			c3.setDescription("���ã���ͯ��ҩ��");
			c3.setCreateTime(new Date());
			
			Medicine d1 = new Medicine();
			d1.setName("����A");
			d1.setPrice(2.5);
			d1.setMedCount(3);
			d1.setCategory(c1);
			d1.setFactoryAdd("��ҩһ��");
			d1.setDescription("Ч��ܺ�");
			d1.setMedNo("abc001");
			
			Medicine d2 = new Medicine();
			d2.setName("����B");
			d2.setPrice(10.05);
			d2.setMedCount(10);
			d2.setCategory(c1);
			d2.setFactoryAdd("��ҩһ��");
			d2.setDescription("�����˷硢ͷʹЧ��ܺ�");
			d2.setMedNo("abc002");
			
			Medicine d3 = new Medicine();
			d3.setName("xx������");
			d3.setPrice(5.8);
			d3.setMedCount(100);
			d3.setCategory(c2);
			d3.setFactoryAdd("��ҩ����");
			d3.setDescription("����������");
			d3.setMedNo("abc003");
			
			Medicine d4 = new Medicine();
			d4.setName("С���ð���");
			d4.setPrice(5.8);
			d4.setMedCount(100);
			d4.setCategory(c3);
			d4.setFactoryAdd("��ҩ��");
			d4.setDescription("Ч��ܺ�");
			d4.setMedNo("abc004");
			
			User u = new User();
			u.setUsername("admin");
			u.setPassword("admin");
			u.setCreateTime(new Date());
			
			session.save(d1);
			session.save(d2);
			session.save(d3);
			session.save(d4);
			session.save(u);
			
			session.getTransaction().commit();			//�ύ����
		} catch (Exception e) {
			session.getTransaction().rollback();		//�ع�����
			e.printStackTrace();
		}finally{
			if(session != null){
				if(session.isOpen()){
					session.close();
				}
			}
		}
	}
}
最近下载更多
krispeng  LV14 2024年11月19日
微信网友_6914427966033920  LV4 2024年3月19日
pangxiangdong  LV4 2023年5月31日
2931765614  LV2 2022年11月14日
wangmeijing  LV5 2022年10月13日
xlg0717  LV5 2022年9月30日
地上有草  LV12 2022年5月11日
葡萄树下酒庄  LV9 2022年1月4日
778123511  LV2 2021年12月9日
1532871844  LV3 2021年12月3日
最近浏览更多
苯科仔 2024年12月16日
暂无贡献等级
plusangel 2024年8月29日
暂无贡献等级
柳咪华沙  LV7 2024年6月28日
微信网友_6914427966033920  LV4 2024年3月19日
iiiiiiixiiiiii  LV1 2023年12月29日
asddwh  LV13 2023年12月26日
hongdongdong  LV14 2023年12月2日
阿桐吖  LV2 2023年11月18日
马宇轩  LV1 2023年11月14日
Weishenghui  LV7 2023年10月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友