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

}

 StudentMapper接口代码

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

}

 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.findAllStudent();         //业务逻辑
    }
}

 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格式)。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值