目录:
SpringBoot入门程序开发
SpringBoot基础配置
1.创建新模块,选择Spring Initializr,并配置模块相关基础信息
2.选择当前模块需要使用的技术集
3.开发控制器类
- //Rest模式
- @RestController
- @RequestMapping("/books")
- public class BookController {
- @GetMapping
- public String getById(){
- System.out.println("springboot is running...");
- return "springboot is running...";
- }
- }
4.运行自动生成的Application类
5.运行结果:

入门案例
最简SpringBoot程序所包含的基础文件
pom.xml文件
Application类
- package com.example._20231018;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class Application {
-
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
-
- }
Spring程序与SpringBoot程序对比

注意:基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
小结:

步骤:


步骤:
手工创建项目(手工导入坐标)

手工创建项目(手工制作引导类)
步骤:

隐藏指定文件/文件夹

Idea中隐藏指定文件或指定类型文件 Setting → File Types → Ignored Files and Folders
SpringBoot简介
总结:
Application.class
- package com.example._20231018;
-
- import com.example._20231018.controller.BookController;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.ConfigurableApplicationContext;
-
- @SpringBootApplication
- public class Application {
-
- public static void main(String[] args) {
- ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
- BookController bean = ctx.getBean(BookController.class);
- System.out.println(bean);
- User user = ctx.getBean(User.class);
- System.out.println(user);
- }
-
- }
运行结果:

启动方式

