返回

SpringBoot+MyBatis整合新手攻略:轻松搞定!

后端

SpringBoot与MyBatis整合:全面解析,轻松操作数据库

摘要

随着SpringBoot和MyBatis这两大技术巨头的强强联合,开发者可以在数据库操作上获得前所未有的便利性。本文将深入浅出地探讨SpringBoot与MyBatis的整合过程,从依赖添加、数据源配置到实体类、Mapper接口、Service层和Controller层的编写,全面覆盖,一步步带你征服数据库操作。

步骤1:添加MyBatis依赖

在你的项目pom.xml文件中,添加MyBatis依赖:

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>

步骤2:配置数据源

在application.properties文件中,配置你的数据库信息:

# 数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库URL
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot_mybatis
# 数据库用户名
spring.datasource.username=root
# 数据库密码
spring.datasource.password=123456

步骤3:创建MyBatis配置文件

在resources目录下,创建一个名为mybatis-config.xml的MyBatis配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
</configuration>

步骤4:创建实体类

在src/main/java/com/example/demo/entity目录下,创建一个实体类User.java:

package com.example.demo.entity;

import lombok.Data;

@Data
public class User {

    private Long id;

    private String name;

    private Integer age;
}

步骤5:创建Mapper接口

在src/main/java/com/example/demo/mapper目录下,创建一个Mapper接口UserMapper.java:

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectById(@Param("id") Long id);

    @Insert("INSERT INTO user(name, age) VALUES (#{name}, #{age})")
    int insert(User user);

    @Update("UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}")
    int update(User user);

    @Delete("DELETE FROM user WHERE id = #{id}")
    int delete(@Param("id") Long id);
}

步骤6:编写Service层

在src/main/java/com/example/demo/service目录下,创建一个Service层UserService.java:

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 {

    @Autowired
    private UserMapper userMapper;

    public User selectById(Long id) {
        return userMapper.selectById(id);
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public int insert(User user) {
        return userMapper.insert(user);
    }

    public int update(User user) {
        return userMapper.update(user);
    }

    public int delete(Long id) {
        return userMapper.delete(id);
    }
}

步骤7:编写Controller层

在src/main/java/com/example/demo/controller目录下,创建一个Controller层UserController.java:

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.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User selectById(@PathVariable Long id) {
        return userService.selectById(id);
    }

    @GetMapping("/all")
    public List<User> selectAll() {
        return userService.selectAll();
    }

    @PostMapping
    public int insert(@RequestBody User user) {
        return userService.insert(user);
    }

    @PutMapping
    public int update(@RequestBody User user) {
        return userService.update(user);
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable Long id) {
        return userService.delete(id);
    }
}

总结

通过以上七个步骤,你已经成功地整合了SpringBoot和MyBatis,为数据库操作打开了便捷之门。

常见问题

1. 如何配置SpringBoot的数据源?

在application.properties文件中,设置数据库驱动、URL、用户名和密码等信息。

2. 如何创建实体类?

在项目目录下创建Java类,包含数据库表的字段。

3. 如何创建Mapper接口?

在项目目录下创建接口,标注@Mapper注解,定义数据库操作方法。

4. 如何编写Service层?

在项目目录下创建服务类,包含业务逻辑和调用Mapper接口的方法。

5. 如何编写Controller层?

在项目目录下创建控制器类,包含处理HTTP请求和调用Service层的方法。