Springboot中mybatis的使用

一.创建Springboot项目并加载依赖

1.利用IDEA创建SpringBoot项目,并勾选必须依赖,步骤如下(IDEA版本为2024版)

注意:

1.首先更换镜像源,否则加载配置环境比较慢,网上搜阿里的镜像源就行。

2.JDK的版本不应太高,并且和Java版本适配。

2.依赖添加

(1)SpringBoot版本选择

选择2.x版本就行,没必要选太高。

(2)选择如下几个依赖

spring web依赖。


SpringBoot DevTools依赖。


SQL的JDBC API和Mybatis Framework和Mysql Driver。

点击创建即可

3.Mysql数据源添加

新建一张表,准备些基本数据,例子如下

二.项目结构实例

**1.**把启动项名字改名为application.yml,yml格式的比较直观,并写下如下代码

复制代码
server:
  port: 8081       //端口号配置,如果冲突请自行更改
spring:
  #数据库连接配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver      //驱动
    url: jdbc:mysql://localhost:3306/schema_name          //数据库的基本信息
    username: root
    password: 123456

#mybatis的相关配置
mybatis:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml          //mabits配置文件路径

2.在启动项DemoApplication的同级文件夹或者子文件夹下创建一个Student类,变量应和数据库的一 一对应,并建立get和set方法。按照图的项目结构创建,或者自己创建。

Student类代码

复制代码
package com.example.demo.entity;

public class Student {
    private int id;
    private String name;
    private int age;
    private String hobby;
    private String address;

    public Student(int id, String name, int age, String hobby, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.hobby = hobby;
        this.address = address;
    }

    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;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

StudentController类代码

复制代码
package com.example.demo.controller;

import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@RestController会自动帮一个对象转换成json的格式
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    //@RequestMapping("/getstudent")的意思告诉前端相应的地址访问
    @RequestMapping("/getstudent")
    public List<Student> findAllStudent(){
        return studentService.findAllStudent();
    }

}



//大家可以启动项目后去postman/apifox测试一下接口,看是否正确

StudentMapper接口代码

复制代码
package com.example.demo.mapper;

import com.example.demo.entity.Student;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StudentMapper {
    @Select("SELECT * FROM 表_name")
   public List<Student> findAll();
}

//这里只做简单示范

StudentService类代码

复制代码
package com.example.demo.service;

import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StudentService {


    @Autowired
    private StudentMapper studentMapper;


    public List<Student> findAllStudent() {
        return studentMapper.findAll();         //业务逻辑
    }
}

StudentMapper.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.StudentMapper"> //namespace后填写接口路径
    <select id="findAllStudent" resultType="com.example.demo.entity.Student">
        //id是该查询方法的名字  resultType后填写Student类的路径
        SELECT * FROM 表_name   //sql语句,以查询所有为例
    </select>
</mapper>

然后运行启动项,打开浏览器,输入localhost:8081/getstudent,即可看到数据库存储的信息(JSON格式)。

启动成功后可以去postman/apifox测试一下接口

postman官网:https://www.postman.com/

apifox官网:https://apifox.com/

相关推荐
ttwuai1 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
用户8356290780511 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
用户9931441579841 小时前
微服务框架中获取用户信息
后端
xuanWb1 小时前
手写一个 LLM API 网关:Anthropic 与 OpenAI 协议转换的完整实现
后端
苍何2 小时前
给 Codex 换皮肤这门生意,被我开源了
后端
用户8356290780512 小时前
Python 实现 Excel 命名范围(Named Range)的创建与管理
后端·python
程序员David2 小时前
我让 Claude 从架构文档一路干到代码,踩了三个坑才摸清边界
后端
Zane19942 小时前
并发 vs 并行:别再傻傻分不清了,一文讲透 Java 并发编程的第一课
java·后端
神奇小汤圆2 小时前
线程池拒绝策略CallerRunsPolicy反而卡死了主线程
后端