欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
操作數據庫的基類(lèi)
Java代碼
  1. package com.huicui.util;   
  2.   
  3. import java.io.Serializable;   
  4. import java.text.DateFormat;   
  5. import java.text.ParseException;   
  6. import java.util.ArrayList;   
  7. import java.util.Date;   
  8. import java.util.HashMap;   
  9. import java.util.HashSet;   
  10. import java.util.Iterator;   
  11. import java.util.List;   
  12. import java.util.Map;   
  13. import java.util.Set;   
  14.   
  15. import org.apache.log4j.Logger;   
  16. import org.hibernate.Criteria;   
  17. import org.hibernate.HibernateException;   
  18. import org.hibernate.Query;   
  19. import org.hibernate.Session;   
  20. import org.hibernate.Transaction;   
  21. import org.hibernate.criterion.Criterion;   
  22. import org.hibernate.criterion.Expression;   
  23. import org.hibernate.criterion.Order;   
  24.   
  25. import com.dudujava.model.HibernateSessionFactory;   
  26.   
  27. /**  
  28.  *    
  29.  *   
  30.  * @author dudujava  
  31.  *   
  32.  */  
  33.     
  34. public class DBUtil {   
  35.     static Logger logger = Logger.getLogger(DBUtil.class.getName());   
  36.   
  37.     @SuppressWarnings("unchecked")   
  38.     public static void main(String[] args) throws Exception {   
  39.   
  40.     }   
  41.   
  42.     public static Object findByID(String objfullname, int id) {   
  43.         try {   
  44.             return HibernateSessionFactory.getSession().get(objfullname.trim(),   
  45.                     id);   
  46.         } catch (Exception e) {   
  47.             logger.error(e);   
  48.             e.printStackTrace();   
  49.             return null;// TODO: handle exception   
  50.         }   
  51.   
  52.     }   
  53.   
  54.     public static Object findByID(String objfullname, String id) {   
  55.         Integer itemp = null;   
  56.         try {   
  57.             itemp = Integer.parseInt(id);   
  58.         } catch (Exception e) {   
  59.         logger.error(e);    // TODO: handle exception   
  60.         }   
  61.   
  62.         return findByID(objfullname, itemp);   
  63.     }   
  64.     
  65.     public static void modify(String sfullname, int id, Map map) {   
  66.   
  67.         if (map.isEmpty())   
  68.             return;   
  69.            
  70.         try {   
  71.             Object obj = findByID(sfullname, id);   
  72.             save(obj, map);   
  73.         } catch (Exception e) {   
  74.             // TODO Auto-generated catch block   
  75.             logger.error(e);   
  76.             e.printStackTrace();   
  77.         }   
  78.   
  79.     }   
  80.     public static void modify(String sfullname, String sid, Map map) {   
  81.   
  82.             
  83.         modify(sfullname,Integer.parseInt(sid), map);   
  84.     }   
  85.   
  86.     /**  
  87.      * @param obj  
  88.      */  
  89.     public static int save(Object obj) {   
  90.   
  91.         Transaction tx = null;   
  92.         Session session = null;   
  93.   
  94.         try {   
  95.             session = HibernateSessionFactory.getSession();   
  96.             tx = session.beginTransaction();   
  97.             Serializable sObj = session.save(obj);   
  98.             tx.commit();   
  99.             return (Integer.parseInt(sObj.toString()));   
  100.         } catch (HibernateException e) {   
  101.             tx.rollback();   
  102.             e.printStackTrace();   
  103.             logger.error(e);   
  104.             return -1;   
  105.         } finally {   
  106.             try {   
  107.                 session.close();   
  108.             } catch (Exception e) {   
  109.             }   
  110.         }   
  111.   
  112.     }   
  113.   
  114.     /**  
  115.      * @param obj  
  116.      */  
  117.     public static void delete(Object obj) {   
  118.   
  119.         Transaction tx = null;   
  120.         Session session = null;   
  121.         try {   
  122.             session = HibernateSessionFactory.getSession();   
  123.             tx = session.beginTransaction();   
  124.             session.delete(obj);   
  125.             tx.commit();   
  126.         } catch (HibernateException e) {   
  127.             tx.rollback();   
  128.             logger.error(e);   
  129.             e.printStackTrace();   
  130.         } finally {   
  131.             try {   
  132.                 session.close();   
  133.             } catch (Exception e) {   
  134.             }   
  135.         }   
  136.     }   
  137.     /**  
  138.      * @param listobj POJO List  
  139.      */  
  140.     @SuppressWarnings("unchecked")   
  141.     public static void delete(List listobj) {    
  142.         Transaction tx = null;   
  143.         Session session = null;   
  144.         try {   
  145.             session = HibernateSessionFactory.getSession();   
  146.             tx = session.beginTransaction();   
  147.             for (int i = 0; i < listobj.size(); i++) {   
  148.                 session.delete(listobj.get(i));    
  149.             }   
  150.             tx.commit();   
  151.         } catch (HibernateException e) {   
  152.             tx.rollback();   
  153.             e.printStackTrace();   
  154.         } finally {   
  155.             try {   
  156.                 session.close();   
  157.             } catch (Exception e) {   
  158.             }   
  159.         }   
  160.     }   
  161.     /**  
  162.      *   
  163.      * @param sql  HQL   
  164.      *            eg:delete from User where Country.id=1;  
  165.      */  
  166.     public static int modify(String sql) {   
  167.   
  168.         Transaction tx = null;   
  169.         Session session = null;   
  170.         try {   
  171.             session = HibernateSessionFactory.getSession();   
  172.             tx = session.beginTransaction();   
  173.             Query q = session.createQuery(sql);   
  174.             int itmep = q.executeUpdate();   
  175.             tx.commit();   
  176.             return itmep;   
  177.         } catch (HibernateException e) {   
  178.             tx.rollback();   
  179.             e.printStackTrace();   
  180.             return -1;   
  181.         } finally {   
  182.             try {   
  183.                 session.close();   
  184.             } catch (Exception e) {   
  185.             }   
  186.         }   
  187.     }   
  188.     public static int modifyBYHql(String sql) {   
  189.   
  190.         Transaction tx = null;   
  191.         Session session = null;   
  192.         try {   
  193.             session = HibernateSessionFactory.getSession();   
  194.             tx = session.beginTransaction();   
  195.             Query q = session.createQuery(sql);   
  196.             int itmep = q.executeUpdate();   
  197.             tx.commit();   
  198.             return itmep;   
  199.         } catch (HibernateException e) {   
  200.             tx.rollback();   
  201.             e.printStackTrace();   
  202.             return -1;   
  203.         } finally {   
  204.             try {   
  205.                 session.close();   
  206.             } catch (Exception e) {   
  207.             }   
  208.         }   
  209.     }   
  210.   
  211.     public static int modifyBYsql(String sql) {   
  212.   
  213.         Transaction tx = null;   
  214.         Session session = null;   
  215.         try {   
  216.             session = HibernateSessionFactory.getSession();   
  217.             tx = session.beginTransaction();   
  218.             Query q = session.createSQLQuery(sql);   
  219.             int itmep = q.executeUpdate();   
  220.             tx.commit();   
  221.             return itmep;   
  222.         } catch (HibernateException e) {   
  223.             tx.rollback();   
  224.             e.printStackTrace();   
  225.             return -1;   
  226.         } finally {   
  227.             try {   
  228.                 session.close();   
  229.             } catch (Exception e) {   
  230.             }   
  231.         }   
  232.     }   
  233.     /**  
  234.      * @param classname  
  235.      * @param sObjID  
  236.      * @throws Exception  
  237.      */  
  238.     public static void delete(String classname, String sObjID)   
  239.             throws  Exception {   
  240.   
  241.         Transaction tx = null;   
  242.         Session session = null;   
  243.         try {   
  244.             session = HibernateSessionFactory.getSession();   
  245.             tx = session.beginTransaction();   
  246.             Object obj = findByID(classname, sObjID);   
  247.             if (obj != null)   
  248.                 session.delete(obj);   
  249.             tx.commit();   
  250.         } catch (HibernateException e) {   
  251.             tx.rollback();   
  252.             logger.error(e);   
  253.             e.printStackTrace();   
  254.         } finally {   
  255.             try {   
  256.                 session.close();   
  257.             } catch (Exception e) {   
  258.             }   
  259.         }   
  260.     }   
  261.   
  262.     public static void save(List list) {   
  263.   
  264.         Transaction tx = null;   
  265.         Session session = null;   
  266.         try {   
  267.             session = HibernateSessionFactory.getSession();   
  268.             tx = session.beginTransaction();   
  269.             for (int i = 0; i < list.size(); i++) {   
  270.                 session.save(list.get(i));   
  271.             }   
  272.             tx.commit();   
  273.         } catch (HibernateException e) {   
  274.             tx.rollback();   
  275.             e.printStackTrace();   
  276.             logger.error(e);   
  277.         } finally {   
  278.             try {   
  279.                 session.close();   
  280.             } catch (Exception e) {   
  281.             }   
  282.         }   
  283.         logger.debug("save successfull save object number:"+list.size());   
  284.     }   
  285.   
  286.     @SuppressWarnings("unchecked")   
  287.     public static int save(String sname, Map map)  {   
  288.   
  289.         Object objtemp = null;   
  290.         try {   
  291.             objtemp =Class.forName(sname.trim()).newInstance();   
  292.         } catch (Exception e1) {   
  293.             // TODO Auto-generated catch block   
  294.             logger.error(e1);   
  295.             e1.printStackTrace();   
  296.         }   
  297.         return DBUtil.save(objtemp,map);   
  298.   
  299.     }   
  300.        
  301.     @SuppressWarnings("unchecked")   
  302.     public static int save(String sname,String sid, Map map)  {   
  303.   
  304.         Object objtemp = null;   
  305.         try {   
  306.             objtemp =findByID(sname,sid);   
  307.         } catch (Exception e1) {   
  308.             // TODO Auto-generated catch block   
  309.             logger.error(e1);   
  310.             e1.printStackTrace();   
  311.         }   
  312.         return DBUtil.save(objtemp,map);   
  313.   
  314.     }   
  315.   
  316.     @SuppressWarnings("unchecked")   
  317.     public static int save(Object objtemp, Map map) {   
  318.   
  319.         String skey = "";   
  320.         Iterator it = map.keySet().iterator();   
  321.         while (it.hasNext()) {   
  322.   
  323.             skey = it.next().toString().trim();   
  324.             if (skey.length() == 0) {   
  325.                 logger.error("key  not allow NULL ,MAP:" + map);   
  326.                 continue;   
  327.             }   
  328.             Object[] args = new Object[1];   
  329.             args[0] = map.get(skey);   
  330.             try {   
  331.   
  332.                 ReflectUtil.invokeMethod(objtemp, "set"  
  333.                         + skey.substring(01).toUpperCase()   
  334.                         + skey.substring(1), args);   
  335.   
  336.             } catch (Exception e) {   
  337.                 e.printStackTrace();   
  338.                 logger.error(e);   
  339.                 return -1;   
  340.   
  341.             }   
  342.         }   
  343.   
  344.         return DBUtil.save(objtemp);   
  345.   
  346.     }   
  347.   
  348.     public static List searchByHql(String sql) {   
  349.         return HibernateSessionFactory.getSession().createQuery(sql).list();   
  350.   
  351.     }   
  352.   
  353.     public static List searchBySql(String sql) {   
  354.         try {   
  355.             return HibernateSessionFactory.getSession().createSQLQuery(sql)   
  356.                     .list();   
  357.         } catch (Exception e) {   
  358.         e.printStackTrace();       
  359.         System.out.println("your input sql is incorrect!");   
  360.         // TODO: handle exception   
  361.         }   
  362.         return null;   
  363.   
  364.     }   
  365.   
  366.     @SuppressWarnings("unchecked")   
  367.     public static List search(String classname, Map map, int ibegin, int inum)   
  368.          {   
  369.   
  370.         List list = new ArrayList();   
  371.         Session session = null;   
  372.         Transaction tran = null;   
  373.                 
  374.         try {   
  375.             session = HibernateSessionFactory.getSession();   
  376.             tran = session.beginTransaction();   
  377.             Criteria temp = session.createCriteria(Class.forName(classname));   
  378.            
  379.             String skey = "";   
  380.             Iterator it = map.keySet().iterator();   
  381.             while (it.hasNext()) {   
  382.                 Object obj = it.next();   
  383.                 if (obj instanceof List) {   
  384.                     List listTemp = (List) obj;   
  385.                     for (int i = 0; i < listTemp.size(); i++) {   
  386.                         temp = temp.add((Criterion) (listTemp.get(i)));   
  387.                     }   
  388.                     continue;   
  389.                 }   
  390.                 if (obj instanceof Order) {   
  391.                     temp = temp.addOrder((Order) obj);   
  392.                     continue;   
  393.                 }   
  394.   
  395.                 skey = obj.toString();   
  396.                 temp = temp.add(Expression.eq(skey, map.get(skey)));   
  397.   
  398.             }   
  399.             temp.setFirstResult(ibegin);   
  400.             temp.setMaxResults(inum);   
  401.             list = temp.list();   
  402.   
  403.         } catch (Exception e) {   
  404.             
  405.             // e.printStackTrace();   
  406.         } finally {   
  407.   
  408.             session.close();   
  409.         }   
  410.   
  411.         return list;   
  412.   
  413.     }   
  414.   
  415.     /**  
  416.      * @param classname  
  417.      * @param map  
  418.      * @return  
  419.      * @throws Exception  
  420.      */  
  421.     @SuppressWarnings("unchecked")   
  422.     public static List search(String classname, Map map)  {   
  423.         List list = new ArrayList();   
  424.         Session session = null;   
  425.         Transaction tran = null;   
  426.   
  427.         try {   
  428.             session = HibernateSessionFactory.getSession();   
  429.             Criteria temp = session.createCriteria(Class.forName(classname));   
  430.             if (map.isEmpty())   
  431.                 return temp.list();   
  432.             String skey = "";   
  433.             Iterator it = map.keySet().iterator();   
  434.             while (it.hasNext()) {   
  435.                 Object obj = it.next();   
  436.                 if (obj instanceof List) {   
  437.                     List listTemp = (List) obj;   
  438.                     for (int i = 0; i < listTemp.size(); i++) {   
  439.                         temp = temp.add((Criterion) (listTemp.get(i)));   
  440.                     }   
  441.                     continue;   
  442.                 }   
  443.                 if (obj instanceof Order) {   
  444.                     temp = temp.addOrder((Order) obj);   
  445.                     continue;   
  446.                 }   
  447.                 skey = obj.toString();   
  448.                 temp = temp.add(Expression.eq(skey, map.get(skey)));   
  449.   
  450.             }   
  451.             list = temp.list();   
  452.   
  453.         } catch (Exception e) {   
  454.             logger.info(e);   
  455.             e.printStackTrace();   
  456.         } finally {   
  457.   
  458.             session.close();   
  459.   
  460.         }   
  461.   
  462.         return list;   
  463.   
  464.     }   
  465.   
  466.     /**  
  467.      * @param classname  POJO full name  
  468.      * @param map  put restrictions   keys values  
  469.      * @param sprefix  input data prefix  
  470.      * @return  POJO list  
  471.      * @throws Exception  
  472.      */  
  473.     @SuppressWarnings("unchecked")   
  474.     public static List search(String classname, Map map, String sprefix)   
  475.               {   
  476.         List list = new ArrayList();   
  477.         Session session = null;   
  478.         Transaction tran = null;   
  479.         List pc = null;   
  480.             
  481.         try {   
  482.             session = HibernateSessionFactory.getSession();   
  483.             tran = session.beginTransaction();   
  484.             Criteria temp = session.createCriteria(Class.forName(classname));   
  485.             if (map.isEmpty())   
  486.                 return temp.list();   
  487.             temp.setMaxResults(50);   
  488.             String skey = "";   
  489.             map = doFilter(map, sprefix);   
  490.             Iterator it = map.keySet().iterator();   
  491.             while (it.hasNext()) {   
  492.                 Object obj = it.next();   
  493.                 if (obj instanceof List) {   
  494.                     List listTemp = (List) obj;   
  495.                     for (int i = 0; i < listTemp.size(); i++) {   
  496.                         temp = temp.add((Criterion) (listTemp.get(i)));   
  497.                     }   
  498.                     continue;   
  499.                 }   
  500.   
  501.                 if (obj instanceof Order) {   
  502.                     temp = temp.addOrder((Order) obj);   
  503.                     continue;   
  504.                 }   
  505.   
  506.                 skey = obj.toString();   
  507.                 temp = temp.add(Expression.eq(skey, map.get(skey)));   
  508.   
  509.             }   
  510.             list = temp.list();   
  511.   
  512.         } catch (Exception e) {   
  513.             logger.info(e);   
  514.             // e.printStackTrace();   
  515.         } finally {   
  516.   
  517.             session.close();   
  518.         }   
  519.   
  520.         return list;   
  521.   
  522.     }   
  523.   
  524.     @SuppressWarnings("unchecked")   
  525.     public static Map doFilter(Map map) {   
  526.         return doFilter(map, "para");   
  527.   
  528.     }   
  529.   
  530.     @SuppressWarnings("unchecked")   
  531.     public static Map doFilter(Map map, String sprefix) {   
  532.         if (sprefix == null || sprefix.trim().length() == 0)   
  533.             sprefix = "para";   
  534.         return doFilter(map, sprefix, false);   
  535.   
  536.     }   
  537.   
  538.        
  539.   
  540. }  
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
沒(méi)有SPRING,hibernate 延遲加載
java+mysql實(shí)現保存圖片到數據庫,以及讀取數據庫存儲的圖片
dbutils封裝ORM 實(shí)現BaseDAO
一個(gè)簡(jiǎn)單的Hibernate工具類(lèi)HibernateUtil
通向架構師的道路(第七天)之漫談使用ThreadLocal改進(jìn)你的層次的劃分
單例模式(反射+封裝注冊表)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久