拦截器
package com.esolution.xreport.config; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusLockConfig { /** * 乐观锁拦截器 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); OptimisticLockerInnerInterceptor optimisticLockInnerInterceptor = new OptimisticLockerInnerInterceptor(); interceptor.addInnerInterceptor(optimisticLockInnerInterceptor); return interceptor; } }
实体类
package com.esolution.xreport.entity; import com.baomidou.mybatisplus.annotation.*; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import org.springblade.core.mp.base.BaseEntity; /** *测试乐观锁 * * @author * @since */ @Data @TableName("biz_test_mybatisplus") @EqualsAndHashCode(callSuper = true) public class TestMybatisplus extends BaseEntity { private static final long serialVersionUID = 1L; @TableId(type=IdType.AUTO) private Long id; @ApiModelProperty(value = "版本号") @Version private Integer version; @ApiModelProperty(value = "用户名") private String userName; @ApiModelProperty(value = "用户号") private String userCode; @ApiModelProperty(value = "地址") private String address; @ApiModelProperty(value = "部门") private Integer createDept; }
业务类
package com.esolution.xreport.service.impl; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.esolution.xreport.entity.TestMybatisplus; import com.esolution.xreport.mapper.*; import com.esolution.xreport.service.ITestMybatisplusService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.mp.base.BaseServiceImpl; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Random; /** *测试 * @author * @date */ @Service @RequiredArgsConstructor @Slf4j public class TestMybatisplusImpl extends BaseServiceImpl<TestMybatisplusMapper, TestMybatisplus> implements ITestMybatisplusService { @Override public void testMyBatisPlusAdd() { List<TestMybatisplus> list = new ArrayList<>(); for(int i=0;i<5;i++){ TestMybatisplus bean = new TestMybatisplus(); bean.setAddress("北京"); bean.setUserName("张三"+i); bean.setUserCode("mm"+i); bean.setVersion(1); list.add(bean); } saveBatch(list); } //批量更新 @Override public void testMyBatisPlusUpdate() { List<TestMybatisplus> list = list(); log.info("list==="+list); list.forEach(e ->{ Random random = new Random(); int randomNumber = random.nextInt(10) + 1; String name= e.getUserName()+randomNumber; e.setUserName(name); }); //this.updateBatchById(list); System.out.println("list.get(0)==="+list.get(1)); this.updateBatchById(list); } //单个更新 @Override public void testMyBatisPlusUpdateOne() { List<TestMybatisplus> list = list(); log.info("list==="+list); list.forEach(e ->{ Random random = new Random(); int randomNumber = random.nextInt(10) + 1; String name= e.getUserName()+randomNumber; e.setUserName(name); }); //this.updateBatchById(list); System.out.println("list.get(0)==="+list.get(1)); this.updateById(list.get(0)); } @Override public void testUpdate() { List<TestMybatisplus> list = list(); TestMybatisplus ben = list.get(0); LambdaUpdateWrapper<TestMybatisplus> wrapper = new LambdaUpdateWrapper<>(); Random random = new Random(); int randomNumber = random.nextInt(10) + 1; String name= ben.getUserName()+randomNumber; wrapper.eq(TestMybatisplus::getUserCode,"mm0"); wrapper.set(TestMybatisplus::getUserName,name); wrapper.eq(TestMybatisplus::getVersion,list.get(0).getVersion()); update(wrapper); } }
标签:批量,get,Random,list,更新,new,mybatis,import,com From: https://www.cnblogs.com/xianz666/p/18164310