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/

相关推荐
东风破_3 小时前
从跨域到 WebSocket:前端跨域方案、SSE 与双向实时通信详解
前端·后端
行百里er4 小时前
Gateway 上别硬塞 MVC:DeferredImportSelector 看菜下锅
spring boot·后端·开源
名字还没想好☜4 小时前
Java 遍历时删元素抛 ConcurrentModificationException:fail-fast 原理与三种正确删法
后端
IT_陈寒5 小时前
React Hooks闭包陷阱差点让我加班到凌晨
前端·人工智能·后端
黑马程序员毕设5 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
码事漫谈6 小时前
本体论,彻底懂
后端
创新技术阁6 小时前
FastapiAdmin 系统日志体系与核心配置参数详解
前端·后端·fastapi
掘金者阿豪6 小时前
SQL Server 迁到金仓,真正麻烦的不是数据,而是这些 T-SQL
后端
xcl09256 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·spring boot·需求分析
Kyrie_kk6 小时前
Java--IO--Path文件访问
java·后端