欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

SpringBoot创建项目入门案例

发布时间:2023/12/3 javascript 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SpringBoot创建项目入门案例 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目录结构

一、创建SpringBoot项目

1.创建骨架名称

2.给项目命名

3.配置pom.xml文件


4.MySql的驱动包

5.自动生成的pom.xml文件

<?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.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.william</groupId><artifactId>keepmoving</artifactId><version>0.0.1-SNAPSHOT</version><name>keepmoving</name><description>Demo project for Spring Boot</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>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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

二、写入demo

package com.william.keepmoving.controller;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author :lijunxuan* @date :Created in 2020/5/27 22:42* @description :* @version: 1.0*/ @RestController public class HoldOnLife {@RequestMapping("/hello")public String hello(){return "hello";}}

三、启动项目

发起请求
http://localhost:8080/hello

响应的页面

四、Spring的自动配置

五、yml文件的使用

特殊的单词出现的问题


home 会输出本地的home
country 会输出国家的英文简称

yml的两种注入方式

1.实体类注入

创建实体类

1.加入注解
@Component
@ConfigurationProperties(prefix = “user”)
2.加入以上两个注解时需要在pom.xml文件中加入配置处理器依赖
不加配置处理器依赖会提示

<!--配置处理器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>

3.user要和yml文件中的user相同,user实体类要加入对应的get(),set()方法
4.点击实体类中的图标会跳转到yml文件对应的字段

package com.william.keepmoving.domain;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;import java.util.Arrays;/*** @author :lijunxuan* @date :Created in 2020/5/28 22:26* @description :* @version: 1.0*/ @Component @ConfigurationProperties(prefix = "user") public class User {private String city;private String country;private String[] home;private String time;private String ip;private String password;private String test;@Overridepublic String toString() {return "User{" +"city='" + city + '\'' +", country='" + country + '\'' +", home=" + Arrays.toString(home) +", time='" + time + '\'' +", ip='" + ip + '\'' +", password='" + password + '\'' +", test='" + test + '\'' +'}';}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String[] getHome() {return home;}public void setHome(String[] home) {this.home = home;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getTest() {return test;}public void setTest(String test) {this.test = test;} }

在yml文件中加入特定值

测试类需要注入

2.注解注入

在yml文件中配置

只需要在测试类中加入注解@value配置就可以了

六、数组

yml文件

实体类

package com.william.keepmoving.domain;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;import java.util.Arrays;/*** @author :lijunxuan* @date :Created in 2020/5/28 22:26* @description :* @version: 1.0*/ @Component @ConfigurationProperties(prefix = "user") public class User {private String city;private String country;private String[] home1;private String time;private String ip;private String password;private String test;@Overridepublic String toString() {return "User{" +"city='" + city + '\'' +", country='" + country + '\'' +", home1=" + Arrays.toString(home1) +", time='" + time + '\'' +", ip='" + ip + '\'' +", password='" + password + '\'' +", test='" + test + '\'' +'}';}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String[] getHome1() {return home1;}public void setHome1(String[] home1) {this.home1 = home1;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getTest() {return test;}public void setTest(String test) {this.test = test;} }

执行后的效果

总结

以上是生活随笔为你收集整理的SpringBoot创建项目入门案例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。