magnussen1996
随便写写

导航

 

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

当控制台打印如下信息:

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

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

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

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

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

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

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

1月23日补充:@Mapper和@MapperScan注解都是mybatis的注解,而@Component和@ComponentScan注解都是spring的,在mybatis的应用场景下,用mybatis的注解。


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>
1月24日补充:“<include>**/*.xml</include>”可根据情况修改。

3.控制台报The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or ...
在数据库url后面加上“?serverTimezone=GMT%2B8”,一劳永逸。

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

posted on 2019-01-13 18:44  magnussen1996  阅读(1962)  评论(0编辑  收藏  举报