天天看點

Mybatis Oracle資料庫批量更新資料

最近在項目中用到了批量資料的問題,記錄一下

第一層、控制層代碼:

@RestController
@RequestMapping("/dsDetailEntity")
public class demmoController {
    @Autowired
   	DmDsDetailService dmDsDetailService; // 注入業務層
   	
    @ApiOperation(value="編輯資料集合明細") // 使用了swagger注解
    @ApiImplicitParams({
       @ApiImplicitParam(name = "dmDsId", value = "集合主鍵ID", dataType = "String", required = true),
       @ApiImplicitParam(name = "dataEleIds", value = "資料元主鍵ID", dataType = "String", allowMultiple = true) 
    })
    @PutMapping(value="/update/dmDsDetail")
    public ResultMessage updateDmDsDetail(@RequestParam(value = "dataEleIds") List<String> dataEleIds,String dmDsId){
      	return dmDsDetailService.updateDmDsDetail(dataEleIds, dmDsId);
    }
}
           

第二層:業務層代碼

package com.mxx.demo.base;
import com.baomidou.mybatisplus.extension.service.IService;
  /**
   * 所有服務層接口的父接口
   * @param <T> 操作所綁定的Entity
   */
public interface SuperService<T> extends IService<T> { // 繼承了mybatis-plus的service接口
    // 可以封裝一下公用的接口方法
    	
    /**
      * 異步插入一條記錄
      * @param entity 實體對象
      */
      boolean asyncSave(T entity);
}

        
package com.mxx.demo.service;
// service層接口 (此處可以繼承也可以不繼承SuperService)
public interface DmDsDetailService extends SuperService<DmDsDetailEntity> { 
      // 編輯資料集合明細
      ResultMessage updateDmDsDetail(List<String> dataEleIds,String dmDsId);
}
---- --------------------------------------------- 

package com.mxx.demo.serviceImpl;
@Transactional
@Service
public class DmDsDetailServiceImpl  implements DmDsDetailService {
    @Autowired
	DmDsDetailMapper dmDsDetailMapper ;  
	
    // 編輯資料集明細
	@Override
	public ResultMessage updateDmDsDetail(List<String> dataEleIds, String dmDsId) {
           dmDsDetailMapper .updateDataEleOrderFlag(dataEleIds , dmDsId);
     }
}
           

第三層:mapper層

package com.mxx.demo.base;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
 * 所有 mapper 父類,注意這個類不要讓 mp 掃描到!!
 */
public interface SuperMapper<T> extends BaseMapper<T> {
    // 這裡可以放一些公共的方法
}


package com.mxx.demo.mapper;
//  Mapper層映射接口,繼承了mybatis-plus的BaseMapper類(這樣就可以使用mybatis-plus中封裝好的方法了)
public interface DmDsDetailMapper extends SuperMapper<DmDsDetailEntity> {
      // 編輯資料集明細
      int updateDataEleOrderFlag(@Param("updateList") List<DmDsDetailEntity> updateList );
}
    
----------------- mapper映射檔案(字段名和實體類名用resultMap進行轉換映射)
  <!-- 編輯資料集明細 (所有參數都是實體類DmDsDetailEntity中的)-->
<update id="updateDataEleOrderFlag" parameterType="java.util.List">
      <foreach collection="updateList" item="item" index="index"  open="begin" close="end;">
		 update HJ_MDM_DM_DS_DETAIL
		 <set>
		     data_ele_order_flag = #{item.dataEleOrderFlag}
		 </set>
		 where data_ele_id = #{item.dataEleId} 
		        and dm_ds_id= #{item.dmDsId} 
		        and invalid_flag = #{item.invalidFlag};
       </foreach>
</update>
           

繼續閱讀