简化
Spring应用的初始搭建
以及开发过程
简化依赖配置
)<?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.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
内置服务器
,……)starter
减少依赖配置
的目的parent
减少依赖冲突
的目的实际开发
启动方式
package com.springboot_01_quickstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot01QuickstartApplication {
public static void main( String[] args ) {
SpringApplication.run(Springboot01QuickstartApplication.class, args);
}
}
SpringBoot在创建项目时,采用jar的打包方式
SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
创建第一个SpringBoot程序(controller.BookController)
package com.springboot_01_quickstart.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById( @PathVariable Integer id ){
System.out.println("id==》"+id);
return "hello,spring boot!";
}
}
最简SpringBoot程序包含的基础文件
Spring程序与SpringBoot程序对比
类/配置文件 | Spring | SpringBoot |
---|---|---|
pom文件中的坐标 | 手工添加 | 勾选添加 |
web3.0配置类 | 手工制作 | 无 |
Spring/SpringMVC配置类 | 手工制作 | 无 |
控制器 | 手工制作 | 手工制作 |
运行电脑需要有jdk环境
properties
yml
yaml
.yml(主流)
#
表示注释数据前面要加空格与冒号隔开
study:
- java
- 前端
- 运维
使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名……}
package com.springboot_03_read_data.controller;
import com.springboot_03_read_data.domain.Enterprise;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String subject_00;
@GetMapping("/{id}")
public String getById( @PathVariable Integer id ){
System.out.println(lesson);
System.out.println(port);
System.out.println(subject_00);
return "hello,spring boot!";
}
}
封装全部数据到Environment对象(@Autowired自动装配
)
package com.springboot_03_read_data.controller;
import com.springboot_03_read_data.domain.Enterprise;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById( @PathVariable Integer id ){
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("enterprise.age"));
System.out.println(environment.getProperty("enterprise.subject[1]"));
return "hello,spring boot!";
}
}
自定义对象封装指定数据
添加依赖
)<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
package com.springboot_03_read_data.domain;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String[] subject;
@Override
public String toString( ) {
return "Enterprise{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
", subject='" + subject + '\'' +
'}';
}
public String getName( ) {
return name;
}
public void setName( String name ) {
this.name = name;
}
public Integer getAge( ) {
return age;
}
public void setAge( Integer age ) {
this.age = age;
}
public String getTel( ) {
return tel;
}
public void setTel( String tel ) {
this.tel = tel;
}
public String[] getSubject( ) {
return subject;
}
public void setSubject( String[] subject ) {
this.subject = subject;
}
}
package com.springboot_03_read_data.controller;
import com.springboot_03_read_data.domain.Enterprise;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById( @PathVariable Integer id ){
System.out.println(enterprise);
return "hello,spring boot!";
}
}
yaml多环境启动(三个-表示不同模块)
#设置启用的环境
spring:
profiles:
active: test //启动的环境名称
---
#开发
server:
port: 80
spring:
config:
activate:
on-profile: dev //环境名称
---
#生产
server:
port: 81
spring:
config:
activate:
on-profile: pro //环境名称
---
#测试
server:
port: 82
spring:
config:
activate:
on-profile: test //环境名称
properties文件多环境启动
#设置启用饿环境
spring.profiles.active=pro
pro
.propertiesserver.port=8080
dev
.propertiesserver.port=8081
test
.propertiesserver.port=8082
这里是yaml切换方法
)--server.port=88
)最高
)最低
)
如果测试类在SpringBoot启动类的包或子包中,可以省略启动类配置,也就是省略classes的设定
创建BookService
package com.springboot_06_test;
import com.springboot_06_test.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot06TestApplicationTests {
@Autowired
private BookService bookService;
@Test
void contextLoads( ) {
bookService.save();
}
}
创建BookServiceImpl
package com.springboot_06_test.service.impl;
import com.springboot_06_test.service.BookService;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Override
public void save( ) {
System.out.println("book service is running ···");
}
}
SpringBoot整合Junit
package com.springboot_06_test;
import com.springboot_06_test.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot06TestApplicationTests {
@Autowired
private BookService bookService; //把要测试的资源注入进去
@Test
public void testSave( ) {
bookService.save();
}
}
名称:@SpringBootTest
类型:测试类注解
位置:测试类上方
作用:设置Junit加载的SpringBoot启动类
范例:
@SpringBootTest(classes = Springboot06TestApplication.class)
class Springboot06TestApplicationTests {}
相关属性:classes:设置SpringBoot启动类
主要
)搭建数据库
CREATE DATABASE `ssm_db`;
USER `ssm_db`;
CREATE TABLE `tbl_user`(
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`type` VARCHAR(30) DEFAULT NULL,
`description` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `tbl_user` (`id`,`name`,`type`,`description`) VALUES
(1,'Java学不会','1','123456'),
(2,'张三','2','123456'),
(3,'李四','1','123456')
设置数据源参数(application.yml)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
编写实体类
package com.springboot_07_mybatis.domain;
public class User {
private Integer id;
private String name;
private String type;
private String description;
@Override
public String toString( ) {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", type='" + type + '\'' +
", description='" + description + '\'' +
'}';
}
public Integer getId( ) {
return id;
}
public void setId( Integer id ) {
this.id = id;
}
public String getName( ) {
return name;
}
public void setName( String name ) {
this.name = name;
}
public String getType( ) {
return type;
}
public void setType( String type ) {
this.type = type;
}
public String getDescription( ) {
return description;
}
public void setDescription( String description ) {
this.description = description;
}
}
编写dao层
package com.springboot_07_mybatis.dao;
import com.springboot_07_mybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserDao {
@Select("select * from tbl_user where id = #{id}")
public User getById( Integer id);
}
测试类
package com.springboot_07_mybatis;
import com.springboot_07_mybatis.dao.UserDao;
import com.springboot_07_mybatis.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot07MybatisApplicationTests {
@Autowired
private UserDao bookDao;
@Test
void testGetById( ) {
User book = bookDao.getById(1);
System.out.println(book);
}
}