SpringBoot整合MyBatis-过程及报错解决
引言
最近在学习Spring Boot,在整合MyBatis进行数据访问,报过多次错,踩了很多坑,在此记录一下。
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML或注解来配置和映射原生信息,将接口和 Java 的POJOs(Plain Ordinary Java Object,普通的Java对象)映射成数据库中的记录。
整合过程
- 导入依赖 pom.xml文件如下:
|
- 在 application.properties 中连接数据库
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver - 测试数据源 在tests类进行测试是否连接了数据库,代码及测试结果如下:
package com.kuang;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
class Springboot05MybatisApplicationTests {
DataSource dataSource;
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}## 项目目标 对数据库 mybatis 中 user 表的查询,user 表内容为:
项目构建
项目结构
搭建步骤: 1). 在 application.properties 中整合 MyBatis
2). 创建实体类User#设置包别名(在Mapper映射文件中直接使用实体类名)
mybatis.type-aliases-package==com.kuang.pojo
#告诉系统在哪里找mapper.xml文件(映射文件)
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml3). 创建数据访问接口package com.kuang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
public class User {
private int id;
private String uname;
private String pwd;
}4). 创建 Mapper 映射文件 在 resources 目录下,创建名为 mapper 的包,并在包中创建 SQL 映射文件 Mapper.xml。package com.kuang.mapper;
import com.kuang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/****
* 创建MyBatis的 Mapper 映射接口
*/
//这个注解表示这是一个mybatis的 mapper 类 :Dao(数据访问层)
public interface UserMapper{
List<User> queryUserList();
// User queryUserById(int id);
// int addUser(User user);
// int updateUser(User user);
// int deleteUser(int id);
}5). 创建控制器类
<mapper namespace="com.kuang.mapper.UserMapper">
<select id="queryUserList" resultType="com.kuang.pojo.User">
select * from user
</select>
</mapper>6). 运行 由于在 Mapper 映射接口中有 @Mapper 注解,Application 主类中不用添加 @MapperScan 注解。直接启动,运行效果为:package com.kuang.controller;
import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
public class UserController {
private UserMapper userMapper;
public List<User> queryUserList(){
List<User> userList = userMapper.queryUserList();
for(User user : userList){
System.out.println(user);
}
return userList;
}
}## 报错记录 #### bean无法注入的问题——Error creating bean with name 'userController':
错误原因可能有: 1. 包的位置不对,可以看下上面的项目结构,主类 和 子包都要在com.kuang 包下。 2. 注解使用不对,如 Controller接口没有使用@Controller、@RestController注解等。 3. xml 文件格式,去掉空格和中文注解 #### Invalid bound statement (not found): 这个报错原因一般是xml 映射文件有错误。检查下面几处: 1. xml 文件所在包名 与 接口 所在包名 一致,否则会报500
- xml 文件中 namespace 与 映射接口 的位置一致
- 检察函数名是否一致 UserMapper 映射接口中
Mapper.xml 中
## 总结 虽然是个很简单的整合,但一直报错,反复检查代码的过程中,也对各个注解的功能,项目的架构理解更深,继续积累,继续加油!