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最强大的功能之一在于我们可以在数据查询的执行中可以使用联结,来将多个表中的数据作为整体进行筛选. 模拟一个简单的在线商品购物系统, ...
随机推荐
- 解决weblogic Managed Server启动非常慢的情况
jdk版本:1.7.0_79 查看控制台日志停留在如下地方: . . JAVA Memory arguments: -Xms2048m -Xmx4096m -XX:MaxPermSize=512m . ...
- AndroidStudio SVN检出
版本管理是每个项目的必经之路,很多的ADT都会集成版本管理插件.AS也同样可以集成GITHUB和SVN插件.github对项目有一定的限制,而SVN就比较开放了,所以我们在用AS开发的时候一般用SVN ...
- 《University Calculus》-chaper12-多元函数-拉格朗日乘数法
求解条件极值的方法:拉格朗日乘数法 基于对多元函数极值方法的了解,再具体的问题中我们发现这样一个问题,在求解f(x,y,z)的极值的时候,我们需要极值点落在g(x,y,z)上这种对极值点有约束条件,通 ...
- FindControl的使用方法
Control.FindControl (String):在当前的命名容器中搜索带指定 id参数的服务器控件.(有点类似javascript中的getElementById(string)) 简单的例 ...
- js 魔鬼训练
1.Object.assign 偷梁换柱 / 融合 - 将多个对象合并到第一个对象中去.这样一来methods对象中就包含着data对象了.否则this无法正常访问data中的title var ne ...
- nginx的upstream目前支持5种方式的分配
Nginx nginx的upstream目前支持5种方式的分配 FROM: 转载 1 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器, 如果后端服务器down掉, 能自动剔除. 2 w ...
- [Angular 2] Inject Service
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- [Angular 2] ng-model and ng-for with Select and Option elements
You can use Select and Option elements in combination with ng-for and ng-model to create mini-forms ...
- PHP学习路径
php学习大致可分为三个阶段: 第一阶段:基础知识,页面布局. 学习内容:html.div+css.js. 学习目标:div+css设计. 阶段二:php核心知识和数据库交互. 学习内容:php核心知 ...
- 在Qt中使用sleep
关于sleep函数,我们先来看一下他的作用:sleep函数是使调用sleep函数的线程休眠,线程主动放弃时间片.当经过指定的时间间隔后,再启动线程,继续执行代码.sleep函数并不能起到定时的作用 ...