springboot整合mybatis遇到的那些坑

1.接口类(指*Mapper.java)在spring中注册的问题

当控制台打印如下信息:

A component required a bean named '*Mapper' that could not be found.

意思是spring找不到这个bean,也就是说这个类没有在spring中注册。亲测可用的解决办法有两个:

(1)给接口类加上@Mapper注解

加@Repository或者@Component都不管用,必须得@Mapper注解才行。

// TODO:为啥后两个不行?

(2)在启动类上加@MapperScan注解

需要传入接口类所在包的路径参数,例如@MapperScan("com.example.demo.**.dao")。可传入多个路径,之间以逗号分隔即可。

两种方法比较起来,第一种方法需要在每个类上都添加@Mapper注解,所以第二种比较简洁,只需在启动类上添加一次即可。


2.项目build之后,在target/classes下没有对应的映射文件(指*Mapper.xml)的问题

这种问题在启动时没什么异常,但是当你调用这个mapper的方法时就会抛出:

BindingException: Invalid bound statement (not found)

解决办法是在pom.xml(maven项目)中的build标签里加入:

<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

3.控制台报The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or ...
参见https://blog.csdn.net/tai4lin/article/details/83504479。

4.没有配置Mybatis
参见https://my.oschina.net/yangok/blog/1923209。

猜你喜欢

转载自www.cnblogs.com/magnussen1996/p/10263577.html