批量插入。AJAX发起请求,核心代码如下:
var mids = new Array(); for (var i=0; i< rows.length; i++) { mids.push(rows[i].id); } $.ajax({ type: 'POST', dataType: "json", async: false, traditional:true, //这一行很重要 url: parent.getBasePath()+"sys/role/setAuth.do", data: {'mids': mids}, success: function(data) {}, error: function(err) {} });
接下来是Controller的代码:
//直接在方法里传入List会报错,所以要通过一个类封装一层 @RequestMapping("/setAuth") @ResponseBody public Map<String,Object> setAuth(Short id, ModParam midsA) { } //封装List对象的类 public class ModParam implements Serializable{ private static final long serialVersionUID = 8607859409880232081L; private List<Short> mids; public List<Short> getMids() { return mids; } public void setMids(List<Short> mids) { this.mids = mids; } }
Contoller一层一层的往下调用,最后到了Mapper:
/*** * 批量插入 * @return * @throws Exception */ int insertWithList(List<RoleResource> rr) throws Exception;
对应的xml(我这里的id不是一个自增的Sequence,我是去表中选取最大的id,然后依次加1,写入到对象,所以没有用到mybatis的selectKey,这里是针对Oracle):
<insert id="insertWithList" parameterType="java.util.List"> insert into T_WEB_ROLE_RESOURCE(id, role_id, source_id, remark) <foreach collection="list" item="item" index="index" separator="union all"> select #{item.id},#{item.roleId},#{item.sourceId},#{item.remark} from dual </foreach> </insert>
嗯,这就差不多了。
附上一个参考链接:
http://chenzhou123520.iteye.com/blog/1583407
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/8002.html