[Mybatis] An error occurred when annotating sql

1. Error:

When doing the exercise of Mybatis injecting sql with annotations, I reported such an error.

It's normal to encounter mistakes, and then I learned the content I just learned today, reviewing the old and learning new things.

The error question is as follows:

2. File structure:

BookMapper.java

public interface BookMapper {
    @SelectProvider(type = BookMapperSQL.class,method = "bookSelectById")
    Book bookSelectById(int id);

}

BookMapperSQL .java

public class BookMapperSQL {
    public String bookSelectById(final int id){
        return new SQL().
                SELECT("*")
                .FROM("t_user")
                .WHERE("id=#{id}").toString();

    }
}

Then I deleted all the contents of BookMapper.xml without deleting the file. The directory is as follows:

 

Mybatis configuration file

This mapping with package and class has no effect, because we are doing annotation configuration

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="Dd.properties"/>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <package name="org.example.po"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
<!--        <mapper class="org.example.mapper.BookMapper"/>-->
        <package name="org.example.mapper"/>
    </mappers>
</configuration>

3. Analysis:

      From the above file, most of my problems should be in the mapper package. Then it is to see if the number of parameters is aligned, but it will not cause such an error. It is said that the SQL Mapper configuration is parsed incorrectly , so I was entangled in this question at first whether the content of the BookMapperSQL.java was written incorrectly, and I studied more than one. Hours, I re-learned it directly and cried.

      Later, I wondered if the problem occurred because I took the homework I did before and modified it. Then I looked at my previous file directory

       I suddenly remembered that when I implemented this dynamic proxy method before, the pom.xml file was configured and specified in the mapper package.

 Is it because of the specified location and the file loading that there is a problem? So I just commented out this code

, then it runs successfully. 

When it's hot, when we use annotations to inject sql, there is no need to add this file to the mapper package. It can only be said that I am stupid, and no one is as oozy as me when searching online.

Guess you like

Origin blog.csdn.net/m0_56233309/article/details/123561569