内置服务器
总结:
REST简介
传统风格资源描述形式
REST风格描述形式
REST风格优点:
按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
根据REST风格对资源进行访问称为RESTful
UserController.class
- package com.example._20231018.controller;
-
- import com.example._20231018.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
-
- @Controller
- public class UserController {
- @RequestMapping(value = "/users", method = RequestMethod.POST)
- @ResponseBody
- public String save(@RequestBody User user) {
- System.out.println("user save..." + user);
- return "{'moudle':'user save'}";
- }
-
- @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
- @ResponseBody
- public String delete(@PathVariable Integer id) {
- System.out.println("user delete..." + id);
- return "{'moudle':'user delete'}";
- }
-
- @RequestMapping(value = "/users", method = RequestMethod.PUT)
- @ResponseBody
- public String update(@RequestBody User user) {
- System.out.println("user update..." + user);
- return "{'moudle':'user update'}";
- }
-
-
- @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
- @ResponseBody
- public String getById(@PathVariable Integer id) {
- System.out.println("user getById..." + id);
- return "{'moudle':'user getById'}";
- }
-
- @RequestMapping(value = "/users", method = RequestMethod.GET)
- @ResponseBody
- public String getAll() {
- System.out.println("user getAll...");
- return "{'moudle':'user getAll'}";
- }
- }
User.class
- package com.example._20231018;
- import org.springframework.stereotype.Component;
-
- @Component
- public class User {
- public String name;
- public Integer age;
-
- @Override
- public String toString() {
- return "User{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
项目结构:

postman验证接口:
新增操作

删除操作:

修改操作:

查询单个操作:

查询全部操作:

运行结果:





@RequestBody @RequestParam @PathVariable
区别
应用
BookController.class
- package com.example._20231018.controller;
-
- import com.example._20231018.Book;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
-
- //@Controller
- //@ResponseBody
- @RestController
- @RequestMapping("/books")
- public class BookController {
- // @RequestMapping(method = RequestMethod.POST)
- // @ResponseBody
- @PostMapping
- public String save(@RequestBody Book book) {
- System.out.println("book save..." + book);
- return "{'moudle':'book save'}";
- }
-
- // @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
- // @ResponseBody
- @DeleteMapping("/{id}")
- public String delete(@PathVariable Integer id) {
- System.out.println("book delete..." + id);
- return "{'moudle':'book delete'}";
- }
-
- // @RequestMapping(method = RequestMethod.PUT)
- // @ResponseBody
- @PutMapping
- public String update(@RequestBody Book book) {
- System.out.println("book update..." + book);
- return "{'moudle':'book update'}";
- }
-
- // @RequestMapping(value = "/{id}", method = RequestMethod.GET)
- // @ResponseBody
- @GetMapping("/{id}")
- public String getById(@PathVariable Integer id) {
- System.out.println("book getById..." + id);
- return "{'moudle':'book getById'}";
- }
-
- // @RequestMapping(method = RequestMethod.GET)
- // @ResponseBody
- @GetMapping
- public String getAll() {
- System.out.println("book getAll...");
- return "{'moudle':'book getAll'}";
- }
- }
Book.class
- package com.example._20231018;
-
- import org.springframework.stereotype.Component;
-
- @Component
- public class Book {
- public String name;
- public int price;
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", price=" + price +
- '}';
- }
- }
postman测试接口:
新增操作:

删除操作:

修改操作:

查询单个操作:

查询全部操作:

运行结果:

总结:

原则
总结:

修改配置



总结:

小结:
1.配置文件间的加载优先级
2. 不同配置文件中相同配置按照加载优先级相互覆盖,不同配置文件中不同配置全部保留


小结:



小结:
1. yaml语法规则
2.注意属性名冒号后面与数据之间有一个空格
3.字面值、对象数据格式、数组数据格式
yaml与yml文件有啥不同?
示例:读取application.yml文件
- country: china
-
- user:
- name_1: itcast
- age: 16
-
- a:
- b:
- c:
- d:
- e: 123
-
- likes:
- - game
- - music
- - sleep
-
- likes2: [ game,music,sleep ]
-
-
- users3:
- - name: zhangsan
- age: 45
-
- - name: lisi
- age: 23
ReadYmlController.class
- package com.example.demo_01.controller;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/read")
- public class ReadYmlController {
- @Value("${country}")
- private String country_variable;
-
- @Value("${user.name_1}")
- private String name_variable;
-
- @Value("${user.age}")
- private Integer age_variable;
-
- @Value("${a.b.c.d.e}")
- private Integer e_variable;
-
- @Value("${likes[1]}")
- private String music_variable;
-
- @Value("${likes2[0]}")
- private String game_variable;
-
- @Value("${users3[0].name}")
- private String name111_variable;
-
- @GetMapping
- public String showVariable() {
- System.out.println(country_variable);
- System.out.println(name_variable);
- System.out.println(age_variable);
- System.out.println(e_variable);
- System.out.println(music_variable);
- System.out.println(game_variable);
- System.out.println(name111_variable);
-
- return country_variable + name_variable + age_variable + e_variable + music_variable + game_variable + name111_variable;
- }
- }
运行结果:


application.yml
- country: china
-
- user:
- name_1: itcast
- age: 16
-
- a:
- b:
- c:
- d:
- e: 123
-
- likes:
- - game
- - music
- - sleep
-
- likes2: [ game,music,sleep ]
-
-
- users3:
- - name: zhangsan
- age: 45
-
- - name: lisi
- age: 23
-
- baseDir: c\windows
-
- #tempDir: c\windows\temp
- tempDir: ${baseDir}\temp
- #使用引号包裹的字符串,其中的转义字符可以生效
- tempDir1: "${baseDir}\temp \t1 \t2 \t3"
ReadYmlController.class
- package com.example.demo_01.controller;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/read")
- public class ReadYmlController {
- @Value("${country}")
- private String country_variable;
-
- @Value("${user.name_1}")
- private String name_variable;
-
- @Value("${user.age}")
- private Integer age_variable;
-
- @Value("${a.b.c.d.e}")
- private Integer e_variable;
-
- @Value("${likes[1]}")
- private String music_variable;
-
- @Value("${likes2[0]}")
- private String game_variable;
-
- @Value("${users3[0].name}")
- private String name111_variable;
-
- @Value("${tempDir}")
- private String temp_dir_variable;
-
- @Value("${tempDir1}")
- private String temp_dir1_variable;
-
- @GetMapping
- public String showVariable() {
- System.out.println(country_variable);
- System.out.println(name_variable);
- System.out.println(age_variable);
- System.out.println(e_variable);
- System.out.println(music_variable);
- System.out.println(game_variable);
- System.out.println(name111_variable);
- System.out.println(temp_dir_variable);
- System.out.println(temp_dir1_variable);
-
- return country_variable +
- name_variable +
- age_variable +
- e_variable +
- music_variable +
- game_variable +
- name111_variable +
- temp_dir_variable +
- temp_dir1_variable
- ;
- }
- }
运行结果:



小结:


示例:
ReadYmlController.class
- package com.example.demo_01.controller;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.env.Environment;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/read")
- public class ReadYmlController {
- @Value("${country}")
- private String country_variable;
-
- @Value("${user.name_1}")
- private String name_variable;
-
- @Value("${user.age}")
- private Integer age_variable;
-
- @Value("${a.b.c.d.e}")
- private Integer e_variable;
-
- @Value("${likes[1]}")
- private String music_variable;
-
- @Value("${likes2[0]}")
- private String game_variable;
-
- @Value("${users3[0].name}")
- private String name111_variable;
-
- @Value("${tempDir}")
- private String temp_dir_variable;
-
- @Value("${tempDir1}")
- private String temp_dir1_variable;
-
- //使用自动装配将所有的数据封装到一个对象Environment中
- @Autowired
- private Environment environment;
-
- @GetMapping
- public String showVariable() {
- System.out.println(country_variable);
- System.out.println(name_variable);
- System.out.println(age_variable);
- System.out.println(e_variable);
- System.out.println(music_variable);
- System.out.println(game_variable);
- System.out.println(name111_variable);
- System.out.println(temp_dir_variable);
- System.out.println(temp_dir1_variable);
- System.out.println("---------------------------------");
- System.out.println(environment.getProperty("country"));
- System.out.println(environment.getProperty("user.age"));
-
- return country_variable +
- name_variable +
- age_variable +
- e_variable +
- music_variable +
- game_variable +
- name111_variable +
- temp_dir_variable +
- temp_dir1_variable
- ;
- }
- }
运行结果:




代码示例:
application.yml
- country: china
-
- user:
- name_1: itcast
- age: 16
-
- a:
- b:
- c:
- d:
- e: 123
-
- likes:
- - game
- - music
- - sleep
-
- likes2: [ game,music,sleep ]
-
-
- users3:
- - name: zhangsan
- age: 45
-
- - name: lisi
- age: 23
-
- baseDir: c\windows
-
- #tempDir: c\windows\temp
- tempDir: ${baseDir}\temp
- #使用引号包裹的字符串,其中的转义字符可以生效
- tempDir1: "${baseDir}\temp \t1 \t2 \t3"
-
-
-
- datasource:
- driver: com.mysql.jdbc.Driver
- url: jdbc:mysql://localhost/springboot_db
- username: root
- password: 666666
MyDataSource.class
- package com.example.demo_01.entity;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
-
- //1.定义数据模型封装yamL文件中对应的数据
- //2.定义为spring管控的bean
- //3.指定加载的数据
- @Component
- @ConfigurationProperties("datasource")
- public class MyDataSource {
- private String driver;
- private String url;
- private String username;
- private String password;
-
- public String getDriver() {
- return driver;
- }
-
- public void setDriver(String driver) {
- this.driver = driver;
- }
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- @Override
- public String toString() {
- return "MyDataSource{" +
- "driver='" + driver + '\'' +
- ", url='" + url + '\'' +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }
ReadYmlController.class
- package com.example.demo_01.controller;
-
- import com.example.demo_01.entity.MyDataSource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.env.Environment;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/read")
- public class ReadYmlController {
- @Value("${country}")
- private String country_variable;
-
- @Value("${user.name_1}")
- private String name_variable;
-
- @Value("${user.age}")
- private Integer age_variable;
-
- @Value("${a.b.c.d.e}")
- private Integer e_variable;
-
- @Value("${likes[1]}")
- private String music_variable;
-
- @Value("${likes2[0]}")
- private String game_variable;
-
- @Value("${users3[0].name}")
- private String name111_variable;
-
- @Value("${tempDir}")
- private String temp_dir_variable;
-
- @Value("${tempDir1}")
- private String temp_dir1_variable;
-
- //使用自动装配将所有的数据封装到一个对象Environment中
- @Autowired
- private Environment environment;
-
- @Autowired
- private MyDataSource myDataSource;
-
- @GetMapping
- public String showVariable() {
- System.out.println(country_variable);
- System.out.println(name_variable);
- System.out.println(age_variable);
- System.out.println(e_variable);
- System.out.println(music_variable);
- System.out.println(game_variable);
- System.out.println(name111_variable);
- System.out.println(temp_dir_variable);
- System.out.println(temp_dir1_variable);
- System.out.println("---------------------------------");
- System.out.println(environment.getProperty("country"));
- System.out.println(environment.getProperty("user.age"));
- System.out.println("---------------------------------");
- System.out.println(myDataSource);
-
- return country_variable +
- name_variable +
- age_variable +
- e_variable +
- music_variable +
- game_variable +
- name111_variable +
- temp_dir_variable +
- temp_dir1_variable
- ;
- }
- }
运行结果:




代码示例:
BookDao.interface
- package com.example.junit.Dao;
-
- public interface BookDao {
- public void save();
- }
BookDaoImpl.class
- package com.example.junit.Dao.impl;
-
- import com.example.junit.Dao.BookDao;
- import org.springframework.stereotype.Repository;
-
- @Repository
- public class BookDaoImpl implements BookDao {
- @Override
- public void save() {
- System.out.println("book dao is running ...");
- }
- }
JunitApplicationTests.class
- package com.example.junit;
-
- import com.example.junit.Dao.BookDao;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class JunitApplicationTests {
- //1.注入你要测试的对象
- @Autowired
- private BookDao bookDao;
-
-
- @Test
- void contextLoads() {
- //2.执行要测试的对象对应的方法
- bookDao.save();
- }
- }
项目结构:

运行结果:

小结:

小结:
整合MyBatis



代码示例:
BookDao.interface
- package com.example._20231023.dao;
-
-
- import com.example._20231023.domain.Book;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- @Mapper
- public interface BookDao {
- @Select("select * from tbl_book where id = #{id}")
- public Book getById(Integer id);
- }
Book.class
- package com.example._20231023.domain;
-
- public class Book {
- private Integer id;
- private String type;
- private String name;
- private String description;
-
- @Override
- public String toString() {
- return "Book{" +
- "id=" + id +
- ", type='" + type + '\'' +
- ", name='" + name + '\'' +
- ", description='" + description + '\'' +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
- }
Application.Tests
- package com.example._20231023;
-
- import com.example._20231023.dao.BookDao;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class ApplicationTests {
-
- @Autowired
- private BookDao bookDao;
-
- @Test
- void contextLoads() {
- System.out.println(bookDao.getById(11));
- }
-
- }
application.yml
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
运行结果:

小结:

BookDao.interface
- package com.example._20231024.dao;
-
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.example._20231024.domain.Book;
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- public interface BookDao extends BaseMapper
{ -
- }
Book.class
- package com.example._20231024.domain;
-
- public class Book {
- private Integer id;
- private String type;
- private String name;
- private String description;
-
- @Override
- public String toString() {
- return "Book{" +
- "id=" + id +
- ", type='" + type + '\'' +
- ", name='" + name + '\'' +
- ", description='" + description + '\'' +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
- }
application.yml
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
-
- mybatis-plus:
- global-config:
- db-config:
- table-prefix: tbl_
ApplicationTests
- package com.example._20231024;
-
- import com.example._20231024.dao.BookDao;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class ApplicationTests {
-
- @Autowired
- private BookDao bookDao;
-
- @Test
- void contextLoads() {
- System.out.println(bookDao.selectById(2));
- }
-
- @Test
- void testGetAll() {
- System.out.println(bookDao.selectList(null));
- }
- }
运行结果:

MyBatis-Plus与MyBatis区别

小结:


整合任意第三方技术
代码示例:
BookDao.interface
- package com.example.dao;
-
- import com.example.domain.Book;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- @Mapper
- public interface BookDao {
- @Select("select * from tbl_book where id = #{id}")
- public Book getById(Integer id);
- }
Book.class
- package com.example.domain;
-
- public class Book {
- private Integer id;
- private String type;
- private String name;
- private String description;
-
- @Override
- public String toString() {
- return "Book{" +
- "id=" + id +
- ", type='" + type + '\'' +
- ", name='" + name + '\'' +
- ", description='" + description + '\'' +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
- }
application.yml
- #spring:
- # datasource:
- # driver-class-name: com.mysql.cj.jdbc.Driver
- # url: jdbc:mysql://localhost:3308/test_db
- # username: root
- # password: 666666
- # type: com.alibaba.druid.pool.DruidDataSource
-
- spring:
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
ApplicationTests
- package com.example;
-
- import com.example.dao.BookDao;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class ApplicationTests {
-
- @Autowired
- private BookDao bookDao;
-
- @Test
- void contextLoads() {
- System.out.println(bookDao.getById(3));
- }
-
- }
运行结果:

小结:
SSMP整合案例
小结: