注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码。
本章节将介绍怎样使用 IServer 提供的 save 方法保存数据到数据库。接口提供了如下三个 save 方法:
// 插入一条记录(选择字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection<T> entityList); // 插入(批量) boolean saveBatch(Collection<T> entityList, int batchSize);
参数说明:
entity:实体对象
entityList:实体对象集合
batchSize:插入批次数量,即每一个批次最大插入的数量
(1)保存单个实体到数据库,代码如下:
package com.hxstrive.mybatis_plus.simple_service.save; import com.hxstrive.mybatis_plus.model.UserBean; import com.hxstrive.mybatis_plus.service.UserService; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; @RunWith(SpringRunner.class) @SpringBootTest class Save1Test { @Autowired private UserService userService; @Test void contextLoads() { UserBean entity = new UserBean(); entity.setUserId(9999); entity.setName("save-" + System.currentTimeMillis()); entity.setSex("男"); entity.setAge(40); entity.setBorthday(new Date()); boolean flag = userService.save(entity); System.out.println("flag=" + flag); } }
(2)一次保存多个实体到数据库,代码如下:
package com.hxstrive.mybatis_plus.simple_service.save; import com.hxstrive.mybatis_plus.model.UserBean; import com.hxstrive.mybatis_plus.service.UserService; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest class Save2Test { @Autowired private UserService userService; @Test void contextLoads() { List<UserBean> userBeanList = new ArrayList<>(); userBeanList.add(new UserBean(9991, "name-9991", "女", 20)); userBeanList.add(new UserBean(9992, "name-9992", "男", 30)); userBeanList.add(new UserBean(9993, "name-9993", "男", 40)); boolean flag = userService.saveBatch(userBeanList); System.out.println("flag=" + flag); } }
(3)批量保存,将数据分成多个批次,每个批次数量为2。代码如下:
package com.hxstrive.mybatis_plus.simple_service.save; import com.hxstrive.mybatis_plus.model.UserBean; import com.hxstrive.mybatis_plus.service.UserService; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest class Save3Test { @Autowired private UserService userService; @Test void contextLoads() { List<UserBean> userBeanList = new ArrayList<>(); userBeanList.add(new UserBean(9994, "name-9994", "女", 20)); userBeanList.add(new UserBean(9995, "name-9995", "男", 30)); userBeanList.add(new UserBean(9996, "name-9996", "女", 32)); userBeanList.add(new UserBean(9997, "name-9997", "女", 29)); userBeanList.add(new UserBean(9998, "name-9998", "男", 33)); boolean flag = userService.saveBatch(userBeanList, 2); System.out.println("flag=" + flag); } }