一.创建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: rootpassword: 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 {@Autowiredprivate 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 {@Autowiredprivate 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 {@Autowiredprivate 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格式)。