mybatis的基本使用

mybatis的基本使用

精选文章moguli202025-05-12 14:16:119A+A-

下面会详细说明怎样用 XML 配置文件的方式将 Spring Boot 与 MyBatis 整合,并且给出相应的代码示例。

1. 创建 Spring Boot 项目

可以通过 Spring Initializr(https://start.spring.io/ )创建新的 Spring Boot 项目,在依赖中添加 Spring Web、MyBatis Framework 和 MySQL Driver。

2. 配置数据库连接

在 application.yml 里配置数据库连接信息:



yaml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_database
    username: your_username
    password: your_password
    driver-class-name: com.mysql.cj.jdbc.Driver

3. 创建实体类

创建一个 Java 类来表示数据库中的表,例如 User 类:



java

package com.example.demo.entity;

public class User {
    private int id;
    private String name;
    private int age;

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

4. 创建 Mapper 接口

创建一个 Mapper 接口,不过 SQL 语句会在 XML 文件里定义:



java

package com.example.demo.mapper;

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

import java.util.List;

@Mapper
public interface UserMapper {
    List<User> getAllUsers();
    User getUserById(int id);
}

5. 创建 Mapper XML 文件

在 resources 目录下创建 mapper 文件夹,然后在其中创建 UserMapper.xml 文件:



xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
    <select id="getAllUsers" resultType="com.example.demo.entity.User">
        SELECT * FROM users
    </select>
    <select id="getUserById" parameterType="int" resultType="com.example.demo.entity.User">
        SELECT * FROM users WHERE id = #{id}
    </select>
</mapper>

6. 创建 Service 层

创建一个 Service 类来调用 Mapper 接口的方法:



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 List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }
}

7. 创建 Controller 层

创建一个 Controller 类来处理 HTTP 请求:



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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

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

8. 配置 MyBatis

在 application.yml 中配置 MyBatis 的 mapper 位置:



yaml

mybatis:
  mapper-locations: classpath:mapper/*.xml

总结

  • 添加依赖:在 Spring Boot 项目中添加 MyBatis 和数据库驱动的依赖。
  • 配置数据库:在 application.yml 中配置数据库连接信息。
  • 创建实体类:创建 Java 类来表示数据库中的表。
  • 创建 Mapper 接口:定义方法,但 SQL 语句在 XML 文件中定义。
  • 创建 Mapper XML 文件:编写 SQL 语句并映射到 Mapper 接口的方法。
  • 创建 Service 层:调用 Mapper 接口的方法。
  • 创建 Controller 层:处理 HTTP 请求。
  • 配置 MyBatis:在 application.yml 中配置 Mapper XML 文件的位置。
点击这里复制本文地址 以上内容由莫古技术网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

莫古技术网 © All Rights Reserved.  滇ICP备2024046894号-2