当前位置: 首页 > news >正文

SpringBoot3集成Mybatis

文章目录

      • 基础使用代码
        • 1. 创建Spring Boot 3项目并添加依赖
        • 2. 配置数据库连接
        • 3. 创建实体类
        • 4. 创建Mapper接口
        • 5. 创建Service层
        • 6. 创建Controller层
        • 7. 主应用类
      • 踩坑记录
        • 1. 依赖版本不兼容
        • 2. Mapper接口扫描问题
        • 3. 数据库连接问题
        • 4. Java版本问题
      • 心得体会

基础使用代码

1. 创建Spring Boot 3项目并添加依赖

使用Spring Initializr创建一个Spring Boot 3项目,添加以下依赖到pom.xml文件中:

<dependencies><!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- MyBatis Spring Boot Starter --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.2</version></dependency><!-- MySQL 驱动 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency>
</dependencies>
2. 配置数据库连接

application.properties文件中添加数据库连接信息:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3. 创建实体类

创建一个简单的User实体类:

package com.example.demo.entity;public class User {private Long id;private String name;private Integer age;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}
}
4. 创建Mapper接口

创建UserMapper接口,使用MyBatis注解来定义SQL查询:

package com.example.demo.mapper;import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface UserMapper {@Select("SELECT * FROM user")List<User> getAllUsers();
}
5. 创建Service层

创建UserService类,调用UserMapper的方法:

package com.example.demo.service;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> getAllUsers() {return userMapper.getAllUsers();}
}
6. 创建Controller层

创建UserController类,提供RESTful接口:

package com.example.demo.controller;import com.example.demo.entity.User;
import com.example.demo.service.UserService;
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;@RestController
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/users")public List<User> getAllUsers() {return userService.getAllUsers();}
}
7. 主应用类

确保主应用类添加了必要的注解,并且能扫描到Mapper接口:

package com.example.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

踩坑记录

1. 依赖版本不兼容

Spring Boot 3对依赖版本要求较为严格,如果MyBatis相关依赖版本与Spring Boot 3不兼容,可能会导致启动失败或出现各种异常。要确保使用的mybatis-spring-boot-starter版本与Spring Boot 3兼容。

2. Mapper接口扫描问题

若没有在主应用类上添加@MapperScan注解指定Mapper接口的扫描路径,Spring Boot无法自动将Mapper接口注册为Bean,就会抛出No qualifying bean of type异常。

3. 数据库连接问题

要保证数据库服务正常运行,数据库连接信息(如URL、用户名、密码)准确无误。同时,要注意数据库驱动的版本是否与数据库版本兼容。

4. Java版本问题

Spring Boot 3要求Java 17及以上版本,如果使用的Java版本不符合要求,会导致项目无法正常启动。

心得体会

Spring Boot 3集成MyBatis能让开发者更高效地进行数据库操作开发。Spring Boot 3的自动配置机制和强大的依赖管理,结合MyBatis灵活的SQL映射能力,能快速搭建出功能强大的数据库应用。在集成过程中,要特别注意依赖版本的兼容性和配置的准确性,遇到问题时要仔细查看日志,利用官方文档和社区资源来解决。同时,建议多实践,深入理解Spring Boot和MyBatis的原理和使用方法,这样在开发过程中就能更加得心应手。

相关文章:

  • iPhone 和 Android 在日期格式方面的区别
  • 报表的那些事:四部演进史——架构视角下的技术跃迁与实战思考
  • java中try..catch如何捕捉超时的情况
  • LeetCode:对称二叉树
  • 编程日志4.27
  • RPA与After Effects 2024深度融合:自动化影视特效全链路革命
  • Unity垃圾回收(GC)
  • Spring Boot 中 AOP 的自动装配原理
  • 如何使用极狐GitLab 软件包仓库功能托管 npm?
  • 战术级微波干扰系统:成都鼎轻量化装备如何实现全频段智能压制?
  • http Status 400 - Bbad request 网站网页经常报 HTTP 400 错误,清缓存后就好了的原因
  • Java程序题案例分析
  • Nvidia-smi 运行失败(Failed to initialize NVML: Driver/library version mismatch)
  • 2025FIC初赛(手机)
  • 【实战教程】零基础搭建DeepSeek大模型聊天系统 - Spring Boot+React完整开发指南
  • 阿里云平台与STM32的物联网设计
  • 大模型Prompt工程2.0:多Prompt协同完全指南——从原理到实战,高效解锁AI深层潜力
  • 什么是回调 钩子 Hook机制 钩子函数 异步编程
  • shell脚本实现远程重启多个服务器
  • 代码随想录算法训练营第三十四天
  • 夜读丨喜马拉雅山的背夫
  • 上海市委常委会会议暨市生态文明建设领导小组会议研究基层减负、生态环保等事项
  • 马上评丨维护学术诚信别陷入“唯AI检测”误区
  • 江苏省人社厅党组书记、厅长王斌接受审查调查
  • 人民时评:透过上海车展读懂三组密码
  • 梅花奖在上海|第六代“杨子荣”是怎样炼成的?