为了提高数据处理效率,大量数据需要插入数据时可以采用批量数据插入的策略提高数据插入的效率。
如下是实现方法
1、代码结构
2、实体类
package little.tiger.one.application.testblockt.domain; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import java.util.Date; /** * <p> * 。 * </p> * * @author yc * @since 2023-08-01 */ @TableName(value = "test_block_t") public class TestBlockT { private static final long serialVersionUID = 1L; /** * 。 */ @TableId private String blockId; /** * 。 */ private String blockName; public void setBlockId(String blockId) { this.blockId = blockId; } public String getBlockId() { return blockId; } public void setBlockName(String blockName) { this.blockName = blockName; } public String getBlockName() { return blockName; } @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("blockId", getBlockId()) .append("blockName", getBlockName()) .toString(); } }
3、mapper
package little.tiger.one.application.testblockt.mapper; import little.tiger.one.application.testblockt.domain.TestBlockT; import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * <p> * Mapper 接口。 * </p> * * @author yc * @since 2023-08-01 */ public interface TestBlockTMapper extends BaseMapper<TestBlockT> { }
4、接口 尤其注意红色部分
package little.tiger.one.application.testblockt.service; import com.baomidou.mybatisplus.extension.service.IService; import little.tiger.one.application.testblockt.domain.TestBlockT; import java.util.Collection; import java.util.List; import java.util.Map; /** * <p> * 服务类。 * </p> * * @author yc * @since 2023-08-01 */ public interface ITestBlockTService extends IService<TestBlockT> { /** * 单记录新增。 * * @param testBlockT 参数说明 * @return status */ int insert(TestBlockT testBlockT); boolean saveBatch(Collection<TestBlockT> entityList); }
5、接口实现 注意红色部分,如果是单数据源@Transactional 这么写就可以,如果是多数据源一定要指定事务例如:@Transactional(transactionManager="testTransactionManager")
package little.tiger.one.application.testblockt.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import little.tiger.one.application.testblockt.domain.TestBlockT; import little.tiger.one.application.testblockt.mapper.TestBlockTMapper; import little.tiger.one.application.testblockt.service.ITestBlockTService; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.springframework.transaction.annotation.Transactional; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.HashMap; /** * <p> * 服务实现类。 * </p> * * @author yc * @since 2023-08-01 */ @Service public class TestBlockTServiceImpl extends ServiceImpl<TestBlockTMapper, TestBlockT> implements ITestBlockTService { @Autowired TestBlockTMapper testBlockTMapper; /** * 单记录新增。 * * @param testBlockT 参数说明 * @return status */ @Override public int insert(TestBlockT testBlockT) { return testBlockTMapper.insert(testBlockT); } @Override @Transactional public boolean saveBatch(Collection<TestBlockT> entityList) { return super.saveBatch(entityList); } }
6、控制类
package little.tiger.one.application.testblockt.controller; import little.tiger.one.application.testblockt.service.ITestBlockTService; import little.tiger.one.application.testblockt.domain.TestBlockT; import little.tiger.one.basemanager.page.PageUtil; import little.tiger.one.webbase.component.HttpStatusCode; import little.tiger.one.webbase.controller.BaseController; import little.tiger.one.webbase.pojo.AjaxResult; import little.tiger.one.webbase.pojo.PageDomain; import little.tiger.one.webbase.util.ServletUtil; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.validation.Valid; import javax.validation.constraints.NotNull; /** * <p> * 前端控制器。 * </p> * * @author yc * @since 2023-08-01 */ @RestController @RequestMapping("/testblockt") public class TestBlockTController extends BaseController { @Autowired private ITestBlockTService testBlockTService; /** * 单记录新增。 * * @param testBlockT 参数说明 * @return AjaxResult */ @PostMapping(value = "/add", headers = API_VERSION_NAME + "=v1") @ResponseBody public AjaxResult addSave(@Valid TestBlockT testBlockT) { List<TestBlockT> list = new ArrayList(); TestBlockT testBlockT1 = new TestBlockT(); testBlockT1.setBlockId("1"); testBlockT1.setBlockName("oracle"); list.add(testBlockT1); TestBlockT testBlockT2 = new TestBlockT(); testBlockT2.setBlockId("2"); testBlockT2.setBlockName("mysql"); list.add(testBlockT2); Boolean result = testBlockTService.saveBatch(list); System.out.println(result); return AjaxResult.success(HttpStatusCode.POST_SUCCESS_CODE, "成功"); } }
7、效果
调用接口日志里就是批量插入、数据库表里数据也有了
标签:util,little,批量,tiger,testblockt,plus,mybatis,import,public From: https://www.cnblogs.com/yclh/p/17596137.html