【SpringBoot系列05】SpringBoot之整合Mybatis

本文介绍了如何在SpringBoot项目中整合Mybatis,包括添加依赖、配置数据源和Mybatis、创建mapper接口和实体类、编写mapper XML文件,以及进行单元测试,确保增删改查功能正常工作。
摘要由CSDN通过智能技术生成

前言

上一篇博客中,我们完成了springboot 使用spring data jpa但是在我们实际工作中,可能大部分的同学还是使用mybatis比较多,所以今天我们在这里实现一下springboot使用mybatis实现对user表的增删改查并且进行单元测试

实现

1、添加mybaits依赖

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
   </dependencies>

并且在yml中配置一下数据源跟mybatis的配置。

server:
  port: 8989
mybatis:
  mapper-locations: classpath:/mybaits/mapper/*.xml
  config-location:  classpath:/mybatis/config/mybatis-config.xml
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
    driver-class-name: com.mysql.jdbc.Driver

在上面的配置中

mybatis:
  mapper-locations: classpath:/mybaits/mapper/*.xml
  config-location:  classpath:/mybatis/config/mybatis-config.xml
  • mapper-locations
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值