Java后台數據操作(增、刪、改、查)


(一)查詢操作
//Action層:類: Hibernate映射:A--->A.hbm.xml

List stList = new ArrayList();
  stList .add("N");
  stList .add("");
int snId = Integer.parseInt("6"); // ServiceConstants.SN_ID_MXZ="6"
List snIdList = new ArrayList();
  snIdList.add(snId);
List aList = mxzerService.getA(stList , snIdList);
if(!CollectionUtil.isEmpty(aList )){ // 通過工具包處理List對象
  for(A a: aList ){
        // ....具體其他操作
   }
}
工具類: public final class  CollectionUtil {
     public static boolean isEmpty(Collection list){
          if(list==null || list.isEmpty()){
               return true;
          }else{
               return false;
         }
    }
}
// Service + Dao層:

    @Override
    public List getA(List stList, List snIdList) {
        String hql =" from A c where c.statusCode in (:stList) and c.snId in (:snIdList)";
        Session session = super.getSession();
        Query query = session.createQuery(hql.toString());
        query.setParameterList("stList", stList);
        query.setParameterList("snIdList", snIdList);
        //query.setMaxResults(300);// 如限制查詢結果的數量,可加該語句;一次查詢處理多少數據
        return query.list();
    }
 
(二)更新操作
 
// Action層:
  List updateAList = new ArrayList();
  A a = new A();
  updateAList .add(a);
 mxzerService.updateA(updateAList);
 updateAList.clear(); // 清理一次
 
Service + Dao層:
    @Override
    public void updateA(List aList) throws Exception {
        mxzerDao.updateA(aList);
    }
dao層:
public class aImpl extends BaseDao implements IADao{}
    @Override
    public void updateA(List aList) throws Exception {
       super.saveOrUpdateByBatch(aList, 500); //一次要更新多少條數據
    }
 
(三)刪除操作
 
(四)新增操作
 
 
 
 
 
 
 
 
 
 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM