赞
踩
在Java开发中,Spring Boot和MyBatis是两个常用的框架,结合使用可以方便地连接和操作MySQL数据库。本文将详细介绍如何在Spring Boot项目中使用MyBatis框架连接MySQL数据库,并通过一个抖音视频数据存取操作的示例展示完整的增删改功能代码。
在pom.xml
文件中添加Spring Boot和MyBatis相关依赖:
- <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>2.2.0</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.28</version>
- </dependency>
在application.properties
文件中配置MySQL数据库连接信息:
- spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
- spring.datasource.username=root
- spring.datasource.password=your_password
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
- package com.example.demo.model;
-
- public class Video {
- private Long id;
- private String title;
- private String url;
-
- // 省略getter和setter方法
- }
- package com.example.demo.mapper;
-
- import com.example.demo.model.Video;
- import org.apache.ibatis.annotations.*;
-
- import java.util.List;
-
- @Mapper
- public interface VideoMapper {
- @Select("SELECT * FROM videos")
- List<Video> findAll();
-
- @Select("SELECT * FROM videos WHERE id = #{id}")
- Video findById(Long id);
-
- @Insert("INSERT INTO videos(title, url) VALUES(#{title}, #{url})")
- void insert(Video video);
-
- @Update("UPDATE videos SET title = #{title}, url = #{url} WHERE id = #{id}")
- void update(Video video);
-
- @Delete("DELETE FROM videos WHERE id = #{id}")
- void delete(Long id);
- }
- package com.example.demo.service;
-
- import com.example.demo.mapper.VideoMapper;
- import com.example.demo.model.Video;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service
- public class VideoService {
- @Autowired
- private VideoMapper videoMapper;
-
- public List<Video> getAllVideos() {
- return videoMapper.findAll();
- }
-
- public Video getVideoById(Long id) {
- return videoMapper.findById(id);
- }
-
- public void addVideo(Video video) {
- videoMapper.insert(video);
- }
-
- public void updateVideo(Video video) {
- videoMapper.update(video);
- }
-
- public void deleteVideo(Long id) {
- videoMapper.delete(id);
- }
- }
- package com.example.demo.controller;
-
- import com.example.demo.model.Video;
- import com.example.demo.service.VideoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
-
- @RestController
- @RequestMapping("/videos")
- public class VideoController {
- @Autowired
- private VideoService videoService;
-
- @GetMapping("/")
- public List<Video> getAllVideos() {
- return videoService.getAllVideos();
- }
-
- @GetMapping("/{id}")
- public Video getVideoById(@PathVariable Long id) {
- return videoService.getVideoById(id);
- }
-
- @PostMapping("/")
- public void addVideo(@RequestBody Video video) {
- videoService.addVideo(video);
- }
-
- @PutMapping("/")
- public void updateVideo(@RequestBody Video video) {
- videoService.updateVideo(video);
- }
-
- @DeleteMapping("/{id}")
- public void deleteVideo(@PathVariable Long id) {
- videoService.deleteVideo(id);
- }
- }
启动Spring Boot应用,可以使用Postman或浏览器访问API接口进行增删改查操作,例如:
通过本文的介绍了如何在Spring Boot项目中集成MyBatis框架,并实现了对MySQL数据库的增删改查操作。这种组合可以使开发更加高效和便捷,适用于各种类型的Java应用开发。希望本文对大家有所帮助,祝愿大家在Java开发中取得更多的成就!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。