mybatis-plus批量插入报错Invalid bound statement (not found): insertBatchSomeColumn问题解决

前言

mybatis-plus自带的saveBatch 性能并不能满足我们的需求,故有了insertBatchSomeColumn的扩展。但是扩展后出现了Invalid bound statement (not found): insertBatchSomeColumn的报错,网上却始终搜不到正确的解决办法,故这里记录一下,需要看解决办法的可以直接拉到最后。
注:这里我使用的mybatis-plus是3.1.2版本的,不同版本可能有所差别

引入pom依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.1.2</version>
 </dependency>

编写sql注入器


import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.additional.InsertBatchSomeColumn;

import java.util.List;

public class MySqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
        return methodList;
    }
}

注入bean

@Bean
public MySqlInjector sqlInjector() {
    return new MySqlInjector();
}

使用方式

方式一、直接在Mapper文件中添加该方法

Integer insertBatchSomeColumn(Collection<T> entityList);

例:

public interface planInfoMapper extends BaseMapper<PlanInfo> {
    int insertBatchSomeColumn(List<PlanInfo> entityList);
}

方式二、编写自己的mapper继承BaseMapper,并在其他继承BaseMapper的类上改为继承自己的mapper


public interface MyBaseMapper<T> extends BaseMapper<T> {
    /**
     * 批量插入 仅适用于mysql
     *
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}
public interface ImbPtePlanInfoMapper extends MyBaseMapper<PlanInfo> {

}

大部分博客到这里,基本就完结撒花了,但是实际上,某些情况我们直接调用该方法,是没办法用的。
在这里插入图片描述

报错解决

在实际使用过程中,可能会出现如下报错:

org.apache.ibatis.binding.BindingException:
		 Invalid bound statement (not found): xxx.xxx.xxx.insertBatchSomeColumn

这个时候,就可能是我们使用了自己的SqlSessionFactory
那么就需要手动设置SqlInjector

		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setCallSettersOnNulls(true);
        //禁用mybatis 一级缓存
        configuration.setLocalCacheScope(LocalCacheScope.STATEMENT);
        bean.setConfiguration(configuration);
        bean.setMapperLocations(resolveMapperLocations());
        Interceptor[] plugins = {paginationInterceptor};
        bean.setPlugins(plugins);
        //关键点,请注意这里
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setSqlInjector(new MySqlInjector());
        bean.setGlobalConfig(globalConfig);

至此,问题解决!

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MybatisPlus 是 Mybatis 的增强工具,在 Mybatis 的基础上,简化了开发流程,提高了开发效率。使用 MybatisPlus 可以省去很多重复的 CRUD 操作代码,同时还提供了很多实用的功能,如分页、逻辑删除、乐观锁等。 使用 MybatisPlus 需要进行以下步骤: 1. 引入 MybatisPlus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.x.x</version> </dependency> ``` 2. 配置 MybatisPlus: 在 Spring Boot 中,可以在 application.yml 或 application.properties 文件中添加以下配置: ```yaml mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml # mapper.xml 文件的位置 type-aliases-package: com.example.entity # 实体类所在的包名 ``` 3. 编写实体类: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } ``` 4. 编写 Mapper 接口: ```java public interface UserMapper extends BaseMapper<User> { } ``` 5. 使用 MybatisPlus 进行 CRUD 操作: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getById(Long id) { return userMapper.selectById(id); } @Override public List<User> list() { return userMapper.selectList(null); } @Override public int save(User user) { return userMapper.insert(user); } @Override public int update(User user) { return userMapper.updateById(user); } @Override public int delete(Long id) { return userMapper.deleteById(id); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值