1、业务是从前台传入List<T> ,在controller层接受参数,并进行批量新增操作。

2、需要处理的细节

  a) mybatis可以支持批量新增,注意数据表需要将主键设置成自增列。

b) 由于spring mvc 无法将参数[{id:0,text:'a'},{id:1,text:'b'}] json字符串转换为对应的List<T>,因此需要自己手动封装一个方法用于将传入的字符串转换为泛型集合

3、具体实现步骤

  a) js提交

  需要注意的是必须在参数名上加引号

                        var deptPersonList = $('#personList').datagrid('getSelections');
if(deptPersonList.length > 0 ){
var jsonstr = "[";
$.each(deptPersonList,function(index,item){
jsonstr += "{\"departmentId\":\""+selectedId+"\",\"personId\":\""+item.personId+"\"},";
});
jsonstr = jsonstr.substr(0,jsonstr.length-1);
jsonstr += "]"; $.post('setDeptPerson.action', { params: jsonstr }, function (data) {
if (data == true) {
$.messager.alert("提示", "保存成功!");
$("#setDialog").dialog('destroy');
$('#departmentList').datagrid('reload');
} else {
$.messager.alert("提示", "保存失败!");
}
});
}else{
$.messager.alert("提示","请至少选择一条数据进行保存!");
}

  b) controller

  getParameterString为我自定义的封装客户端提交请求数据的公共方法,对HttpServletRequest和HttpServletResponse进行封装

    /**
* 设置部门下人员
* @param departperson
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
@RequestMapping(value="/setDeptPerson",method=RequestMethod.POST)
@ResponseBody
public Boolean setDeptPerson() throws Exception{
String json = getParameterString("params");
List<DepartPerson> departpersonList = formatJsonUtil.readJson(json, List.class, DepartPerson.class);
return departmentService.add(departpersonList);
}  

  c) 参数处理类

package com.frame.core.util;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; public class formatJsonUtil { public static <T> T readJson(String jsonStr,Class<T> collectionClass,Class<?>... elementClass) throws Exception{
ObjectMapper mapper = new ObjectMapper();
JavaType javatype = mapper.getTypeFactory().constructParametricType(collectionClass, elementClass);
return mapper.readValue(jsonStr, javatype);
}
}

  d) mapper.xml配置

    直接上mapper的配置方法,add方法就是mybatis的sqlSession的insert方法的封装

    <!-- 批量插入部门人员对应关系表 -->
<insert id="insertDeptPersons" useGeneratedKeys="true" parameterType="java.util.List">
<selectKey resultType="int" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
INSERT INTO departmentPerson (personId,departmentId)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.personId},#{item.departmentId})
</foreach>
</insert>

  非常简单,也非常直观。对于事务的处理在后面的博文中有具体说明。

mybatis 学习笔记(4) —— 批量新增数据的更多相关文章

  1. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  2. mybatis学习之路----mysql批量新增数据

    原文:https://blog.csdn.net/xu1916659422/article/details/77971867 接下来两节要探讨的是批量插入和批量更新,因为这两种操作在企业中也经常用到. ...

  3. MyBatis 学习笔记(七)批量插入ExecutorType.BATCH效率对比

    MyBatis 学习笔记(七)批量插入ExecutorType.BATCH效率对比一.在mybatis中ExecutorType的使用1.Mybatis内置的ExecutorType有3种,默认的是s ...

  4. MyBatis基础入门《十三》批量新增数据

    MyBatis基础入门<十三>批量新增数据 批量新增数据方式1:(数据小于一万) xml文件 接口: 测试方法: 测试结果: =============================== ...

  5. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

    文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...

  6. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  7. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

  8. Mybatis学习笔记(二) 之实现数据库的增删改查

    开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...

  9. MyBatis:学习笔记(3)——关联查询

    MyBatis:学习笔记(3)--关联查询 关联查询 理解联结 SQL最强大的功能之一在于我们可以在数据查询的执行中可以使用联结,来将多个表中的数据作为整体进行筛选. 模拟一个简单的在线商品购物系统, ...

随机推荐

  1. sed删除文本第一个匹配行

    源文本如下,要求删除第一个为happy-123456的行. ----------------------------- aaaaaaa happy- bbbbbb asdasawe happy- ds ...

  2. 【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

    题目链接: http://codeforces.com/gym/100851 题目大意: 一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小 ...

  3. Invert Binary Tree——LeetCode

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  4. Combination Sum —— LeetCode

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. [Audio processing] wav音频文件合并

    合并多个文件,需要包含1.文件读取和写入功能,2.数组合并 package com.audioprocessingbox.myfunc; import java.io.File; import jav ...

  6. 《ACM国际大学生程序设计竞赛题解Ⅰ》——模拟题

    这篇文章来介绍一些模拟题,即一类按照题目要求将现实的操作转换成程序语言. zoj1003: On every June 1st, the Children's Day, there will be a ...

  7. Jenkins 学习资料

    学习资料: iTech's Blog: Jenkins 入门总结 爱自己: 18篇博客 阳光温暖了心情: 17 篇博客 官网 参考: 构建基于Jenkins + Github的持续集成环境 CI持续集 ...

  8. Solr系列一:Solr与Tomcat的整合

    第一次尝试着去写一个系列的教程,希望自己能坚持下去,也希望自己能够通过博客的编写来加深自己对solr搜索的理解. Solr与Tomcat的整合网上有很多教程,我就以我的整合为例来讲述一下我的整合过程, ...

  9. 实现nodejs的promises库(基于promise.js改写)

    原promise.js库地址:https://github.com/stackp/promisejs promises是JavaScript实现优雅编程的一个非常不错的轻量级框架.该框架可以让你从杂乱 ...

  10. MyISAM与InnoDB的区别

    1. 存储结构: MyISAM:(文件名以表名开始) .frm文件存储表定义 .MYD文件存储数据 .MYI文件存储索引 InnoDB: 所有的表保存在同一个(也可能多个)数据文件中,表的大小仅受限于 ...