When using mybatis, I encountered nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiati

When using MyBatis yesterday, I encountered an error: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class xxx. Let me introduce to you how to solve this problem.

First, let's look at common situations where this error occurs. When we use the MyBatis framework, we sometimes define a Mapper interface and the corresponding SQL mapping file. In the configuration file, we use @MapperScanannotations to scan the Mapper interface and register it as a Bean. Then, use annotations in our service class @Autowiredto inject the Mapper interface in order to use SQL statements in the code.

But sometimes, when we execute the code, we will encounter exception information similar to the following:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name XXX: Unsatisfied dependency expressed through field XXX; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXXMapper': Failed to instantiate XXXMapper defined in XXX: Error instantiating class XXX mapper. Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class XXX

The reason for this error is usually that MyBatis cannot instantiate the XXXMapper class. Here are several common causes of this problem and their corresponding solutions:

1. XML mapping file path error

Check that the path to your XML mapping file is correct to make sure the file is being loaded correctly. In the configuration file, the attribute <mapper>of the tag resourcespecifies the path of the mapping file, and confirm whether the path is configured correctly.

2. The fully qualified name of the Mapper interface in the XML mapping file is wrong

In the XML mapping file, the attribute <mapper>of the tag namespaceshould specify the fully qualified name of the Mapper interface. Make sure this qualified name matches the actual interface name, including the package name.

3. The method name of the Mapper interface is wrong

In the XML mapping file, idthe attribute of the SQL statement should be consistent with the name of the corresponding method in the Mapper interface. Make sure that the method name matches the method name defined by the interface, including capitalization.

4. The parameter type of the Mapper interface method does not match

In the XML mapping file, parameterTypethe attribute of the SQL statement should be consistent with the parameter type of the corresponding method in the Mapper interface. Make sure that this parameter type is consistent with the parameter type defined by the interface.

5. The XXXMapper interface is not scanned correctly

In the configuration file, @MapperScanwhen the annotation scans the Mapper interface, ensure that the specified package path is correct and includes the package path where the XXXMapper interface is located.

The above are several common nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class XXXcauses of errors and corresponding solutions.

The reference code is as follows:

// XXXMapper.java
import org.apache.ibatis.annotations.Param;

public interface XXXMapper {
    void insertData(@Param("data") String data);
    // ...
}

<!-- XXXMapper.xml -->
<mapper namespace="com.example.mapper.XXXMapper">
    <insert id="insertData">
        INSERT INTO xxx_table (data) VALUES (#{data})
    </insert>
    <!-- ... -->
</mapper>

// XXXService.java
import org.springframework.stereotype.Service;

@Service
public class XXXService {
    @Autowired
    private XXXMapper xxxMapper;
    // ...
}

I hope my sharing can help you solve nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class XXXproblems about MyBatis.

Guess you like

Origin blog.csdn.net/liuqingup/article/details/131593444