目录
三、配置文件两种写法properties和yml,习惯property
一、新建第一个springboot程序
名字什么自己写,Java选择1.8或者Java8
Springboot选择主流的2而不是3,勾一下Spring Web,然后create创建
二、3个接口与3个URL配置方式
简单介绍一下项目结构
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ex</groupId>
<artifactId>spring-boot-first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-first</name>
<description>spring-boot-first</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ParaController类:
package com.ex.springbootfirst.controller;
import com.ex.springbootfirst.pojo.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Aiver
* @Date: 2023/02/02~~22:08
* @Description:演示接口和传参
*/
//表示是一个JSON格式而不是页面格式
@RestController
public class ParaController {
@GetMapping("/firstRequest")
public String firstRequest(){
return "球哥的第一个firstRequest接口";
}
@GetMapping("paraRequest")
public Integer paraRequest(Integer x){
x++;
return x;
// return x++;++x;
}
@PostMapping("/postRequest")
public String postRequest(@RequestBody Student student){
return "postRequest"+student;
}
}
学生类记得加构造器,全参构造方法,否则传不进去参数,像这样
学生类:
package com.ex.springbootfirst.pojo;
/**
* @Author: Aiver
* @Date: 2023/02/02~~22:34
* @Description:数据传输对象
*/
public class Student {
private Integer id;
private String name;
private Integer age;
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
分别测试三个接口:
三、配置文件两种写法properties和yml,习惯property
四、自定义配置
PropertyController类:
package com.ex.springbootfirst.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Aiver
* @Date: 2023/02/02~~23:21
* @Description:和配置相关的controller
*/
@RestController
public class PropertyController {
private Integer grade=3;
private Integer classNum=6;
@GetMapping("/gradeClass")
public String gradeClass(){
return "年级"+grade+"班级"+classNum;
}
}
测试:
自定义在配置文件中写法
PropertyController类改为:
package com.ex.springbootfirst.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Aiver
* @Date: 2023/02/02~~23:21
* @Description:和配置相关的controller
*/
@RestController
public class PropertyController {
@Value("${student.grade}")
private Integer grade;
@Value("${student.classNum}")
private Integer classNum;
@GetMapping("/gradeClass")
public String gradeClass(){
return "年级"+grade+"班级"+classNum;
}
}
property文件:
server.port=8666
spring.application.name=spring-boot-first
#server.servlet.context-path=qiu
server.servlet.context-path=/qiu
student.grade=6
student.classNum=13
测试一下:
静态变量static要有set方法才能注入参数
PropertyController类
package com.ex.springbootfirst.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Aiver
* @Date: 2023/02/02~~23:21
* @Description:和配置相关的controller
*/
@RestController
public class PropertyController {
@Value("${student.grade}")
private Integer grade;
@Value("${student.classNum}")
private Integer classNum;
@Value("${student.age}")
static Integer age;
@GetMapping("/gradeClass")
public String gradeClass(){
return "年级"+grade+"班级"+classNum;
}
@GetMapping("/staticPara")
public String staticPara(){
return "static变量age:"+age;
}
}
配置文件:
测试,发现参数注入不了:
配置文件保持不变,使用set方法对静态变量的参数注入,右键genera生成set方法,然后去掉static。
错误示范没有去掉static,测试一样没有成功传入测试:
package com.ex.springbootfirst.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Aiver
* @Date: 2023/02/02~~23:21
* @Description:和配置相关的controller
*/
@RestController
public class PropertyController {
@Value("${student.grade}")
private Integer grade;
@Value("${student.classNum}")
private Integer classNum;
//@Value("${student.age}")
static Integer age;
@GetMapping("/gradeClass")
public String gradeClass(){
return "年级"+grade+"班级"+classNum;
}
@GetMapping("/staticPara")
public String staticPara(){
return "static变量age:"+age;
}
@Value("${student.age}")
public static void setAge(Integer age) {
PropertyController.age = age;
}
}
正确示范并测试可以传入参数:
package com.ex.springbootfirst.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Aiver
* @Date: 2023/02/02~~23:21
* @Description:和配置相关的controller
*/
@RestController
public class PropertyController {
@Value("${student.grade}")
private Integer grade;
@Value("${student.classNum}")
private Integer classNum;
//@Value("${student.age}")
static Integer age;
@GetMapping("/gradeClass")
public String gradeClass(){
return "年级"+grade+"班级"+classNum;
}
@GetMapping("/staticPara")
public String staticPara(){
return "static变量age:"+age;
}
@Value("${student.age}")
public void setAge(Integer age) {
PropertyController.age = age;
}
}
五、Service和Dao层编写,跑通数据库
1.加入数据库和mybatis依赖,数据库配置
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
server.port=8666
spring.application.name=spring-boot-first
#server.servlet.context-path=qiu
server.servlet.context-path=/qiu
student.grade=6
student.classNum=13
student.age=19
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springbootFirst?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
2.新建Service和dao包的类
dao层接口:
package com.ex.springbootfirst.mapper;
import com.ex.springbootfirst.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
/**
* @Author: Aiver
* @Date: 2023/02/03~~10:07
* @Description:查询学生的dao层
*/
@Mapper
@Repository
public interface StudentMapper {
@Select("SELECT * FROM students WHERE id = #{id}")
Student queryStudentById(Integer id);
// public Student queryStudentById(Integer id);
}
Service层:
package com.ex.springbootfirst.service;
import com.ex.springbootfirst.mapper.StudentMapper;
import com.ex.springbootfirst.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author: Aiver
* @Date: 2023/02/03~~10:04
* @Description:查询学生的Service
*/
@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
public Student queryStudentById(Integer id){
return studentMapper.queryStudentById(id);
}
}
controller层:
错误示范
正确示范
package com.ex.springbootfirst.controller;
import com.ex.springbootfirst.pojo.Student;
import com.ex.springbootfirst.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Aiver
* @Date: 2023/02/03~~10:42
* @Description:学生Controller
*/
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@GetMapping("/queryStudentById")
public String queryStudentById(@RequestParam Integer id){
return studentService.queryStudentById(id).toString();
}
}
测试成功,注意在postman中测试的请求是GET请求而不是post请求: