<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
上面的是SpringBoot的Pop.xml数据库框架代码
SpringBoot是会自动读取配的,就不需要像之前一样配置Mybatis文件了,但是会出现一些奇怪的问题
例如这些
2023-11-09T20:42:47.570+08:00 WARN 23284 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.example.test]' package. Please check your configuration.
第一种是因为你没有@Mapper这个注释
错误案例如上图,添加@Mapper注释即可
No database selected 第二种
[] threw exception [Request processing failed:
第三种org.springframework.jdbc.UncategorizedSQLException:
### Error querying database. Cause: java.sql.SQLException: No database selected这两种是数据库选择问题
错误案例如上,因为没有添加对应的数据库而是直接调用属性
第三种:Access denied for user '严欣铷'@'localhost' (using password: YES)
第四种2023-11-09T20:50:20.501+08:00 ERROR 14124 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.mybatis.spring.MyBatisSystemException] with root cause
这个的核心是因为你的配置文件是name而不是USername
错误案例如下
改成username就完事了
完整代码及数据库如下
注意了yml文件和那个properties文件除了样式没有区别,用法是一样的
特别注意YML文件 名字 = 这一定要有一个空格 属性
运行结果如下
JSON文件的格式