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/

相关推荐
涡能增压发动积17 小时前
同样的代码循环 10次正常 循环 100次就抛异常?自定义 Comparator 的 bug 让我丢尽颜面
后端
Wenweno0o17 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
swg32132117 小时前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
tyung17 小时前
一个 main.go 搞定协作白板:你画一笔,全世界都看见
后端·go
gelald17 小时前
SpringBoot - 自动配置原理
java·spring boot·后端
@yanyu66617 小时前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
那个失眠的夜18 小时前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis
殷紫川18 小时前
深入拆解 Java 内存模型:从原子性、可见性到有序性,彻底搞懂 happen-before 规则
java·后端
元宝骑士18 小时前
FIND_IN_SET使用指南:场景、优缺点与MySQL优化策略
后端·mysql
用户319523703477118 小时前
记一次 PostgreSQL WAL 日志撑爆磁盘的排查
后端