Spring Boot 基础
Posted 可能自洽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 基础相关的知识,希望对你有一定的参考价值。
Spring Boot 基础
一、快速上手SpringBoot
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
1.使用 idea 创建项目
1.1 创建空项目 springboot-demo
1.2 创建模块
2.新建 BookController
package org.leo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
// Rest 模式
@RestController
@RequestMapping("/books")
public class BookController
@GetMapping
public String getById()
System.out.println("springboot is running...");
return "springboot is running...";
3.直接运行
浏览器访问 http://localhost:8080/books,可以看到已经运行成功,控制台也打印出信息
二、SpringBoot基础配置
1.配置文件分类
1.1 application.properties
服务器端口配置
server.port=80
这样就不用:8080了,浏览器访问 http://localhost/books 就好了
更多的配置见 application-properties
1.2 application.yml
1.3 application.yaml
1.4 上边序号即为优先级排序,一般只保留yml文件
2.读取yaml格式的文件的数据
2.1 正常读取
server:
port: 80
country: china
likes:
- game
- music
- sleep
user:
name: leo
age: 17
package org.leo.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;
// Rest 模式
@RestController
@RequestMapping("/books")
public class BookController
@Value("$country")
private String country;
// 使用自动装配将所有的数据封装到一个对象 Environment 中
@Autowired
private Environment environment;
@GetMapping
public String getById()
System.out.println("springboot is running...");
System.out.println(country);
System.out.println(environment.getProperty("likes[1]"));
System.out.println(environment.getProperty("user.name"));
return "springboot is running...";
运行 Springboot0101Application,可以发现控制台已经输出成功了
2.2 封装数据
创建类,用于封装下面的数据
由 spring 帮我们去加载数据到对象中,告诉 spring 加载这组信息
使用时候从 spring 中直接获取信息使用
三、基于 SpringBoot 的 SSMP 整合案例
1.准备工作
1.1 创建项目
1.2 只需要两个
因为后边要用 MP,所以不用勾选 MyBatis Framework
1.3 pom.xml 里添加几个依赖
mybatis-plus-boot-starter、druid-spring-boot-starter、lombok
<?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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.leo</groupId>
<artifactId>sb-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sb-demo</name>
<description>sb-demo</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.4 修改为 application.yml
1.5 创建表 tb_book
drop table if exists tb_book;
create table tb_book
(
id int(11) NOT NULL AUTO_INCREMENT,
type varchar(20) DEFAULT NULL,
name varchar(50) DEFAULT NULL,
description varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);
insert into tb_book(type, name, description)
values ('计算机知识', 'android权威指南', '王贺 人民邮电出版社'),
('计算机知识', 'linux私房菜', '鸟哥 人民邮电出版社'),
('计算机知识', 'Java编程思想', '布鲁斯 机械工业出版社'),
('计算机知识', 'javascript DOM 编程艺术', '吉米科 人民邮电出版社'),
('计算机知识', '英语常用口语100句', '李希 南方工业出版社'),
('数学知识', '高等数学1', '王志胜 吉林工业出版社'),
('数学知识', '高等数学2', '王志胜 吉林工业出版社'),
('文学', '三国鼎立', '吴风 人民出版社'),
('文学', '且看盛唐', '文旭烟 人民出版社');
2.Book 实体类开发
使用Lombok快速制作实体类(Lombok,一个Java类库,提供了一组注解,简化pojo实体类开发)
可以看到 只需要一个 @Data 就自动进行getter setter操作,左侧 structure 可以看到已经全部做好了
package org.leo.domain;
import lombok.Data;
@Data
public class Book
private Integer id;
private String type;
private String name;
private String description;
3.Dao开发 整合MyBatisPlus,制作数据层测试类
3.1 application.yml 配置
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/my_db?serverTimeZone=UTC
username: root
password: 1234
# 配置一个表名前缀,不然的话就要手动指定表名 mybatis-plus 默认去找类名对应的表,比如 book
mybatis-plus:
global-config:
db-config:
table-prefix: tb_
# 加上 type,解决问题:插入数据时,MP给了 id 一个很大的数
id-type: auto
configuration:
# MP 运行日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.2 BookDao
package org.leo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.leo.domain.Book;
@Mapper
public interface BookDao extends BaseMapper<Book>
3.3 MPConfig
package org.leo.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MPConfig
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor()
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
// 分页功能需要一个拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
3.4 BookDaoTest
package org.leo.dao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.leo.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BookDaoTest
@Autowired
private BookDao bookDao;
@Test
void testSelectById()
System.out.println(bookDao.selectById(268861444));
@Test
void testInsert()
Book book = new Book();
book.setType("test123123123");
book.setName("test123123123");
book.setDescription("test123123123");
bookDao.insert(book);
@Test
void testDeleteById()
bookDao.deleteById(9);
@Test
void testUpdateById()
Book book = new Book();
book.setId(9);
book.setDescription("test");
book.setType("test123123123");
book.setName("test123123123");
bookDao.updateById(book);
@Test
void testSelectPage()
IPage iPage = new Page(3, 5);
bookDao.selectPage(iPage, null);
4.Service开发 基于MyBatisPlus进行增量开发
4.1 BookService
package org.leo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.leo.domain.Book;
public interface BookService extends IService<Book>
boolean modify(Book book);
IPage<Book> getPage(int currentPage, int pageSize);
IPage<Book> getPage(int currentPage, int pageSize, Book book);
4.2 BookServiceImpl
package org.leo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.leo.dao.BookDao;
import org.leo.domain.Book;
import org.leo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements BookService
@Autowired
private BookDao bookDao;
@Override
public boolean modify(Book book)
return bookDao.updateById(book) > 0;
@Override
public IPage<Book> getPage(int currentPage, int pageSize)
IPage<Book> iPage = new Page(currentPage, pageSize);
return bookDao.selectPage(iPage, null);
@Override
public IPage<Book> getPage(int currentPage, int pageSize, Book book) Spring Boot 推荐的基础 POM 文件
Spring Boot系列 Spring Boot介绍和基础POM文件
[Spring Boot] 3. Spring Boot实现自动配置的基础