mybatis中使用InsertProvider注解报错解决全过程

目录
  • 使用InsertProvider注解报错解决
  • mybatis注解开发@InsertProvider
    • Userprovider类
    • mapper的书写

使用InsertProvider注解报错解决

目前项目在使用mybatis,并且是使用注解的方式。

在使用InsertProvider注解的时候报了一下的错误:

org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation.  Cause: org.apache.ibatis.builder.BuilderException: Error creating SqlSource for SqlProvider. Method........

注解是如下这个样子的

@InsertProvider(method = "insertlist",type=SqlProvider.class)
 public int insertInnerTable(List list,String dbTable);

思路是要写一个通用的插入一个集合的方法,但是在执行的时候就报了上面的错误。

在网上查资料未果。

于是只能自己动手,丰衣足食了。

一步步跟断点,跟到mybatis了报错的方法中,发现了如下的代码

try {
      this.sqlSourceParser = new SqlSourceBuilder(config);
      this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);
      providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);
      for (Method m : this.providerType.getMethods()) {
        if (providerMethodName.equals(m.getName())) {
          if (m.getParameterTypes().length < 2
              && m.getReturnType() == String.class) {
            this.providerMethod = m;
            this.providerTakesParameterObject = m.getParameterTypes().length == 1;
          }
        }
      }
    } catch (Exception e) {
      throw new BuilderException("Error creating SqlSource for SqlProvider.  Cause: " + e, e);
    }

注意标黄的位置,终于发现导致错误的罪魁祸首了,原来是这里限制了参数的个数,不能操作两个参数的啊。

于是将方法以及注解改为如下形式

@InsertProvider(method = "insert",type=SqlProvider.class)
 public int insert(SqlContext sqlContext);
在SqlProvider中对应的方法为
public String insert(SqlContext sqlContext){
      ........
}

至此问题解决

mybatis注解开发@InsertProvider

插入一条user的数据,可以直接根据username和password插入

//insert into user(username,password) values(?,?)
@Insert("insert into user(username,password) values(#{username},#{password})")
void save(User user);

但是如果有一个需求,要求传入username和password能正常输入,传入username、password、id也能够正常插入,这个时候就可以使用Provider注解。

这个的用法就是可以创建一个方法类,在类里面做判断,让mapper里面的查询方法根据写的方法来选择需要的sql语句。

Userprovider类

import com.sikiedu.springbootssm.entity.User;
public class Userprovider {    
    public String saveUser(User user)
    {
        if(user.getId()==null)
        {
            return "insert into user(username,password) values(#{username},#{password}) ";
        }
        else
        {
            return "insert into user values(#{id},#{username},#{password})";
        }
    }    
}

在这个类里面我们做判断,选择需要的sql语句。

mapper的书写

