SpringBoot3集成Mybatis(开启第一个集成Mybatis的后端接口)

如果你在创建SpringBoot3项目时没有选择Mybatis依赖的话,需要在pom.xml中加入以下的依赖

复制代码
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>3.0.3</version>
</dependency>

配置mybatis实体和xml映射

XML 复制代码
# 配置mybatis实体和xml映射
mybatis:
  ## 映射xml
  mapper-locations: classpath:mapper/*.xml
  configuration:
    # 配置日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

告诉Springboot如何扫描mapper包(路径可以按照下图方式找到,别忘记把中间的/替换成.)

创建Service并且标注为Springboot里面的一个bean

别忘了安装MyBatisX插件

准备工作做好,测试一下
controller层代码(访问的时候,后面加/employee/selectAll)

service层

mapper接口层

写好代码后,进行测试访问 http://localhost:9090/employee/selectAll

相关推荐
日月云棠1 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840821 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide1 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家1 天前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺1 天前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户908324602731 天前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端
桦说编程1 天前
实战分析 ConcurrentHashMap.computeIfAbsent 的锁冲突问题
java·后端·性能优化
大道至简Edward2 天前
Spring Boot 2.7 + JDK 8 升级到 Spring Boot 3.x + JDK 17 完整指南
spring boot·后端
程序员清风2 天前
用了三年AI,我总结出高效使用AI的3个习惯!
java·后端·面试
beata2 天前
Java基础-13: Java反射机制详解:原理、使用与实战示例
java·后端