如何实现Mybatis批量插入数据并获取返回的主键?

2026-05-28 09:301阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计532个文字,预计阅读时间需要3分钟。

如何实现Mybatis批量插入数据并获取返回的主键?

响应效果(id为主键):{ data: [{ studentName: 张三, classNo: 一班, id: 111 }, { studentName: 李四, classNo: 二班, id: 112 }, { studentName: 王五, classNo: 一班, id: 113 }] }控制层:@PostMapp

响应效果(id为主键):

{ "data": [ {"studentName": "张三","classNo": "一班","id": 111}, {"studentName": "李四","classNo": "二班","id": 112}, {"studentName": "王五","classNo": "一班","id": 113} ] }

控制层:

@PostMapping("/test") @ResponseBody public Map<String, Object> test(@RequestBody String data) { Map<String, Object> resultMap = new HashMap<String, Object>(); //非空校验 if (!checkParams.checkString(data)) { resultMap.put("code", "1"); resultMap.put("msg", "参数为空。"); return resultMap; } //json转List<Map<String, Object>> JSONObject json= new JSONObject(data); String dataString = json.get("data").toString(); com.google.gson.Gson gson = new Gson(); List<Map<String, Object>> list = gson.fromJson(dataString, new com.google.common.reflect.TypeToken<List<Map<String, Object>>>() { }.getType()); //请求接口 resultMap=registerService.test(list); return resultMap; }

接口:

public Map<String, Object> test(List<Map<String,Object>> data);

实现类:

@Override public Map<String, Object> test(List<Map<String,Object>> data) { Map<String, Object> resultMap = new HashMap<String, Object>(); registerMapper.test( data); resultMap.put("data",data); return resultMap; }

持久层:

public void test(List<Map<String,Object>> list);

statement:

<!-- =========================批量插入返回主键示例======================== --> <insert id="test" parameterType="list" useGeneratedKeys="true" keyProperty="id" > INSERT INTO student_info(student_name,class_no)VALUES <foreach collection="list" item="item" separator=","> ( #{item.studentName}, #{item.classNo} ) </foreach> </insert>

请求方式:

localhost/xxx/test

请求参数:

{ "data": [ {"studentName": "张三","classNo": "一班"}, {"studentName": "李四","classNo": "二班"}, {"studentName": "王五","classNo": "一班"} ] }

注意事项:

statement中keyProperty的赋值是可以自定义的,如果将keyProperty的值改为key,即改成如下:

<!-- =========================批量插入返回主键示例======================== --> <insert id="test" parameterType="list" useGeneratedKeys="true" keyProperty="key" > INSERT INTO student_info(student_name,class_no)VALUES <foreach collection="list" item="item" separator=","> ( #{item.studentName}, #{item.classNo} ) </foreach> </insert>

则响应效果(key为主键)如下:

{ "data": [ {"studentName": "张三","classNo": "一班","key": 111}, {"studentName": "李四","classNo": "二班","key": 112}, {"studentName": "王五","classNo": "一班","key": 113} ] }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

如何实现Mybatis批量插入数据并获取返回的主键?

标签:实现

本文共计532个文字,预计阅读时间需要3分钟。

如何实现Mybatis批量插入数据并获取返回的主键?

响应效果(id为主键):{ data: [{ studentName: 张三, classNo: 一班, id: 111 }, { studentName: 李四, classNo: 二班, id: 112 }, { studentName: 王五, classNo: 一班, id: 113 }] }控制层:@PostMapp

响应效果(id为主键):

{ "data": [ {"studentName": "张三","classNo": "一班","id": 111}, {"studentName": "李四","classNo": "二班","id": 112}, {"studentName": "王五","classNo": "一班","id": 113} ] }

控制层:

@PostMapping("/test") @ResponseBody public Map<String, Object> test(@RequestBody String data) { Map<String, Object> resultMap = new HashMap<String, Object>(); //非空校验 if (!checkParams.checkString(data)) { resultMap.put("code", "1"); resultMap.put("msg", "参数为空。"); return resultMap; } //json转List<Map<String, Object>> JSONObject json= new JSONObject(data); String dataString = json.get("data").toString(); com.google.gson.Gson gson = new Gson(); List<Map<String, Object>> list = gson.fromJson(dataString, new com.google.common.reflect.TypeToken<List<Map<String, Object>>>() { }.getType()); //请求接口 resultMap=registerService.test(list); return resultMap; }

接口:

public Map<String, Object> test(List<Map<String,Object>> data);

实现类:

@Override public Map<String, Object> test(List<Map<String,Object>> data) { Map<String, Object> resultMap = new HashMap<String, Object>(); registerMapper.test( data); resultMap.put("data",data); return resultMap; }

持久层:

public void test(List<Map<String,Object>> list);

statement:

<!-- =========================批量插入返回主键示例======================== --> <insert id="test" parameterType="list" useGeneratedKeys="true" keyProperty="id" > INSERT INTO student_info(student_name,class_no)VALUES <foreach collection="list" item="item" separator=","> ( #{item.studentName}, #{item.classNo} ) </foreach> </insert>

请求方式:

localhost/xxx/test

请求参数:

{ "data": [ {"studentName": "张三","classNo": "一班"}, {"studentName": "李四","classNo": "二班"}, {"studentName": "王五","classNo": "一班"} ] }

注意事项:

statement中keyProperty的赋值是可以自定义的,如果将keyProperty的值改为key,即改成如下:

<!-- =========================批量插入返回主键示例======================== --> <insert id="test" parameterType="list" useGeneratedKeys="true" keyProperty="key" > INSERT INTO student_info(student_name,class_no)VALUES <foreach collection="list" item="item" separator=","> ( #{item.studentName}, #{item.classNo} ) </foreach> </insert>

则响应效果(key为主键)如下:

{ "data": [ {"studentName": "张三","classNo": "一班","key": 111}, {"studentName": "李四","classNo": "二班","key": 112}, {"studentName": "王五","classNo": "一班","key": 113} ] }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

如何实现Mybatis批量插入数据并获取返回的主键?

标签:实现