MyBatis Spring-Boot 多模块配置 BindingException

huangapple go评论147阅读模式
英文:

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

我看到很多人都遇到了这个错误,我尝试了我看到的每一个“修复”方法,但是没有任何效果...

architecture

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

architecture

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(&quot;id&quot;) Long id);
}

UserMapper.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;

&lt;mapper namespace=&quot;fr.mydomain.user.mapper.UserMapper&quot;&gt;
	&lt;resultMap type=&quot;fr.mydomain.user.model.User&quot; id=&quot;usermap&quot;&gt;
        &lt;id column=&quot;id&quot; property=&quot;id&quot;/&gt;
    &lt;/resultMap&gt;
    &lt;select id=&quot;findById&quot; resultMap=&quot;usermap&quot;&gt;
        SELECT id FROM user
        WHERE id = #{id};
    &lt;/select&gt;
&lt;/mapper&gt;

SqlMapConfig.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE configuration
PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot;
&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;

&lt;configuration&gt;
   &lt;mappers&gt;
      &lt;mapper resource=&quot;fr/mydomain/user/mapper/UserMapper.xml&quot;/&gt;
   &lt;/mappers&gt;
&lt;/configuration&gt;

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 = &quot;fr.mydomain.user&quot;)
@MapperScan(&quot;fr.mydomain.user&quot;)
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(&quot;fr.mydomain.user&quot;) =&gt; @MapperScan(&quot;fr.mydomain.user.mapper&quot;)

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

huangapple
  • 本文由 发表于 2020年7月24日 17:33:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63070843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定