SpringBoot整合Mybatis3

1、导入Mybatis依赖

点击这里,进入,Mybatis-SpringBoot-Starter的官方网址
Mybatis依赖为:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.1</version>
</dependency>

2、学习Mybatis导入的依赖,中的自动配置原理

进入最重要的Mybatis自动配置类,学习其中的源码
在这里插入图片描述

3、学习Mybatis的使用(官网)

点击这里,进入,mybatis官网

如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NoerHlnZ-1676703496826)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218094320525.png)]

根据以上文档的阅读整合Mybatis

接下来讲解如何进行SpringBoot整合Mybatis

4、在配置文件application.yml中,配置Mybatis的Mapper的位置。

  1. 找到application.yml或者application.properties文件

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ebyssNfA-1676703496827)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218143351976.png)]

  2. 配置mybatis的mapper位置:

    首先建立以下的目录结构

    我的目录结构为:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ejWyLslP-1676703496827)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218143631600.png)]

    所以,

    配置的路径为:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B1GLeKq8-1676703496828)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218143451234.png)]
    即,

    mybatis:
      mapper-locations: classpath:mybatis/mapper/*.xml
    

    你可以根据你的配置文件的路径进行修改里面的配置(注意,上面图中*.xml指的是mapper文件夹下的所有的xml文件。)

5、编写Mybatis的接口(interface,注意要加@Mapper注解),以及对应的XML文件

  1. 建立如下的目录结构:mapper文件夹,以及在此文件夹创新UserMapper(接口,即UserMapper.interface)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S4hSE0kL-1676703496828)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218144201874.png)]

其中,有个小鸟,是因为我安装了==MybatisX的插件==

UserMapper的代码为:

package com.zhao.admin.mapper;

import com.zhao.admin.bean.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    User getUserById(Long id);
}

6、编写Service接口调用Mapper接口,Controller调用Service接口

  1. 建立service层调用Mapper接口

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YVbrGAcF-1676703496829)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218144642046.png)]

    UserService.java代码为:

    package com.zhao.admin.service;
    
    import com.zhao.admin.bean.User;
    import com.zhao.admin.mapper.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
    
        @Autowired
        UserMapper userMapper;
    
        public User getUserById(Long id) {
            return userMapper.getUserById(id);
        }
    }
    
    
  2. Controller层调用service层接口

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LWzbY8fu-1676703496829)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218144849671.png)]

    UserController.java的代码为:

    package com.zhao.admin.controller;
    
    import com.zhao.admin.bean.User;
    import com.zhao.admin.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class UserController {
    
        @Autowired
        UserService userService;
    
        @ResponseBody
        @GetMapping("/user")
        public User getUserById(@RequestParam("id") Long id) {
            return userService.getUserById(id);
        }
    }
    

7、测试

运行项目,测试:localhost:8080/user?id=2

Postman中测试的结果如下:(如果没有postman在浏览器中输入以上也是可以的,但是开发中可能用postman的比较多,最好多学习学习工具的使用,若对postman的使用不清晰,可以留言,我可以写一个教程)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cPZPVxMK-1676703496830)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20230218145423962.png)]
最后,如果对你有帮助,点个关注相互学习吧。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

azuredragonz

相互鼓励,相互帮助,共同进步。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值