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创建项目入门案例相关推荐
- springboot初始化项目慢,springboot创建项目慢
================================ ©Copyright 蕃薯耀 2020-06-23 https://www.cnblogs.com/fanshuyao/ spring ...
- SpringBoot创建项目内网穿透过程在到jar包本地运行小案例
目录 1.创建SpringBoot项目 2.做这个demo的初衷(有赞获取token) 3.解决内网穿透的问题 4.测试内网穿透是否成功 5.SpringBoot打包Jar运行 6.本地运行jar 1 ...
- spring-boot创建项目出现spring-boot-starter-parent版本报红问题
创建新springboot项目,pom.xml文件就报这个问题:version报红 百度了很多方法,根据其中一个解决办法: 勾选上图所示,仍然报错:没有找到setting.xml文件,我就创建了set ...
- springboot创建项目(通过spring官网)详细
Spring官网下载 官网自动生成springboot项目结构 https://spring.io/projects/spring-boot–进入后翻到最下面有个Spring Initializr点进 ...
- Lucene创建索引入门案例
最近在学习lucene,参考网上的资料写了一个简单搜索demo: 项目jar包: //索引关键类 <pre name="code" class="java" ...
- SpringBoot整合Mybatis-Plus入门案例
1.择Idear中的File->New->project 2.输入项目组.项目名称及版本 3.检查项目名称及存储位置 4.引用依赖 <dependency> <gro ...
- springboot创建项目
2小时学会springboot 和 springboot之web进阶 springboot+jpa mybatis radis websoket 微信相关特性 支付和登录 ...
- SpringBoot创建项目生成的maven-wrapper(mvnw)
1 生成 用https://start.spring.io/生成Spring Boot初始项目后,会生成几个文件: . |-- .mvn | -- wrapper | |-- maven-wrappe ...
- springboot创建项目 编写dao serviece 和controller 持久层用mybatis
11 刷新一下pom 万一没有引入进去 jpa和mybatis选择哪个? 1.看领导要求 2.都会最好 多学点是没错的
最新文章
- 符合自己的德国学校与专业
- python哪一版好用-Python最好用的编辑器是哪款?北京老男孩教育
- [转]OpenStack的网络模式
- python 减少可调用对象的参数个数
- 身份证号码有效性检测算法 ( js版 转 C#版 )
- pip和conda的区别【转载】
- 双重差分模型能做固定效应吗_互助问答第53期:控制时间效应、交互项等问题...
- idea 分享代码位置
- JavaScript 简介 1
- Oracle基础 10 表 table
- 视频教程-汇编语言程序设计VII-其他
- 2016版excel_一招鲜,吃遍天之四:高效办公必备工具——Excel 易用宝
- 利用sublime3 + xmapp使用服务器调试
- 删库是不可能删库的,这辈子是不可能删库的
- 新版白话空间统计(3):空间分布模式
- 最新PHP对接微信支付,发起商家转账API,商家转账到零钱
- 【数据库系统】——经典示例University数据库建表代码和添加数据(大学系统)
- 数据结构循环队列C++实现
- C/C++中生成随机序列——随机函数的选择和自我实现
- php接入钉钉注册回调
热门文章
- cloud foundry_实际的Reactor操作–检索Cloud Foundry应用程序的详细信息
- apache fop_Apache FOP与Eclipse和OSGi的集成
- jooq sql_用jOOQ用Java编写SQL
- 管理多个Java安装
- 使用Spring Security在Spring Boot中进行缓存
- threadlocals_如何使用ThreadLocals射击自己
- 适用于Java开发人员的Elasticsearch:Java的Elasticsearch
- 一个具有Spring Boot,Spring Security和Stormpath的简单Web应用程序-15分钟
- jenkins api_接触Jenkins(Hudson)API,第1部分
- java 映射数组_Java中的数组,列表,集合,映射,元组,记录文字