MyBatis BindingException:参数'tagId'未找到问题排查问询
Hey there, let's figure out why you're hitting this parameter binding error in MyBatis. The error message gives us a key clue: MyBatis is treating your parameters as positional ones ([0, 1, param1, param2]) instead of recognizing the named parameters you defined with @Param. Let's break down the most likely fixes step by step:
1. Double-check your @Param annotation package
This is the most common culprit. MyBatis only recognizes the @Param annotation from its own official package: org.apache.ibatis.annotations.Param. If you accidentally imported a different @Param (like Spring Data's org.springframework.data.repository.query.Param), MyBatis will completely ignore it and fall back to positional parameter handling.
Fix:
Ensure your DAO interface has the correct import statement:
import org.apache.ibatis.annotations.Param;
2. Confirm MyBatis and MyBatis-Spring version compatibility
Older or mismatched versions of MyBatis and MyBatis-Spring can break annotation processing. For example, very early MyBatis versions had limited support for @Param, and a misaligned Spring integration package might fail to pass parameter names correctly to MyBatis.
Fix:
Stick to a compatible version combination. A reliable choice is:
- MyBatis 3.5.x or newer
- MyBatis-Spring 2.0.x or newer
You can verify version compatibility via the official MyBatis documentation (no external links needed—just ensure your versions are aligned).
3. Enable annotation processing in your build tool
If you're using Maven or Gradle, missing annotation processor configuration can prevent MyBatis from generating the proper proxy classes that recognize @Param annotations.
Maven Fix:
Add the MyBatis annotation processor to your maven-compiler-plugin in pom.xml:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <!-- Match your project's Java version --> <target>1.8</target> <annotationProcessorPaths> <path> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> <!-- Match your MyBatis version --> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
Gradle Fix:
Add the annotation processor dependency to your build.gradle:
dependencies { // Other project dependencies... annotationProcessor 'org.mybatis:mybatis:${mybatis.version}' // Match your MyBatis version }
4. Temporary workaround: Use positional parameters
If you need a quick fix while resolving the root issue, you can use the positional parameter names MyBatis is already recognizing:
- Replace
#{tagId}with#{param1}(or#{0}for older MyBatis versions) - Replace
#{articleId}with#{param2}(or#{1})
Your mapper XML would look like this:
<select id="selectByTypeAnd" resultMap="BaseResultMap" > SELECT <include refid="Base_Column_List"/> FROM channel_dynamic where tag_id = #{param1,jdbcType=BIGINT} and article_id = #{param2,jdbcType=VARCHAR} </select>
Note: This is just a band-aid—fixing the @Param annotation issue is the better long-term solution for code readability and maintainability.
内容的提问来源于stack exchange,提问作者huxiaoxiao

