• notice
  • Congratulations on the launch of the Sought Tech site

Use the InsertProvider annotation in mybatis to report the error and solve the whole process

The current project is using mybatis and using annotations.

The following error was reported when using the InsertProvider annotation:

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........

The annotation is as follows

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

The idea is to write a general method of inserting a collection, but the above error is reported when it is executed.

Searching for information online was unsuccessful.

So I can only do it myself and have enough food and clothing.

Follow the breakpoint step by step, and follow the method of mybatis to report the error, and found the following code

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);

Pay attention to the position of the yellow mark, and finally found the culprit that caused the error. It turns out that the number of parameters is limited here, and two parameters cannot be manipulated.

So change the method and annotation to the following form

@InsertProvider(method = "insert",type=SqlProvider.class)
 public int insert(SqlContext sqlContext);
The corresponding method in SqlProvider is
public String insert(SqlContext sqlContext){

So far the problem is solved

mybatis annotation development @InsertProvider

Insert a user's data, which can be inserted directly based on username and password

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

However, if there is a requirement that the incoming username and password can be entered normally, and the incoming username, password, and id can also be inserted normally, the Provider annotation can be used at this time.

The usage of this is to create a method class, make judgments in the class, and let the query method in the mapper select the required sql statement according to the written method.

Userprovider class

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

In this class, we make judgments and select the required sql statement.

Writing of mapper

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


Technical otaku

Sought technology together

Related Topic


Leave a Reply