springboot
SpringBoot
第一个SpringBoot程序
-
编写controller控制类
package com.zhm.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){ return "hello springboot "+id; } } -
启动主程序
package com.zhm; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootStudyApplication { public static void main(String[] args) { SpringApplication.run(SpringbootStudyApplication.class, args); } }
springboot简介
-
优点:简化spring,springmvc,springservlet配置文件,tomcat服务器配置,插件
-
主要就是靠pom.xml里的parent和dependencies的starter起步依赖,parent继承版本对应的包管理dependencyManagement
dependencyManagement代表版本声明,子项目调用就不需要写版本号但要dependencies引入
starter起步依赖
parent为版本管理,parent主要配置了该springboot版本内各种最佳适配依赖版本号,起作用的还是子项目dependencies引入,两个依赖包含了tomcat服务器等起步依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> -
SpringbootStudyApplication 主启动类,启动后由于starter起步依赖内含tomcat,所以运行tomcat
-
用哪个版本依赖就看需要的依赖版本号对应的boot是哪个版本
排除依赖
- 换服务器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
三种配置文件
-
-
主要用application.yml
-
优先级 properties>yml>yaml
-
yml格式(yaml)
server: port: 81 logging: level: root: info
yaml数据格式
-
记得数据前空格,大小写敏感
-
层级用换行+前面空格表示,前面空格一样代表一个层级,空格少层级高
-
存多个数据用-加空格加数据
hobby: - music - game - run
读取yaml数据
server:
port: 81
logging:
level:
root: info
enterprise:
name: it
age: 100
tel: 15347223316
subject:
- java
- 前端
- 大数据
-
直接@Value注解
-
environment对象自动注入,保存全部数据在内,通过getpropert方法huoq
-
定义一个类,@component注解和@ConfigurationProperties(prefix=“类对应的属性值”),对象.get获取
package com.zhm.domain; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Arrays; @Component @ConfigurationProperties(prefix = "enterprise") public class Enterprise { private String name; private Integer age; private String tel; private String[] 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; } @Override public String toString() { return "Enterprise{" + "name='" + name + '\'' + ", age=" + age + ", tel='" + tel + '\'' + ", subject=" + Arrays.toString(subject) + '}'; } }package com.zhm.controller; import com.zhm.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; import javax.swing.*; import java.util.Arrays; @RestController @RequestMapping("/books") public class BookController { // @GetMapping("/{id}") // public String getById(@PathVariable Integer id){ // return "hello springboot "+ id; // } // 1 @Value("${server.port}") private Integer port; @Value("${enterprise.subject[1]}") private String name; // 2 @Autowired private Environment environment; // 3 @Autowired private Enterprise enterprise; @GetMapping public String getById(){ System.out.println(port); System.out.println(name); System.out.println("------------------------------"); //只能获取单个 // System.out.println(environment.getProperty("enterprise")); System.out.println(environment.getProperty("enterprise.name")); System.out.println(environment.getProperty("enterprise.subject[2]")); System.out.println("------------------------------"); System.out.println(enterprise.getName()); System.out.println(Arrays.toString(enterprise.getSubject())); System.out.println(enterprise); return "hello springboot "; } }
配置多套生产环境
- ---隔开,active代表激活用哪一个profiles
- 这是yaml和yml格式,properties要写多个环境文件,在主application里面用哪一个激活哪一个
spring:
profiles:
active: pro
---
# 测试环境
spring:
profiles: test
server:
port: 81
---
#开发环境
spring:
profiles: dev
server:
port: 82
---
#生产环境
spring:
profiles: pro
server:
port: 83
命令行切换运行环境
-
jar包注意点,先clean再打包,文件编码utf-8
-
执行jar,jar文件目录cmd
java -jar springboot-02-application-0.0.1-SNAPSHOT.jar
-
修改激活环境
java -jar springboot-02-application-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
-
修改端口号,优先级高
java -jar springboot-02-application-0.0.1-SNAPSHOT.jar --server.port=88
-
4种配置文件等级
- 4级就是resources下的文件,3级的resources下config文件夹下的文件,开发用34
- 2级是打包jar包同级文件,1是同级config文件夹下文件,发表改用12
整合Junit
-
SpringBootTest注解,加载springboot启动类,classes写启动类位置
-
写一个service接口和实现类,加上@Service注解,通过启动类加载成功生效
-
package com.zhm; import com.zhm.service.BookService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; // 这里springboot帮你加载了测试类 // @SpringBootTest这个配置相当于加载springboot启动类(配置文件),如果test目录对应,不用写classes=,否则要加classes = Springboot03JunitApplication.class // springboot启动类 Springboot03JunitApplication会扫包同级和下级包,service注解才生效 @SpringBootTest(classes = Springboot03JunitApplication.class) class Springboot03JunitApplicationTests { @Autowired private BookService bookService; @Test void getBookTest() { bookService.getBook(); } }
整合Mybatis
-
初始化项目,勾选mysql驱动和mybatis框架
-
写配置文件,连接数据库信息 application.yml
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/ssmbuild username: root password: 123456 -
写数据库对应实体类和dao接口
dao接口要加@mapper实现原来的MapperScannerConfigurer扫dao包,自动装配
package com.zhm.dao; import com.zhm.domain.Book; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @Mapper public interface BookDao { @Select("select * from book where bookId = #{id}") Book getById(@Param("id") Integer id); } -
测试
package com.zhm; import com.zhm.dao.BookDao; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class Springboot04MybatisApplicationTests { @Autowired private BookDao bookDao; @Test void getById() { System.out.println(bookDao.getById(17)); } }
浙公网安备 33010602011771号