1. 程式人生 > >更新表中記錄次數!

更新表中記錄次數!

先查詢表中是否有此條資料,如果有隻更新number(記錄次數)欄位,redListFromMap.put("number",number++); number++不能放在put中。需要單獨給它拿出來。問題解決。

/**
 * 新增紅名單
 * @param
 * @return
 * @throws Exception
 */
@RequestMapping("addRedList")
@ResponseBody
public Object addRedList(GenRedList genRedList,String company) {
    try {
    RedListFromMap redListFromMap = new RedListFromMap();

    GenRedListExample example = new GenRedListExample();
    GenRedListExample.Criteria criteria = example.createCriteria();
    criteria.andCompanyEqualTo(company);

    // select if size>0{不執行insert} else{執行insert 執行update}
    List<GenRedList> genRedLists = genRedListMapper.selectByExample(example);
        if(genRedLists.size()>0){
            Integer number = genRedLists.get(0).getNumber();
            
            redListFromMap.put("number",number++);//錯誤
number++; redListFromMap.put("number",number); //正確 genRedListMapper.editEntity(redListFromMap); }else { redListFromMap.put("id_no", genRedList.getIdNo()); redListFromMap.put("company", genRedList.getCompany()); redListFromMap.put("number", "1"); genRedListMapper.addEntity(redListFromMap); } } catch (Exception e) { throw new SystemException("新增紅名單異常"); } return SUCCESS ; }