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); //一次要更新多少条数据
    }
 
(三)删除操作
 
(四)新增操作
 
 
 
 
 
 
 
 
 
 
 

 

posted @ 2017-02-18 15:42  Mxzer.Zhang  阅读(3353)  评论(0编辑  收藏  举报