package com.lyq.dao; import com.lyq.persistence.Medicine; import com.lyq.util.HibernateFilter; /** * 药品数据库操作类 * * @author Li Yong Qiang */ public class MedicineDao extends SupperDao { /** * 查询药品信息 * * @param id * @return Medicine */ public Medicine loadMedicine(int id) { Medicine med = null; try { session = HibernateFilter.getSession(); // 获取Session对象 session.beginTransaction(); // 开启事物 // 加载药品信息 med = (Medicine) session.load(Medicine.class, new Integer(id)); session.getTransaction().commit(); // 提交事物 } catch (Exception e) { e.printStackTrace(); // 打印异常信息 session.getTransaction().rollback(); // 回滚事物 } return med; } /** * 通过fetch同时抓取药品和类别 * * @param id * @return Medicine */ public Medicine loadMedicineAndCategory(int id) { Medicine med = null; try { session = HibernateFilter.getSession(); // 获取Session对象 session.beginTransaction(); // 开启事物 // HQL查询语句 String hql = "select a from Medicine a join fetch a.category b where a.id = " + id; med = (Medicine) session.createQuery(hql) // 创建Query对象 .uniqueResult(); // 单值检索 session.getTransaction().commit(); // 提交事物 } catch (Exception e) { e.printStackTrace(); // 打印异常信息 session.getTransaction().rollback(); // 回滚事物 } return med; } /** * 根据medNo查询 * * @param medNo * @return Medicine */ public Medicine findMedicineByMedNo(String medNo) { Medicine med = null; try { session = HibernateFilter.getSession(); // 获取Session对象 session.beginTransaction(); // 开启事物 // HQL查询语句 String hql = "from Medicine d where d.medNo = ?"; med = (Medicine) session.createQuery(hql) // 创建Query对象 .setParameter(0, medNo) // 对HQL动态赋值 .uniqueResult(); // 返回单个对象 session.getTransaction().commit(); // 提交事物 } catch (Exception e) { e.printStackTrace(); // 打印异常信息 session.getTransaction().rollback(); // 回滚事物 } return med; } }

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日