hibernate实现增删改查的各种方法

1》接口(主要是增删改查的接口)BaseDao.java
  1. /** 
  2.  * 
  3.  * @author fly.zhou 
  4.  */  
  5. public interface IBaseDao {  
  6.   
  7.     //增加对应实体的一条记录    
  8.     public boolean save(Object o);  
  9.   
  10.     //更新对应实体的一条记录  
  11.     public boolean update(Object o);  
  12.   
  13.     //增加或者更新对应实体的一条记录  
  14.     public boolean saveOrUpdate(Object o);  
  15.   
  16.     //保存一系列对象集合  
  17.     public boolean saveOrUpdateAll(Collection l);  
  18.   
  19.     //删除对应实体的一条记录  
  20.     public boolean delete(Object o);  
  21.   
  22.     //根据id删除对应实体的一条记录  
  23.     public boolean delete(Class c, Serializable id);  
  24.   
  25.     //执行hql语句删除一条记录  
  26.     public Integer delete(String hql, Object... values);  
  27.   
  28.     //删除一系列数据  
  29.     public boolean deleteAll(Collection l);  
  30.   
  31.     //执行hql语句查找  
  32.     public List find(String hql);  
  33.   
  34.     //分页查询,多参数条件  
  35.     public List find(String hql, DataGridReq dgr, List values);  
  36.   
  37.     //分页查询,多参数条件  
  38.     public List find(String hql, DataGridReq dgr, Object... values);  
  39.   
  40.     //不带分页查询,多参数条件  
  41.     public List find(String hql, Object... values);  
  42.       
  43.     //不带分页查询,多参数条件  
  44.     public boolean update(String hql, Object... values);  
  45.   
  46.     //根据主键ID查询对应实体的一条记录  
  47.     public Object get(Class clazz, Serializable id);  
  48.   
  49.     //获取某实体对象  
  50.     public Object load(Class c, Serializable id);  
  51.   
  52.     //获取总记录数  
  53.     public Long total(String hql, List values);  
  54.   
  55.     //获取总记录数  
  56.     public Long total(String hql, Object... values);  
  57.   
  58.     //更新对应实体的某一条记录  
  59.     public boolean updateOneByProperty(Class clazz, Serializable id, String pName, Object pValue);  
  60.   
  61.     //更新对应实体的某几条记录  
  62.     public boolean updateOneByPropertys(Class clazz, Serializable id, List<String> pName, List<Object> pValue);  
  63.   
  64.     //更新对应实体的某几条记录(封装成map)  
  65.     public boolean updateOneByPropertys(Class clazz, Serializable id, Map<String, Object> map);  
  66.   
  67.     //根据属性名以及对应的属性值查找一条记录  
  68.     public Object getSingleByProperty(Class clazz, String pName, Object pValue);  
  69.   
  70.     //判断是否有对应的属性名和属性值存在,存在返回true  
  71.     public boolean ifHasOneByProperty(Class clazz, String pName, Object pValue);  
  72.   
  73.     //根据一系列属性以及对应的属性值查询一条记录  
  74.     public Object getSingleByPropertys(Class clazz, List<String> pName, List<Object> pValue);  
  75.   
  76.     //判断是否有一系列对应的属性名和属性值存在,存在返回true  
  77.     public boolean ifHasOneByPropertys(Class clazz, List<String> pName, List<Object> pValue);  
  78.   
  79.     //根据一系列属性以及对应的属性值(封装成Map)查询一条记录  
  80.     public Object getSingleByPropertys(Class clazz, Map<String, Object> map);  
  81.   
  82.     //判断是否有一系列对应的属性名和属性值(封装成Map)存在,存在返回true  
  83.     public boolean ifHasOneByPropertys(Class clazz, Map<String, Object> map);  
  84.   
  85.     //通过某一对应的属性名和属性值,查询某一个属性的值  
  86.     public Object getValueByPropertys(Class clazz, Map<String, Object> map, String selectName);  
  87.   
  88.     //通过一系列对应的属性名和属性值,查询某一个属性的值  
  89.     public Object getValueByProperty(Class clazz, String pName, Object pValue, String selectName);  
  90.   
  91.     //通过一系列对应的属性名和属性值,查询某一个属性的值  
  92.     public Object getValueByPropertys(Class clazz, List<String> pNames, List<Object> pValues, String selectName);  
  93.   
  94.     //查询对应实体的所有记录  
  95.     public List<Object> getObjects(Class clazz);  
  96.   
  97.     //查询符合属性名以及对应的属性值的一系列记录  
  98.     public List<Object> getObjectsByProperty(Class clazz, String pName, Object pValue);  
  99.   
  100.     //根据任意属性查询名以及对应的属性值的一系列记录  
  101.     public List<Object> getObjectsByAnyProperty(Class clazz, List<String> pName, List<Object> pValue);  
  102.   
  103.     //查询符合一系列属性名以及对应的属性值的一系列记录  
  104.     public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<Object> pValue);  
  105.   
  106.     //根据某属性对应的属性值在某一范围内的一系列记录  
  107.     public List<Object> getObjectsByProperty(Class clazz, String pName, String operator, Object pValue);  
  108.   
  109.     //根据某属性对应的属性值在某一范围内的一系列记录  
  110.     public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<String> operator, List<Object> pValue);  
  111. }  


