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; } }
最近下载更多
javaee165 LV2
2023年2月15日
upup996 LV6
2022年12月15日
微信网友_6261654233190400 LV2
2022年12月14日
Hsy605 LV9
2022年6月22日
北方菜 LV11
2022年5月11日
湫枫1234 LV4
2022年4月3日
wanglinddad LV54
2022年2月23日
破衣飘飘 LV2
2022年2月9日
yiliao LV1
2022年1月7日
米谷毛 LV4
2021年11月29日
最近浏览更多
WBelong LV7
3月29日
yxzzxy LV3
2月15日
247698755 LV5
1月16日
训码士
2023年12月27日
暂无贡献等级
熬夜选手不能熬夜 LV2
2023年11月14日
Chuiwaitim LV2
2023年10月18日
17558420274 LV16
2023年9月10日
Karen_peng
2023年8月7日
暂无贡献等级
yemmmm666 LV1
2023年6月12日
jierffff LV2
2023年4月20日