mybatis 学习笔记(4) —— 批量新增数据
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) —— 批量新增数据的更多相关文章
- mybatis学习之路----批量更新数据两种方法效率对比
原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...
- mybatis学习之路----mysql批量新增数据
原文:https://blog.csdn.net/xu1916659422/article/details/77971867 接下来两节要探讨的是批量插入和批量更新,因为这两种操作在企业中也经常用到. ...
- MyBatis 学习笔记(七)批量插入ExecutorType.BATCH效率对比
MyBatis 学习笔记(七)批量插入ExecutorType.BATCH效率对比一.在mybatis中ExecutorType的使用1.Mybatis内置的ExecutorType有3种,默认的是s ...
- MyBatis基础入门《十三》批量新增数据
MyBatis基础入门<十三>批量新增数据 批量新增数据方式1:(数据小于一万) xml文件 接口: 测试方法: 测试结果: =============================== ...
- mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)
文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...
- mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)
下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...
- mybatis 学习笔记(四):mybatis 和 spring 的整合
mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...
- Mybatis学习笔记(二) 之实现数据库的增删改查
开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...
- MyBatis:学习笔记(3)——关联查询
MyBatis:学习笔记(3)--关联查询 关联查询 理解联结 SQL最强大的功能之一在于我们可以在数据查询的执行中可以使用联结,来将多个表中的数据作为整体进行筛选. 模拟一个简单的在线商品购物系统, ...
随机推荐
- 【转】 Linux IIO子系统分析-1-概述
原文网址:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=20543672&id=2976189 最近稍微看了下LKML ...
- 【最大流】BAPC2014 A Avoiding the Apocalypse (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- CodeForces 591B
题目链接: http://codeforces.com/problemset/problem/591/B 题意: 给你一串字符串,字符串里的字符全是a-z的小写字母,下面的m行,是字符串的交换方式, ...
- 字符串查找 strstr
strstr函数 分类: LINUX 函数名: strstr 功 能: 在串中查找指定字符串的第一次出现 用 法: char *strstr(char *str1, char *str2); st ...
- poj2299
好吧,看到这个图片就知道是干什么的了,求逆序数- - 可以用线段树,貌似还可以用归并排序,这题应该是考的归并排序,毕竟是递归分治- - 基本上都忘了,再写一写试试吧. AC ///////////// ...
- python面向对象【进阶篇】
静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量 ...
- GitLib
http://www.360doc.com/content/15/0603/14/21631240_475362133.shtml 原文 http://blog.csdn.net/williamwan ...
- CSS3伪类选择器 图示
- 关于 Head First SQL 中文版
我想谈谈 我对于Head First SQL 中文版的一些看法 事实上关于我翻译的这个Head First SQL 中文版..我自觉得:的确翻译得非常烂.. 和翻译Head First ...
- Android自动化测试之Monkey工具
前言:Android自动化测试工具.方法和框架,包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.test tool等. 一. 什么是MonkeyMonk ...