一、Mybatis-plus IService接口使用
1、IService介绍
IService里面已经提供了很多常用方法,比如批量保存、批量更新等。只需要配置好直接调用就可以。
default boolean save(T entity) {
return SqlHelper.retBool(this.getBaseMapper().insert(entity));
}
@Transactional(
rollbackFor = {Exception.class}
)
default boolean saveBatch(Collection<T> entityList) {
return this.saveBatch(entityList, 1000);
}
boolean saveBatch(Collection<T> entityList, int batchSize);
@Transactional(
rollbackFor = {Exception.class}
)
default boolean saveOrUpdateBatch(Collection<T> entityList) {
return this.saveOrUpdateBatch(entityList, 1000);
}
...
2、IService使用配置
- 创建SysUserMapper继承BaseMapper
@Repository("sysUserMapper")
public interface SysUserMapper extends BaseMapper<SysUser> {
}
- 创建SysUserService继承ServiceImpl
@Component
public class SysUserService extends ServiceImpl<SysUserMapper,SysUser> {
}
3、IService调用保存
public void insert() {
List<SysUser> sysUsers = new ArrayList<>();
for (int i=1;i<100;i++) {
SysUser sysUser = new SysUser();
sysUser.setUsername("长伞");
sysUser.setPassword("123456");
sysUsers.add(sysUser);
}
sysUserService.saveBatch(sysUsers);
}
二、Mybatis-plus 插件使用
- 分页插件 PaginationInnerInterceptor
- 乐观锁插件OptimisticLockerInnerInterceptor
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
具体详细代码请参考GitHub:mybatis-plus