网站建议:179001057@qq.com

java 封装基于hibernate的数据库操作

技术2022-05-12  1

java 封装基于hibernate的数据库操作。包括:查询,插入,更新。

import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.hibernate.LockMode; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class CommonDao extends HibernateDaoSupport { private Logger log = Logger.getLogger(CommonDao.class); //日志纪录 public List getData(String sql, Map mapWhere){ Session session = null; Query query = null; List list = null; try { session = this.getSessionFactory().openSession(); query = session.createSQLQuery(sql); if(mapWhere != null){ Iterator<String> it = mapWhere.keySet().iterator(); while(it.hasNext()){ String key = it.next(); Object o = mapWhere.get(key); if (o instanceof Integer) { query.setInteger(key, (Integer) o); } else if (o instanceof Double) { query.setDouble(key, (Double) o); } else { query.setString(key, (String) o); } } } list = query.list(); } catch (Exception ex) { log.info("对不起,没有符合条件的记录:" + ex.getMessage()); } finally { try { if (session != null) { session.close(); session = null; } } catch (Exception ex) { log.info("无法正常关闭Session:" + ex.getMessage()); } } return list; } public boolean insertData(Object obj){ Session session = null; Transaction tx = null; try{ session = this.getSessionFactory().openSession(); tx = session.beginTransaction(); // 插入一行 session.save(obj); tx.commit(); session.close(); }catch(Exception ex){ log.info("数据插入失败:" + ex.getMessage()); tx.rollback(); return false; } finally { try { if (session != null) { session.close(); session = null; } } catch (Exception ex) { log.info("无法正常关闭Session:" + ex.getMessage()); } } return true; } public boolean updateData(Class cls, String key, Map updateMap, String updateFlg, String updateErr){ Session session = null; Transaction tx = null; Object obj = null; try{ session = this.getSessionFactory().openSession(); tx = session.beginTransaction(); // 获取要更新的记录 obj = session.load(cls, key, LockMode.UPGRADE); // 判定是否可以更新 String updateVal = (String)doGetMethod(cls, obj, updateFlg); if(updateErr.equals(updateVal)){ throw new Exception("该记录已经被其他用户更新。"); } // 编辑更新字段 if(updateMap != null){ Iterator<String> it = updateMap.keySet().iterator(); while (it.hasNext()) { // 取得键值 String fieldName = it.next(); doSetMethod(cls, obj, fieldName, updateMap.get(fieldName)); } } session.saveOrUpdate(obj); tx.commit(); session.close(); }catch(Exception ex){ log.info("数据更新失败:" + ex.getMessage()); tx.rollback(); return false; } finally { try { if (session != null) { session.close(); session = null; } } catch (Exception ex) { log.info("无法正常关闭Session:" + ex.getMessage()); } } return true; } private void doSetMethod(Class cls, Object obj, String fieldName, Object fieldVal)throws Exception{ // 获取字段 Field fld = cls.getDeclaredField(fieldName); // 拼接set方法名 String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); // 获取方法名 Method mtd = cls.getDeclaredMethod(methodName, fld.getType()); // 调用方法赋值 mtd.invoke(obj, fieldVal); } private Object doGetMethod(Class cls, Object obj, String fieldName)throws Exception{ // 获取字段 Field fld = cls.getDeclaredField(fieldName); // 拼接get方法名 String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); // 获取方法名 Method mtd = cls.getDeclaredMethod(methodName); // 调用方法取值 Object rel = mtd.invoke(obj); return rel; } }


最新回复(0)