英文:
MyBatis Spring-Boot Multi-Module configuration BindingException
问题
我正在尝试访问我的非常简单的端点(http://localhost:8100/user/1),该端点位于我(未来的)使用Spring Boot和MyBatis作为映射器的微服务应用中,但是我遇到了以下错误。
org.apache.ibatis.binding.BindingException: 无效的绑定语句(未找到):fr.mydomain.user.service.UserReadService.findById
我看到很多人都遇到了这个错误,我尝试了我看到的每一个“修复”方法,但是没有任何效果...
UserMapper.java
package fr.mydomain.user.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import fr.mydomain.user.model.User;
@Mapper
public interface UserMapper {
User findById(@Param("id") Long id);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="fr.mydomain.user.mapper.UserMapper">
<resultMap type="fr.mydomain.user.model.User" id="usermap">
<id column="id" property="id"/>
</resultMap>
<select id="findById" resultMap="usermap">
SELECT id FROM user
WHERE id = #{id};
</select>
</mapper>
SqlMapConfig.xml
<?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>
<mappers>
<mapper resource="fr/mydomain/user/mapper/UserMapper.xml"/>
</mappers>
</configuration>
有人能看出我漏掉了什么吗?
编辑:您可以在我的GitHub上找到该项目这里,只需配置您的数据库
英文:
I'm trying to reach my very simple endpoint (http://localhost:8100/user/1) on my (future) micro-services app with spring-boot and mybatis as mapper but I'm getting this error.
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): fr.mydomain.user.service.UserReadService.findById
Saw a lot of people getting this error, I tried every "fix" I saw but nothing is working...
UserMapper.java
package fr.mydomain.user.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import fr.mydomain.user.model.User;
@Mapper
public interface UserMapper {
User findById(@Param("id") Long id);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="fr.mydomain.user.mapper.UserMapper">
<resultMap type="fr.mydomain.user.model.User" id="usermap">
<id column="id" property="id"/>
</resultMap>
<select id="findById" resultMap="usermap">
SELECT id FROM user
WHERE id = #{id};
</select>
</mapper>
SqlMapConfig.xml
<?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>
<mappers>
<mapper resource="fr/mydomain/user/mapper/UserMapper.xml"/>
</mappers>
</configuration>
Is someone can see what am I missing ?
Edit : you can find the project on my github here just need to configure your db
答案1
得分: 0
发现了我的问题,它出现在我的 CoreApplication.java 文件中:
```java
@SpringBootApplication(scanBasePackages = "fr.mydomain.user")
@MapperScan("fr.mydomain.user")
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
我原以为 @MapperScan
注解是在寻找 @Mapper
注解,但实际上它将每个类都视为 Mapper,所以它将我的服务也视为了 Mapper...
将:
@MapperScan("fr.mydomain.user") => @MapperScan("fr.mydomain.user.mapper")
英文:
Found my problem, it was in my CoreApplication.java
@SpringBootApplication(scanBasePackages = "fr.mydomain.user")
@MapperScan("fr.mydomain.user")
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
I thought the MapperScan annotation was looking for Mapper annotation but it's actually considering every class as mapper, so it was considering my service as a mapper ...
@MapperScan("fr.mydomain.user") => @MapperScan("fr.mydomain.user.mapper")
答案2
得分: 0
Sure, here's the translation of the text:
你能把你的项目发布到GitHub并分享项目吗?我也构建了一个使用Maven多模块的Spring Boot+MyBatis项目,但无法运行,谢谢。
英文:
can you post you project to github and share project? I alse build maven multi-module sringboot+mybatis and can not running thanks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论