当前位置:   article > 正文

IDEA,Spring Boot整合MyBatis时出现的问题与解决_idea xml cannot find class

idea xml cannot find class

背景

开发工具:IDEA 2019.3
SpringBoot:2.3.2.RELEASE
MyBatis:1.3.2

问题1: Cannot find class: Xxx

具体描述

运行时,提示找不到某个实体类。
详细信息如下:

org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/chen/org/BookMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Book'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Book
  • 1

Mapper中的部分代码如下

    <select id="getAllBooks"  resultType="Book">
		 select  * from book;
	</select>
  • 1
  • 2
  • 3

解决方案

可以尝试在Mapper中,resultType使用全路径的类名
如下所示

    <select id="getAllBooks"  resultType="com.chen.org.Book">
		 select  * from book;
	</select>
  • 1
  • 2
  • 3

问题2: 数据库连接失败

具体描述

数据库名、账户名、密码均正确,但就是提示连接失败。
提示:
The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone
详细信息如下:

2020-06-11 10:00:22.625 ERROR 6544 --- [reate-484184722] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://127.0.0.1:3306/microprinter?characterEncoding=utf-8, errorCode 0, state 01S00

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.20.jar:8.0.20]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1513) ~[druid-1.1.9.jar:1.1.9]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1578) ~[druid-1.1.9.jar:1.1.9]
	at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2466) ~[druid-1.1.9.jar:1.1.9]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

数据库相关配置如下

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/microprinter?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  • 1
  • 2
  • 3
  • 4

解决方案

修改数据库配置中的datasource.url,使用serverTimezone=UTC替代characterEncoding=utf-8

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/microprinter?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  • 1
  • 2
  • 3
  • 4

问题3: BindingException

具体描述

Mapper,Service相关代码都编写正确,但是提示找不到Mapper中的方法
BindingException: Invalid bound statement (not found): com.chen.org.BookMapper.getAllBooks

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.chen.org.BookMapper.getAllBooks
  • 1

解决方案

首先确保Mapper和Service相关代码编写正确,
然后考虑修改pom.xml文件。
在 Maven工程中,运行时会忽略写在包下的XML文件,因此需要在 pom.xml中重新指明资源文件。

<build>
       ......
 <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources> 
</build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

问题4: 找不到mapper

具体描述

Mapper,Service相关代码都编写正确,但是提示找不到Mapper。
在Service层使用@Autowired注解时,
提示Could not autowire. No beans of ‘Xxxmapper’ type found.,如下所示
在这里插入图片描述
项目启动后,报错,信息如下所示:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field reasonMapper in com.cn.service.QuestionReasonService required a bean of type 'com.cn.mapper.QuestionReasonMapper' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:
Consider defining a bean of type 'com.cn.mapper.QuestionReasonMapper' in your configuration.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

解决方案

这可能是因为项目找不到mapper包。
所以,需要在启动类加注解**@MapperScan(“Mapper文件所在的包路径”)**,如下所示:

@SpringBootApplication
@MapperScan("com.cn.mapper")
public class KmApplication {

    public static void main(String[] args) {
        SpringApplication.run(KmApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/618471
推荐阅读
相关标签
  

闽ICP备14008679号