在Spring Boot项目中,当需要使用Mybatis时,需要添加相关的依赖:
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>2.2.2version>
- dependency>
当添加以上依赖项后,如果启动项目,会提示以下错误:
- ***************************
- APPLICATION FAILED TO START
- ***************************
-
- Description:
-
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
-
- Reason: Failed to determine a suitable driver class
因为Spring Boot启动时,如果检测到当前已经添加数据库编程的依赖项,会自动读取连接数据库的配置信息,由于目前尚未配置这些信息,所以,启动会报错!
所以,需要在application.properties中添加配置:
- # 连接数据库的参数
- spring.datasource.url=jdbc:mysql://localhost:3306/mall_pms?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
- spring.datasource.username=root
- spring.datasource.password=root
由于Spring Boot在启动项目时只会读取以上配置并应用,并不会实际的连接数据库,所以,即使以上配置值是错误的,启动项目时并不会报告错误!
可以在src/test/java的根包下的测试类中进行测试连接:
- @SpringBootTest
- class CsmallProductApplicationTests {
-
- @Test
- void contextLoads() {
- }
-
- @Autowired
- DataSource dataSource;
-
- @Test
- void testConnection() throws Exception {
- dataSource.getConnection();
- }
-
- }
当配置的URL错误(含主机名错误、端口号错误),或MySQL未启动时,将出现以下错误:
- com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
-
- The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
如果数据库名称错误,或无此数据库,将出现以下错误:
java.sql.SQLSyntaxErrorException: Unknown database 'mall_pmsxzxxxxx'
如果用户或密码错误,将出现以下错误:
java.sql.SQLException: Access denied for user 'rootx'@'localhost' (using password: YES)
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
然后,应该及时测试以上功能是否正确,可以在src/test/java下的根包下创建mapper.AlbumMapperTests测试类,
- package cn.tedu.csmall.product.mapper;
-
- import cn.tedu.csmall.product.pojo.entity.Album;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- public class AlbumMapperTests {
-
- @Autowired
- AlbumMapper mapper;
-
- @Test
- void testInsert() {
- Album album = new Album();
- album.setName("某电视的相册");
- album.setDescription("某电视的相册的描述");
- album.setSort(63);
- int rows = mapper.insert(album);
- System.out.println("rows = " + rows);
- }
-
- }
在执行测试时,如果此前配置的@MapperScan有误,会出现如下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cn.tedu.csmall.product.mapper.AlbumMapperTests': Unsatisfied dependency expressed through field 'mapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.tedu.csmall.product.mapper.AlbumMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
如果在XML文件中,的namespace属性值配置有误,或者节点的id属性值配置有误,或者在application.properties中没有正确的配置mybatis.mapper-locations属性,都将出现以下错误:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.tedu.csmall.product.mapper.AlbumMapper.insert
如果既没有配置resultType又没有配置resultMap,将会出现以下错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'c
当在上使用resultType属性,取值却是的id时,将出现以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [C:\Users\pc\IdeaProjects\jsd2204-csmall-product-teacher\target\classes\mapper\BrandMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [C:\Users\pc\IdeaProjects\jsd2204-csmall-product-teacher\target\classes\mapper\BrandMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'StandardResultMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: StandardResultMap
当在上使用了resultMap,取值错误时(例如取值为类型的全限定名),将出现以下错误:
java.lang.IllegalArgumentException: Result Maps collection does not contain value for cn.tedu.csmall.product.pojo.vo.BrandStandardVO