2》接口的实现 BaseDaoImpl.java

  1. public class BaseDaoImpl implements IBaseDao {  
  2.   
  3.     private static final Logger logger = Logger.getLogger(BaseDaoImpl.class);  
  4.     private HibernateTemplate hibernateTemplate;  
  5.   
  6.     public HibernateTemplate getHibernateTemplate() {  
  7.         hibernateTemplate.setCacheQueries(true);// 开启二级查询缓存  
  8.         return hibernateTemplate;  
  9.     }  
  10.   
  11.     @Autowired  
  12.     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
  13.         this.hibernateTemplate = hibernateTemplate;  
  14.     }  
  15.   
  16.     @Override  
  17.     public boolean delete(Object o) {  
  18. //        logger.info("删除");  
  19.         try {  
  20.             this.getHibernateTemplate().delete(o);  
  21.             return true;  
  22.         } catch (Exception e) {  
  23.             return false;  
  24.         }  
  25.     }  
  26.   
  27.     @Override  
  28.     public boolean delete(Class c, Serializable id) {  
  29. //        logger.info("删除");  
  30.         try {  
  31.             this.getHibernateTemplate().delete(get(c, id));  
  32.             return true;  
  33.         } catch (Exception e) {  
  34.             return false;  
  35.         }  
  36.     }  
  37.   
  38.     @Override  
  39.     public Integer delete(final String hql, final Object... values) {  
  40.         return this.getHibernateTemplate().execute(new HibernateCallback<Integer>() {  
  41.   
  42.             @Override  
  43.             public Integer doInHibernate(Session session) throws HibernateException, SQLException {  
  44.                 Query q = session.createQuery(hql);  
  45.                 if (values != null && values.length > 0) {  
  46.                     for (int i = 0; i < values.length; i++) {  
  47.                         q.setParameter(i, values[i]);  
  48.                     }  
  49.                 }  
  50.                 return q.executeUpdate();  
  51.             }  
  52.         });  
  53.     }  
  54.   
  55.     @Override  
  56.     public boolean deleteAll(Collection l) {  
  57. //        logger.info("删除");  
  58.         try {  
  59.             this.getHibernateTemplate().deleteAll(l);  
  60.             return true;  
  61.         } catch (Exception e) {  
  62.             return false;  
  63.         }  
  64.     }  
  65.   
  66.     @Override  
  67.     public boolean update(Object o) {  
  68. //        logger.info("更新");  
  69.         try {  
  70.             this.getHibernateTemplate().update(o);  
  71.             return true;  
  72.         } catch (Exception e) {  
  73.             e.printStackTrace();  
  74.             return false;  
  75.         }  
  76.     }  
  77.   
  78.     @Override  
  79.     public boolean save(Object o) {  
  80. //        logger.info("保存");  
  81.         try {  
  82.             this.getHibernateTemplate().save(o);  
  83.             return true;  
  84.         } catch (Exception e) {  
  85.             return false;  
  86.         }  
  87.     }  
  88.   
  89.     @Override  
  90.     public boolean saveOrUpdate(Object o) {  
  91.         try {  
  92.             this.getHibernateTemplate().saveOrUpdate(o);  
  93.             return true;  
  94.         } catch (Exception e) {  
  95.             return false;  
  96.         }  
  97.     }  
  98.   
  99.     @Override  
  100.     public boolean saveOrUpdateAll(Collection l) {  
  101. //        logger.info("编辑");  
  102.         try {  
  103.             this.getHibernateTemplate().saveOrUpdateAll(l);  
  104.             return true;  
  105.         } catch (Exception e) {  
  106.             return false;  
  107.         }  
  108.     }  
  109.   
  110.     @Override  
  111.     public List find(String hql) {  
  112. //        logger.info("查询");  
  113.         return this.getHibernateTemplate().find(hql);  
  114.     }  
  115.   
  116.     @Override  
  117.     public List find(final String hql, final DataGridReq dgr, final List values) {  
  118.         return this.getHibernateTemplate().execute(new HibernateCallback<List>() {  
  119.   
  120.             @Override  
  121.             public List doInHibernate(Session session) throws HibernateException, SQLException {  
  122.                 Query q = session.createQuery(hql);  
  123.                 if (values != null && values.size() > 0) {  
  124.                     for (int i = 0; i < values.size(); i++) {  
  125.                         q.setParameter(i, values.get(i));  
  126.                     }  
  127.                 }  
  128.                 if (dgr == null) {  
  129.                     return q.list();  
  130.                 }  
  131.                 return q.setFirstResult(dgr.getStart()).setMaxResults(dgr.getLimit() * dgr.getPage()).list();  
  132.             }  
  133.         });  
  134.     }  
  135.   
  136.     @Override  
  137.     public List find(final String hql, final DataGridReq dgr, final Object... values) {  
  138.         return this.getHibernateTemplate().execute(new HibernateCallback<List>() {  
  139.   
  140.             @Override  
  141.             public List doInHibernate(Session session) throws HibernateException, SQLException {  
  142.                 Query q = session.createQuery(hql);  
  143.                 if (values != null && values.length > 0) {  
  144.                     for (int i = 0; i < values.length; i++) {  
  145.                         q.setParameter(i, values[i]);  
  146.                     }  
  147.                 }  
  148.                 if (dgr == null) {  
  149.                     return q.list();  
  150.                 }  
  151.                 return q.setFirstResult(dgr.getStart()).setMaxResults(dgr.getLimit() * dgr.getPage()).list();  
  152.             }  
  153.         });  
  154.     }  
  155.   
  156.     @Override  
  157.     public List find(String hql, Object... values) {  
  158.         return this.getHibernateTemplate().find(hql, values);  
  159.     }  
  160.   
  161.     @Override  
  162.     public boolean update(final String hql, final Object... values) {  
  163.         try {  
  164.             this.getHibernateTemplate().bulkUpdate(hql, values);  
  165.             return true;  
  166.         } catch (Exception e) {  
  167.             return false;  
  168.         }  
  169.     }  
  170.   
  171.     @Override  
  172.     public Object get(Class c, Serializable id) {  
  173.         return this.getHibernateTemplate().get(c, id);  
  174.     }  
  175.   
  176.     @Override  
  177.     public Object load(Class c, Serializable id) {  
  178.         return this.getHibernateTemplate().load(c, id);  
  179.     }  
  180.   
  181.     @Override  
  182.     public Long total(final String hql, final List values) {  
  183.         return this.getHibernateTemplate().execute(new HibernateCallback<Long>() {  
  184.   
  185.             @Override  
  186.             public Long doInHibernate(Session session) throws HibernateException, SQLException {  
  187.                 Query q = session.createQuery(hql);  
  188.                 if (values != null && values.size() > 0) {  
  189.                     for (int i = 0; i < values.size(); i++) {  
  190.                         q.setParameter(i, values.get(i));  
  191.                     }  
  192.                 }  
  193.                 return (Long) q.uniqueResult();  
  194.             }  
  195.         });  
  196.     }  
  197.   
  198.     @Override  
  199.     public Long total(final String hql, final Object... values) {  
  200.         return this.getHibernateTemplate().execute(new HibernateCallback<Long>() {  
  201.   
  202.             @Override  
  203.             public Long doInHibernate(Session session) throws HibernateException, SQLException {  
  204.                 Query q = session.createQuery(hql);  
  205.                 if (values != null && values.length > 0) {  
  206.                     for (int i = 0; i < values.length; i++) {  
  207.                         q.setParameter(i, values[i]);  
  208.                     }  
  209.                 }  
  210.                 return (Long) q.uniqueResult();  
  211.             }  
  212.         });  
  213.     }  
  214.   
  215.     @Override  
  216.     public boolean updateOneByProperty(Class clazz, Serializable id, String pName, Object pValue) {  
  217.         String hql = "update " + clazz.getName() + " entity set entity." + pName + " = '" + pValue + "' where entity.id = " + id;  
  218.         getHibernateTemplate().bulkUpdate(hql);  
  219.         return true;  
  220.     }  
  221.   
  222.     @Override  
  223.     public boolean updateOneByPropertys(Class clazz, Serializable id, List<String> pName, List<Object> pValue) {  
  224.         String hql = "update " + clazz.getName() + " entity set entity.";  
  225.         int maxIndex = pName.size() - 1;  
  226.         for (int i = 0; i < maxIndex; i++) {  
  227.             hql += pName.get(i) + " = '" + pValue.get(i) + "', entity.";  
  228.         }  
  229.         hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'where entity.id = " + id;  
  230.         System.out.println(hql);  
  231.         getHibernateTemplate().bulkUpdate(hql);  
  232.         return true;  
  233.     }  
  234.   
  235.     @Override  
  236.     public boolean updateOneByPropertys(Class clazz, Serializable id, Map<String, Object> map) {  
  237.         String hql = "update " + clazz.getName() + " entity set entity.";  
  238.         Set set = map.entrySet();  
  239.         if (set != null) {  
  240.             Iterator iterator = set.iterator();  
  241.             while (iterator.hasNext()) {  
  242.                 Entry entry = (Entry) iterator.next();  
  243.                 Object key = entry.getKey();  
  244.                 Object value = entry.getValue();  
  245.                 hql += key + " = '" + value + "' and entity.";  
  246.             }  
  247.         }  
  248.         hql = hql.substring(0, hql.length() - 12);  
  249.         hql += "'where entity.id = " + id;  
  250.         System.out.println(hql);  
  251.         getHibernateTemplate().bulkUpdate(hql);  
  252.         return true;  
  253.     }  
  254.   
  255.     /** 
  256.      * 根据属性查询 
  257.      * 
  258.      * @param clazz 
  259.      * @param pName 
  260.      * @param pValue 
  261.      * @return 
  262.      */  
  263.     @Override  
  264.     public Object getSingleByProperty(Class clazz, String pName, Object pValue) {  
  265.   
  266.         String hql = "from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
  267.         List<Object> os = getHibernateTemplate().find(hql);  
  268.         if (os != null && !os.isEmpty()) {  
  269.             return os.get(0);  
  270.         } else {  
  271.             return null;  
  272.         }  
  273.     }  
  274.   
  275.     /** 
  276.      * 根据属性查询 
  277.      * 
  278.      * @param clazz 
  279.      * @param pName 
  280.      * @param pValue 
  281.      * @return 
  282.      */  
  283.     @Override  
  284.     public boolean ifHasOneByProperty(Class clazz, String pName, Object pValue) {  
  285.         String hql = "from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
  286.         List<Object> os = getHibernateTemplate().find(hql);  
  287.         if (os != null && !os.isEmpty()) {  
  288.             return true;  
  289.         } else {  
  290.             return false;  
  291.         }  
  292.     }  
  293.   
  294.     /** 
  295.      * 根据属性查询 
  296.      * 
  297.      * @param clazz 
  298.      * @param pName 
  299.      * @param pValue 
  300.      * @return 
  301.      */  
  302.     @Override  
  303.     public List<Object> getObjectsByAnyProperty(Class clazz, List<String> pName, List<Object> pValue) {  
  304.         String hql = "from " + clazz.getName() + " entity where entity.";  
  305.         int maxIndex = pName.size() - 1;  
  306.         for (int i = 0; i < maxIndex; i++) {  
  307.             hql += pName.get(i) + " = '" + pValue.get(i) + "' or entity.";  
  308.         }  
  309.         hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
  310.         System.out.println(hql);  
  311.         List<Object> os = getHibernateTemplate().find(hql);  
  312.         if (os != null && !os.isEmpty()) {  
  313.             return os;  
  314.         } else {  
  315.             return null;  
  316.         }  
  317.     }  
  318.   
  319.     @Override  
  320.     public boolean ifHasOneByPropertys(Class clazz, List<String> pName, List<Object> pValue) {  
  321.         String hql = "from " + clazz.getName() + " entity where entity.";  
  322.         int maxIndex = pName.size() - 1;  
  323.         for (int i = 0; i < maxIndex; i++) {  
  324.             hql += pName.get(i) + " = '" + pValue.get(i) + "' and entity.";  
  325.         }  
  326.         hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
  327.         System.out.println(hql);  
  328.         List<Object> os = getHibernateTemplate().find(hql);  
  329.         if (os != null && !os.isEmpty()) {  
  330.             return true;  
  331.         } else {  
  332.             return false;  
  333.         }  
  334.     }  
  335.   
  336.     @Override  
  337.     public Object getSingleByPropertys(Class clazz, List<String> pName, List<Object> pValue) {  
  338.   
  339.         String hql = "from " + clazz.getName() + " entity where entity.";  
  340.         int maxIndex = pName.size() - 1;  
  341.         for (int i = 0; i < maxIndex; i++) {  
  342.             hql += pName.get(i) + " = '" + pValue.get(i) + "' and entity.";  
  343.         }  
  344.         hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
  345.         System.out.println(hql);  
  346.         List<Object> os = getHibernateTemplate().find(hql);  
  347.         if (os != null && !os.isEmpty()) {  
  348.             return os.get(0);  
  349.         } else {  
  350.             return null;  
  351.         }  
  352.     }  
  353.   
  354.     @Override  
  355.     public Object getSingleByPropertys(Class clazz, Map<String, Object> map) {  
  356.         String hql = "from " + clazz.getName() + " entity where entity.";  
  357.         Set set = map.entrySet();  
  358.         if (set != null) {  
  359.             Iterator iterator = set.iterator();  
  360.             while (iterator.hasNext()) {  
  361.                 Entry entry = (Entry) iterator.next();  
  362.                 Object key = entry.getKey();  
  363.                 Object value = entry.getValue();  
  364.                 hql += key + " = '" + value + "' and entity.";  
  365.             }  
  366.         }  
  367.         hql = hql.substring(0, hql.length() - 12);  
  368.         System.out.println("hql = " + hql);  
  369.         List<Object> os = getHibernateTemplate().find(hql);  
  370.         if (os != null && !os.isEmpty()) {  
  371.             return os.get(0);  
  372.         } else {  
  373.             return null;  
  374.         }  
  375.     }  
  376.   
  377.     @Override  
  378.     public boolean ifHasOneByPropertys(Class clazz, Map<String, Object> map) {  
  379.   
  380.         String hql = "from " + clazz.getName() + " entity where entity.";  
  381.         Set set = map.entrySet();  
  382.         if (set != null) {  
  383.             Iterator iterator = set.iterator();  
  384.             while (iterator.hasNext()) {  
  385.                 Entry entry = (Entry) iterator.next();  
  386.                 Object key = entry.getKey();  
  387.                 Object value = entry.getValue();  
  388.                 hql += key + " = '" + value + "' and entity.";  
  389.             }  
  390.         }  
  391.         hql = hql.substring(0, hql.length() - 12);  
  392.         List<Object> os = getHibernateTemplate().find(hql);  
  393.         if (os != null && !os.isEmpty()) {  
  394.             return true;  
  395.         } else {  
  396.             return false;  
  397.         }  
  398.     }  
  399.   
  400.     /** 
  401.      * 查询所有 
  402.      */  
  403.     @Override  
  404.     public List<Object> getObjects(Class clazz) {  
  405.         String hql = "from " + clazz.getName();  
  406.         List list = getHibernateTemplate().find(hql);  
  407.         return list;  
  408.     }  
  409.   
  410.     /** 
  411.      * 根据属性查询 全部 
  412.      * 
  413.      * @param clazz 
  414.      * @param pName 
  415.      * @param pValue 
  416.      * @return 
  417.      */  
  418.     @Override  
  419.     public List<Object> getObjectsByProperty(Class clazz, String pName, Object pValue) {  
  420.         String hql = "from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
  421.         return getHibernateTemplate().find(hql);  
  422.     }  
  423.   
  424.     @Override  
  425.     public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<Object> pValue) {  
  426.         String hql = "from " + clazz.getName() + " entity where entity.";  
  427.         int maxIndex = pName.size() - 1;  
  428.         for (int i = 0; i < maxIndex; i++) {  
  429.             hql += pName.get(i) + " = '" + pValue.get(i) + "' and ";  
  430.         }  
  431.         hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
  432.         return getHibernateTemplate().find(hql);  
  433.     }  
  434.   
  435.     /** 
  436.      * 根据属性查询 全部 
  437.      * 
  438.      * @param clazz 
  439.      * @param pName 
  440.      * @param pValue 
  441.      * @return 
  442.      */  
  443.     @Override  
  444.     public List<Object> getObjectsByProperty(Class clazz, String pName, String operator, Object pValue) {  
  445.         String hql = "from " + clazz.getName() + " entity where entity." + pName + " " + operator + pValue;  
  446.         return getHibernateTemplate().find(hql);  
  447.     }  
  448.   
  449.     @Override  
  450.     public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<String> operator, List<Object> pValue) {  
  451.         String hql = "from " + clazz.getName() + " entity where entity.";  
  452.         int maxIndex = pName.size() - 1;  
  453.         for (int i = 0; i < maxIndex; i++) {  
  454.             hql += pName.get(i) + " " + operator.get(i) + " '" + pValue.get(i) + "' and ";  
  455.         }  
  456.         hql += pName.get(maxIndex) + " " + operator.get(maxIndex) + " '" + pValue.get(maxIndex) + "'";  
  457.         System.out.println("hql = " + hql);  
  458.         return getHibernateTemplate().find(hql);  
  459.     }  
  460.   
  461.     @Override  
  462.     public Object getValueByPropertys(Class clazz, Map<String, Object> map, String selectName) {  
  463.         String hql = "select entity." + selectName + "from " + clazz.getName() + " entity where entity.";  
  464.         Set set = map.entrySet();  
  465.         if (set != null) {  
  466.             Iterator iterator = set.iterator();  
  467.             while (iterator.hasNext()) {  
  468.                 Entry entry = (Entry) iterator.next();  
  469.                 Object key = entry.getKey();  
  470.                 Object value = entry.getValue();  
  471.                 hql += key + " = '" + value + "' and entity.";  
  472.             }  
  473.         }  
  474.         hql = hql.substring(0, hql.length() - 12);  
  475.         System.out.println("hql = " + hql);  
  476.         return getHibernateTemplate().find(hql);  
  477.     }  
  478.   
  479.     @Override  
  480.     public Object getValueByProperty(Class clazz, String pName, Object pValue, String selectName) {  
  481.         String hql = "select entity." + selectName + " from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
  482.         System.out.println("hql = " + hql);  
  483.         return getHibernateTemplate().find(hql);  
  484.     }  
  485.   
  486.     @Override  
  487.     public Object getValueByPropertys(Class clazz, List<String> pNames, List<Object> pValues, String selectName) {  
  488.         String hql = "select entity." + selectName + "from " + clazz.getName() + " entity where entity.";  
  489.         int maxIndex = pNames.size() - 1;  
  490.         for (int i = 0; i < maxIndex; i++) {  
  491.             hql += pNames.get(i) + " = '" + pValues.get(i) + "' and ";  
  492.         }  
  493.         hql += pNames.get(maxIndex) + " = '" + pValues.get(maxIndex) + "'";  
  494.         System.out.println("hql = " + hql);  
  495.         return getHibernateTemplate().find(hql);  
  496.     }  
  497. }  


3》值得注意的地方Map如何获取key

  1. Set set = map.entrySet();  
  2. if (set != null) {  
  3.     Iterator iterator = set.iterator();  
  4.     while (iterator.hasNext()) {  
  5.         Entry entry = (Entry) iterator.next();  
  6.         Object key = entry.getKey();  
  7.         Object value = entry.getValue();  
  8.         hql += key + " = '" + value + "' and entity.";  
  9.     }  
  10. }  

4》这样写的话 会对项目省去大量的重复代码,在对具体的实体操作时,我们只需要把对应实体的实现继承BaseDaoImpl。

  1. public class XXXDaoImpl extends BaseDaoImpl implements IXXXDao{}  

当然我们也可以在XXXDaoImpl里面添加此实体特殊的实现方法。

5》对于hibernate,有时候为了提高查询和执行效率,我们只需要获取部分数据,而不需要拿出关联数据或者是整行的所有数据,这时候我们可以构造函数来解决这个问题。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值