SpringBoot整合mybatis
1.前言
😂不管是整合 Spring 和 Mybatis 还是整合 SpringBoot 和 Mybatis,必然会出错,每次一运行总是能报这样那样的错误,然后每次一错一运行必然爆炸,一行行红色代码搞的人 心 扉 停 止。所以总结了整合的步骤和分享一下自己经常犯的错误。
2.整合步骤
其实最主要的问题还是在于配置文件,所谓:“配置不规范,运行两行泪”,我现在是深有体会。 这边采用的是 SpringToolSuit4 来演示一下SpringBoot整合Mybatis。
百度网盘地址:pan.baidu.com/s/1fsHjUxZV… 提取码:4gs4
下载完成后直接点击该‘.jar’文件会自行解压安装
2.1 创建SpringBoot项目
这边大家注意一下java版本最好选 8 版本
选择Mybatis FrameWork 记得同时也要选择MySQL Driver,当然,要是用其他数据库的话,就相应选择其他数据库的驱动,这边使用MySQL数据库进行演示
然后点击Finish按钮,等待创建,第一次创建需要从网络上下载依赖jar包,可能要等待一会。
2.2 配置application文件(该阶段比较重要,后面很多错误都是配置不当导致的)
找到src/main/resource下的application.properties文件,将其名字改为application.yml
使用yml文件进行演示
配置如下代码
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
username: root
password: 123456
mybatis:
mapper-locations: classpath:com/etc/mapper/xml/*.xml
type-aliases-package: com.etc.entity
以上配置了 服务器的端口 、 数据库连接池 (使用名称为 test 的数据库),数据库账号和密码, 配置了Mybatis mapper文件的位置,和实体类的位置。
2.3 创建相应的包,和相应的类
1.创建controller、service、entity、mapper包,并创建相应的类
1.1 Admins实体类:
package com.etc.entity;
public class Admins {
private Integer adminid;
private String password;
public Integer getAdminid() {
return adminid;
}
public void setAdminid(Integer adminid) {
this.adminid = adminid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Admins(Integer adminid, String password) {
super();
this.adminid = adminid;
this.password = password;
}
}
1.2 AdminMapper接口
package com.etc.mapper;
import java.util.List;
import com.etc.entity.Admins;
public interface AdminMapper {
List<Admins> findAllAdmins();
}
1.3 AdminMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.etc.mapper.AdminMapper" >
<resultMap id="BaseResultMap" type="com.etc.entity.Admins" >
<id column="adminID" property="adminid" jdbcType="INTEGER" />
<result column="Password" property="password" jdbcType="VARCHAR" />
</resultMap>
<select id="findAllAdmins" resultMap="BaseResultMap">
SELECT
*
FROM admins
</select>
</mapper>
1.4 AdminService类(注意:在该类头上一定要有 @Service 注解!)
package com.etc.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.etc.entity.Admins;
import com.etc.mapper.AdminMapper;
@Service
public class AdminService {
@Autowired
AdminMapper adminMapper;
public List<Admins> findAllAdmins(){
return adminMapper.findAllAdmins();
}
}
1.5 AdminController类(注意:在该类头上一定要有 @RestController 注解!)
package com.etc.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.etc.entity.Admins;
import com.etc.service.AdminService;
@RestController
public class AdminController {
@Autowired
AdminService adminService;
@RequestMapping("findAllAdmins")
public List<Admins> findAllAdmins() {
return adminService.findAllAdmins();
}
}
1.6 SpringBoot启动类(注意:在该类头上要加上 @MapperScan 来扫描Mapper文件)
package com.etc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.etc.mapper")
@SpringBootApplication
public class SpringMybatis1Application {
public static void main(String[] args) {
SpringApplication.run(SpringMybatis1Application.class, args);
}
}
2.4 运行结果
访问网址:http://localhost:8081/findAllAdmins
显示结果:
常见报错
1.application文件配置错误
1.1 mapper-locations 路径错误,导致扫不到mapper包下的文件
会出现如下情况: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.etc.mapper.AdminMapper.findAllAdmins
这时候检查一下 mapper-locations的路径是否有写错
1.2 数据库 账号 或者密码错误
会出现如下情况: There was an unexpected error (type=Internal Server Error, status=500). nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.
2.启动类少写 @MapperScan 导致扫不到Mapper的包
3.Controller类没有 加 @Controller 注解
4.Service类没有 加 @Service注解
总结
大家在做整合的时候一定要细心检查 类名、目录名、文件名,不然到时候因为一个字母导致整个程序跑不起来就很难排查了。