赞
踩
开发工具:IDEA 2019.3
SpringBoot:2.3.2.RELEASE
MyBatis:1.3.2
运行时,提示找不到某个实体类。
详细信息如下:
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
Mapper中的部分代码如下
<select id="getAllBooks" resultType="Book">
select * from book;
</select>
可以尝试在Mapper中,resultType使用全路径的类名
如下所示
<select id="getAllBooks" resultType="com.chen.org.Book">
select * from book;
</select>
数据库名、账户名、密码均正确,但就是提示连接失败。
提示:
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]
数据库相关配置如下
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
修改数据库配置中的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
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
首先确保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>
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.
这可能是因为项目找不到mapper包。
所以,需要在启动类加注解**@MapperScan(“Mapper文件所在的包路径”)**,如下所示:
@SpringBootApplication
@MapperScan("com.cn.mapper")
public class KmApplication {
public static void main(String[] args) {
SpringApplication.run(KmApplication.class, args);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。