@InsertProvider(type = Userprovider.class,method="saveUser")
void save (User user);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 解析Mybatis的insert方法返回数字-2147482646的解决

    前言:前几天在做项目demo的时候,发现有一个很奇怪的现象,就是MyBatis发现更新和插入返回值一直为"-2147482646".无论怎么改,这个值一直不变...是在摸不着头脑,百度和谷歌了一下,有这样的说法原来是由defaultExecutorType设置引起的,如果设置为BATCH,更新返回值就会丢失. If the BATCH executor is in use, the update counts are being lost. 操作:也就是说在spring的配置文件中,只

  • mybatis执行update批量更新时报错的解决方案

    目录 执行update批量更新时报错 在使用Mybatis批量更新时 定义Mapper Dao接口中定义 最后在service中调用 同时执行多条sql的办法 执行update失败问题 说下原因 解决办法 执行update批量更新时报错 在使用Mybatis 批量更新时 想要批量更新时通常在mapper中这么写: 定义Mapper  Dao接口中定义 最后在service中调用 生成的sql直接放到mysql中运行完全没有问题,但是mybatis执行的时候却会报错: <span style=&quo

  • MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法

    最近在项目中有这么一段代码:下载服务器基础业务数据进行本地批量插入操作,因项目中使用mybatis进行持久化操作,故直接考虑使用mybatis的批量插入功能. 1.以下是Mapper接口的部分代码 public interface PrintMapper { @InsertProvider(type = PrintMapperProvider.class,method = "insertAllLotWithVehicleCode4H2") void insertAllLotWithVe

  • 使用mybatis-plus的insert方法遇到的问题及解决方法(添加时id值不存在异常)

    mybatis在持久层框架中还是比较火的,一般项目都是基于ssm.虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活.但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦. 下面给大家介绍使用mybatis-plus的insert方法遇到的问题,具体内容如下所示: 我在添加的时候,无缘无辜的给我报 java.sql.SQLException: Field 'id' doesn't have a default value 如图: 后来了解到 使用 mybati

  • mybatis中使用InsertProvider注解报错解决全过程

    目录 使用InsertProvider注解报错解决 mybatis注解开发@InsertProvider Userprovider类 mapper的书写 使用InsertProvider注解报错解决 目前项目在使用mybatis,并且是使用注解的方式. 在使用InsertProvider注解的时候报了一下的错误: org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation.  

  • 详解vue中引入stylus及报错解决方法

    前提条件是已经有了vue项目,如果没有,请先建立,具体方法看这里https://cn.vuejs.org/v2/guide/installation.html 安装stylus 好了,建立好项目后我们来安装stylus npm install stylus stylus-loader --save-dev 这样就安装上了stylus. 接下来就可以使用了,使用方式分两种.一种是在.vue文件的style块中使用,一种是引用.styl文件的形式 在.vue文件的style块中使用 这个很简单,只要

  • 关于vue中使用three.js报错的解决方法

    目录 前言 1.vue的问题? 2.Proxy的异常情况 3.Three.js的问题 4.defineProperty异常情况 5.解决 总结 前言 最近在学习three.js,同时也学习一下vue3,然后就出现问题了,报错直接用不了,错误信息如下: Uncaught TypeError: 'get' on proxy: property 'modelViewMatrix' is a read-only and non-configurable data property on the prox

  • Mybatis Mapper中多参数方法不使用@param注解报错的解决

    目录 问题描述 寻求解决方案 寻找原因 拓展延伸 在使用低版本的Mybatis的时候,Mapper中的方法如果有多个参数时需要使用@param注解,才能在对应xml的sql语句中使用参数名称获取传入方法的参数值,否则就会报错.本文结合自身在真实开发环境中使用IDEA开发时遇到的问题来共同探讨一下不使用@Param注解报错背后的原因以及解决方案. 问题描述 最近使用IDEA进行开发,项目使用SpringBoot+Mybatis3.4.6,同样的代码检出到本地IDEA后运行,在一个业务查询模块报错,

  • mybatis不加@Parm注解报错的解决方案

    我的idea版本2017.3.4,低版本貌似不会加上这个配置,idea高版本会 补充知识:Mybatis传多个参数的问题 及MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1 问题 对于使用Mybatis ,传多个参数,我们可以使用对象封装外,还可以直接传递参数 对象的封装,例如查询对象条件basequery对象 <select id="getProductByProductQuer

  • SpringDataJpa的@Query注解报错的解决

    目录 SpringDataJpa @Query注解报错 SpringDataJpa @query注解使用原生代码报错 SpringDataJpa @Query注解报错 public interface TimeContentRepository extends JpaRepository<TimeContent,String> { @Query(value = "select id,user_id as userId,create_time as createTime "

  • 解决mybatis批量更新出现SQL报错问题

    一.问题重现 1.配置文件 spring: #DataSource数据源 datasource: url: jdbc:mysql://127.0.0.1:3306/mybatis_test?useSSL=false&amp username: root password: root driver-class-name: com.mysql.jdbc.Driver #MyBatis配置 mybatis: type-aliases-package: com.hl.mybatis.pojo #别名定义

  • SpringMail使用过程中的报错解决办法

    SpringMail使用过程中的报错解决办法 1.Unable to locate provider for protocol: smtp –>缺少依赖造成的 <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> <dependency

  • 解决vue.js在编写过程中出现空格不规范报错的问题

    找到build文件夹下面的webpack.base.conf.js文件. 然后打开该文件,找到图下这段代码,把他注释掉. 注释掉之后,再进行子页面等编写的时候,空格不规范的情况下也不会再报错啦.因为这个报错对于初学者来说实在头大.哈哈O(∩_∩)O哈哈~ 我标注的这些地方,原本是有严格的空格规范要求的,这些报错真是另人烦躁呀o(╥﹏╥)o 反正我把这个问题解决了,特别开心哒哒哒~~~ 以上这篇解决vue.js在编写过程中出现空格不规范报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考

  • .NET中OpenFileDialog使用线程报错的解决方法

    昨天,在做一个NPOI读取的小demo的时候,使用OpenFileDialog打开文件,最开始的写法,直接在按钮点击事件中写,会报错,代码如下: OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Microsoft Office Excel(*.xls;*.xlsx)|*.xls;*.xlsx"; ofd.FilterIndex = 1; ofd.RestoreDirectory = true; if (ofd.ShowD

随机推荐