spring boot项目使用mybatis连接失败问题

我在一个Spring Boot项目中,加入数据库访问功能,使用mybatis。结果项目无法启动。以往没有出现过这种问题。不过,以前的项目,都是将现有项目拷贝过来,修修改改,数据库能访问就行,不会太多关注。

1、欠缺依赖包

首先报的错误是:

nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required。

代码可编译,启动失败。

查来查去,发现少了个依赖:

xml 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>${druid.version}</version>
</dependency>

为什么要有这个东东?搞不懂。不影响编译,没有就运行不了,而且名字也不带mybatis字眼,让人想破脑袋都找不到原因。

查这个包,说明是这样的:Druid Spring Boot Starter 用于帮助你在Spring Boot项目中轻松集成Druid数据库连接池和监控。

2、欠缺Mybatis配置

加上上面的依赖包后,错误变了,变成这样子:
对应 Invalid bound statement (not found)

原因是application.yml中少了关于mybatis的配置:

yml 复制代码
mybatis:
  mapperLocations: classpath:mapper/**/*.xml
  configuration:
    mapUnderscoreToCamelCase: true  # 驼峰转换

在项目时间非常紧张之际,以往正常运行的代码机制忽然报错,而且找不到原因,让人崩溃。

相关推荐
shuoshuohaohao1 天前
MyBatis 小知识点
java·spring·mybatis
CarIise2 天前
MyBatis动态SQL:从条件拼接到底层原理实战
mybatis
CarIise3 天前
Spring Boot图片展示与统一返回格式:从资源映射到泛型封装实战
mybatis
钝挫力PROGRAMER3 天前
开发踩坑记:MyBatis selectKey、空 SQL
数据库·sql·mybatis
自强的小白3 天前
核心功能(Service接口)
java·mybatis
自强的小白3 天前
mybatis-plus 的扩展功能
mybatis·mybatis_plus
自强的小白3 天前
MYbatis-plus(自定义sql)
java·sql·mybatis
\光辉岁月/4 天前
6.mybatisplus学习-BaseMapper、Service、常用注解
java·mybatis·mybatisplus
\光辉岁月/4 天前
1.mybatis学习-基础
学习·mybatis
代码什么用5 天前
在WEB中的应用
mybatis