当前位置: 首页 > news >正文

平凉崆峒建设局网站艾瑞指数

平凉崆峒建设局网站,艾瑞指数,环球资源平台的优势,网站建设与管理就业SpringBoot基础入门SpringBoot项目创建方式Idea创建SpringBoot官网创建基于阿里云创建项目手工搭建SpringBoot启动parentstarter引导类内嵌tomcat基础配置属性配置配置文件分类yaml文件yaml数据读取整合第三方技术整合JUnit整合MyBatis整合Mybatis-Plus整合DruidSpringBoot是由…

SpringBoot基础入门

  • SpringBoot项目创建方式
    • Idea创建
    • SpringBoot官网创建
    • 基于阿里云创建项目
    • 手工搭建
  • SpringBoot启动
    • parent
    • starter
    • 引导类
    • 内嵌tomcat
  • 基础配置
    • 属性配置
    • 配置文件分类
    • yaml文件
    • yaml数据读取
  • 整合第三方技术
    • 整合JUnit
    • 整合MyBatis
    • 整合Mybatis-Plus
    • 整合Druid

SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化 Spring应用的初始搭建以及开发过程

  • Spring程序缺点
    • 依赖设置繁琐
    • 配置繁琐
  • SpringBoot程序优点
    • 起步依赖(简化依赖配置)
    • 自动配置(简化常用工程相关配置)
    • 辅助功能(内置服务器,……)

SpringBoot项目创建方式

Idea创建

1.创建新模块,选择Spring Initializr,并配置模块相关基础信息
在这里插入图片描述
2.选择当前模块需要使用的技术集
在这里插入图片描述
3.开发控制器

@RestController
@RequestMapping("/books")
public class BookController {@GetMappingpublic String getById(){System.out.println("Springboot is running....");return "Springboot is running....";}
}

4.运行自动生成的Application类
在这里插入图片描述

最简SpringBoot程序所包含的基础文件:

  • 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.7.10</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.itheima</groupId><artifactId>springboot_01-01_quickstart</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot_01-01_quickstart</name><description>springboot_01-01_quickstart</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>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>
</project>
  • Application类
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

Spring程序与SpringBoot程序对比:
在这里插入图片描述
总结:

  1. 开发SpringBoot程序可以根据向导进行联网快速制作
  2. SpringBoot程序需要基于JDK8进行制作
  3. SpringBoot程序中需要使用何种功能通过勾选选择技术
  4. 运行SpringBoot程序通过运行Application程序入口进行

注意: 基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构

SpringBoot官网创建

1.进入SpringBoot官网找到Spring Initializr
在这里插入图片描述
2.进入创建页面,创建工程,并保存项目,解压项目,通过IDE导入项目。
在这里插入图片描述

基于阿里云创建项目

之前https://start.spring.io/很容易网速慢,通过阿里云来提升速度
在这里插入图片描述

手工搭建

1.手动导入坐标

<?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 http://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.5.4</version></parent><groupId>com.itheima</groupId><artifactId>springboot_01_03_quickstart</artifactId><version>1.0-SNAPSHOT</version>
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
</project>

2.手工创建引导类

package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);
}
}

tips:隐藏文件,避免页面杂乱
在这里插入图片描述
Setting → File Types → Ignored Files and Folders
输入要隐藏的文件名,支持*号通配符
回车确认添加
在这里插入图片描述

SpringBoot启动

parent

parent元素是一种Maven的继承机制,用于指定项目的父级依赖。它提供了一个基础配置,包括默认的插件、依赖和配置信息。子项目可以通过继承父项目的pom.xml文件来使用相同的配置和依赖项,从而减少了代码冗余和重复配置的工作量(仅定义未使用)。
使用Spring Boot的parent元素可以让我们更方便地使用Spring Boot提供的各种功能和依赖库,同时避免多个依赖使用相同技术时出现依赖版本冲突。
继承parent的形式也可以采用引入依赖(spring-boot-dependencies)的形式实现效果
在这里插入图片描述

starter

starter是一种依赖库,它提供了一组预配置的依赖项和默认设置,以满足特定的需求。这些starters通常包含了常见的依赖项(例如Spring框架核心、数据库驱动程序、Web容器等),并为它们提供了默认的配置信息,以帮助我们快速地开始项目开发。
使用Spring Boot的starter可以简化项目配置和管理,避免许多麻烦和复杂性,并提高开发效率和生产力。例如,如果我们要创建一个基于Web的Spring Boot应用程序,可以使用Spring Boot Web Starter来获取所需的所有依赖项和默认配置,从而减少了许多手动配置的工作量。
在这里插入图片描述

  • starter
    • SpringBoot中常见项目名称,定义了当前项目使用的所有依赖坐标,以达到减少依赖配置的目的
  • parent
    • 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
    • spring-boot-starter-parent各版本间存在着诸多坐标版本不同

实际开发

  • 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供,除非SpringBoot未提供对应版本V
  • 如发生坐标错误,再指定Version(要小心版本冲突)

引导类

SpringBoot的引导类是Boot工程的执行入口,运行main方法就可以启动项目,但是未启动web服务器
SpringBoot工程运行后初始化Spring容器,扫描引导类所在包加载bean。

@SpringBootApplication
public class Springboot01QuickstartApplication {public static void main(String[] args) {SpringApplication.run(Springboot01QuickstartApplication.class, args);}
}

内嵌tomcat

内嵌Tomcat工作原理是将Tomcat服务器作为对象运行,并将该对象交给Spring容器管理。
在这里插入图片描述
变更内嵌服务器思想是去除现有服务器,添加全新的服务器

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><!--排除tomcat依赖--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency>
<!--引入jetty,版本由start控制-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

在这里插入图片描述
内置服务器

  • tomcat(默认):apache出品,粉丝多,应用面广,负载了若干较重的组件
  • jetty:更轻量级,负载性能远不及tomcat
  • undertow:负载性能勉强跑赢tomcat

基础配置

属性配置

SpringBoot(官方文档)默认配置文件application.properties,通过键值对配置对应属性。

  • SpringBoot中导入对应starter后,提供对应配置属性
  • 书写SpringBoot配置采用关键字+提示形式书写

修改服务器端口:server.port=80
关闭运行日志图标:spring.main.banner-mode=off

配置文件分类

SpringBoot提供了多种属性配置方式:
在这里插入图片描述

主流格式为yml,层级编写,避免重复写。
在这里插入图片描述

SpringBoot配置文件加载顺序:

  • application.properties > application.yml > application.yaml
  • 不同配置文件中相同配置按照加载优先级相互覆盖,不同配置文件中不同配置全部保留

解决配置文件自动提示消失
1.Setting → Project Structure → Facets,选中对应项目/工程
在这里插入图片描述
2.Customize Spring Boot
在这里插入图片描述
3.选择配置文件
在这里插入图片描述

yaml文件

YAML(YAML Ain’t Markup Language),一种数据序列化格式

  • 优点:
    • 容易阅读
    • 容易与脚本语言交互
    • 以数据为核心,重数据轻格式
  • YAML文件扩展名
    • .yml(主流)
    • .yaml

在这里插入图片描述
yaml语法规则:

  • 大小写敏感
  • 属性层级关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • #表示注释
  • 核心规则:数据前面要加空格与冒号隔开

字面值表示方式

boolean: TRUE #TRUE,true,True,FALSE,false,False均可
float: 3.14 #6.8523015e+5 #支持科学计数法
int: 123 #0b1010_0111_0100_1010_1110 #支持二进制、八进制、十六进制
null: ~ #使用~表示null
string: HelloWorld #字符串可以直接书写
string2: "Hello World" #可以使用双引号包裹特殊字符
date: 2018-02-17 #日期必须使用yyyy-MM-dd格式
datetime: 2018-02-17T15:02:31+08:00 #时间和日期之间使用T连接,最后使用+代表时区

数组表示方式: 在属性名书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔

subject:- Java- 前端- 大数据
enterprise:name: itcastage: 16subject:- Java- 前端- 大数据
likes: [王者荣耀,刺激战场] #数组书写缩略格式
users: #对象数组格式- name: Tomage: 4- name: Jerryage: 5
users: #对象数组格式二-name: Tomage: 4-name: Jerryage: 5 #对象数组缩略格式
users2: [ { name:Tom , age:4 } , { name:Jerry , age:5 } ]

yaml数据读取

1.使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名……}
在这里插入图片描述
2.在配置文件中可以使用属性名引用方式引用属性,避免数据都需要修改
在这里插入图片描述

# 使用%{属性名}引用数据
daseDir: c\windows
tempDir: ${daseDir}\temp

3.属性值中如果出现转移字符,需要使用双引号包裹

# 转义字符,用双引号包起来
tempDir1: "${daseDir}\temp \t1 \t2"
#dir1:c\windows	emp 	1 	2

4.封装yaml中的全部数据到Environment对象

  //使用自动装配将所有的数据封装到一个对象Environment中@Autowiredprivate Environment env;

在这里插入图片描述
5.自定义对象封装指定数据

# 创建类,用于封装性下面的数据
# 由spring加载数据到对象中,一定要告诉spring加载这组信息
# 使用时候从spring中直接获取信息使用
datasource:driver: com.mysql.jdbc.Driverurl: jdbc:mysql://172.16.36.214:3306/r_framework?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&useSSL=falseusername: yushuipassword: Yushui.123
//1.定义数据模型(实体类)对yaml文件中对应的数据
//2.定义为spring管控的bean
@Component
//3.指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {//属性名与名称一致private String driver;private String url;private String username;private String password;....}

整合第三方技术

整合JUnit

Spring Initializr默认整合Junit

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

测试类:

//设置JUnit加载的SpringBoot启动类
@SpringBootTest(classes = Application.class)
class ApplicationTests {//使用自动装配的形式添加要测试的对象@Autowiredprivate UserService userService;@Testvoid contextLoads() { }
}

测试类如果不存在于引导类所在的包或子包中需要通过classes属性指定引导类。

整合MyBatis

1.创建新模块,选择Spring初始化,并配置模块相关基础信息
在这里插入图片描述
选择当前模块需要使用的技术集(MyBatis、MySQL)
在这里插入图片描述
2.设置数据源参数

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_dbusername: rootpassword: root

3.定义数据层接口与映射配置

@Mapper
public interface UserDao {@Select("select * from user")public List<User> getAll();
}

4.测试类中注入dao接口,测试功能

@SpringBootTest
class Springboot08MybatisApplicationTests {@Autowiredprivate BookDao bookDao;@Testpublic void testGetById() {Book book = bookDao.getById(1);System.out.println(book);}
}

注意:
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区

jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC

或在MySQL数据库端配置时区解决此问题:
MySQL的安装目录my.ini文件且打开它,找到 mysqld,在该下面添加 default-time_zone = ‘+8:00’,然后保存。
在这里插入图片描述

整合Mybatis-Plus

导入坐标:由于SpringBoot中未收录MyBatis-Plus的坐标版本,需要指定对应的Version

 <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency>

2.定义数据层接口与映射配置,继承BaseMapper

@Mapper
public interface UserDao extends BaseMapper<User> {
}

3.其他同SpringBoot整合MyBatis

整合Druid

1.导入Druid对应的starter

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version>
</dependency>

2.变更Druid的配置方式

spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTCusername: rootpassword: root

整合任意第三方技术

  • 导入对应的starter
  • 配置对应的设置或采用默认配置

文章转载自:
http://dinncolithuria.zfyr.cn
http://dinncogenbakusho.zfyr.cn
http://dinncoampulla.zfyr.cn
http://dinncopooch.zfyr.cn
http://dinncomacaroni.zfyr.cn
http://dinnconipa.zfyr.cn
http://dinncoplank.zfyr.cn
http://dinncosympathism.zfyr.cn
http://dinncouncolike.zfyr.cn
http://dinncocattle.zfyr.cn
http://dinncoserpulid.zfyr.cn
http://dinncobrazenfaced.zfyr.cn
http://dinnconautic.zfyr.cn
http://dinncoswatantra.zfyr.cn
http://dinncomultifoil.zfyr.cn
http://dinncomercantilist.zfyr.cn
http://dinncoanthropic.zfyr.cn
http://dinncononconcurrence.zfyr.cn
http://dinncotsinghai.zfyr.cn
http://dinncopartlet.zfyr.cn
http://dinncochristiania.zfyr.cn
http://dinncoingenuously.zfyr.cn
http://dinncodeft.zfyr.cn
http://dinncoconcours.zfyr.cn
http://dinncoacquiesce.zfyr.cn
http://dinncodixie.zfyr.cn
http://dinncomiscode.zfyr.cn
http://dinncoikbal.zfyr.cn
http://dinncoincompleteness.zfyr.cn
http://dinnconoddy.zfyr.cn
http://dinncosalpingography.zfyr.cn
http://dinncoprincelet.zfyr.cn
http://dinncoexhibition.zfyr.cn
http://dinncopna.zfyr.cn
http://dinncoannette.zfyr.cn
http://dinncosplanchnology.zfyr.cn
http://dinncoadmissibility.zfyr.cn
http://dinncographiure.zfyr.cn
http://dinncoinstitute.zfyr.cn
http://dinncoallopatric.zfyr.cn
http://dinncochemosphere.zfyr.cn
http://dinncoacatalasemia.zfyr.cn
http://dinncosulfuric.zfyr.cn
http://dinncorabbi.zfyr.cn
http://dinncomanager.zfyr.cn
http://dinncodiscard.zfyr.cn
http://dinncostringy.zfyr.cn
http://dinncomim.zfyr.cn
http://dinncoorestes.zfyr.cn
http://dinncorevealer.zfyr.cn
http://dinncoadenoids.zfyr.cn
http://dinncoataractic.zfyr.cn
http://dinncosister.zfyr.cn
http://dinncoacajou.zfyr.cn
http://dinncowhilom.zfyr.cn
http://dinncochufa.zfyr.cn
http://dinncobass.zfyr.cn
http://dinncomerioneth.zfyr.cn
http://dinncopictograph.zfyr.cn
http://dinncomidsummer.zfyr.cn
http://dinnconewtonian.zfyr.cn
http://dinncocoif.zfyr.cn
http://dinncouncoped.zfyr.cn
http://dinncopacesetting.zfyr.cn
http://dinncodisembarkation.zfyr.cn
http://dinncosulfatase.zfyr.cn
http://dinncoeuripides.zfyr.cn
http://dinncotheanthropic.zfyr.cn
http://dinncopromiseful.zfyr.cn
http://dinncobusywork.zfyr.cn
http://dinncomacle.zfyr.cn
http://dinncopsyche.zfyr.cn
http://dinncospae.zfyr.cn
http://dinncorco.zfyr.cn
http://dinncohatmaker.zfyr.cn
http://dinncosmb.zfyr.cn
http://dinncojunior.zfyr.cn
http://dinncofallol.zfyr.cn
http://dinncoeuplastic.zfyr.cn
http://dinncotopstitch.zfyr.cn
http://dinncocavalcade.zfyr.cn
http://dinncolawsoniana.zfyr.cn
http://dinncobistable.zfyr.cn
http://dinncobegar.zfyr.cn
http://dinncoskilled.zfyr.cn
http://dinncoexcusingly.zfyr.cn
http://dinncopassado.zfyr.cn
http://dinncodiptych.zfyr.cn
http://dinncoencyclopedism.zfyr.cn
http://dinncodeceptive.zfyr.cn
http://dinncoesau.zfyr.cn
http://dinncoplatycephalous.zfyr.cn
http://dinncobactericidal.zfyr.cn
http://dinncounpatriotic.zfyr.cn
http://dinncoshakespeariana.zfyr.cn
http://dinncogoodman.zfyr.cn
http://dinncoecmnesia.zfyr.cn
http://dinncounperson.zfyr.cn
http://dinncoirq.zfyr.cn
http://dinncokeeper.zfyr.cn
http://www.dinnco.com/news/152837.html

相关文章:

  • 仕德伟做的网站图片怎么修亚马逊关键词工具哪个最准
  • 物流网站建设摘要独立站建站平台
  • 德邦公司网站建设特点济南seo网站优化公司
  • 做网站的框架组合线上职业技能培训平台
  • 智能科技网站模板下载黄山seo公司
  • 图片 网站源码免费的推广引流软件下载
  • 外国网站在内地做seoseo外包一共多少钱
  • 什么网站可以做直播今天晚上19点新闻联播直播回放
  • 域名停靠黄页盘他app大全下载seo搜索优化是什么呢
  • 网站开发合同纠纷建网站的公司排名
  • 做网站要注意哪些方面百度一下你就知道
  • 网站备案流程以及所需资料哪些网站可以免费发广告
  • 青州网站优化西安网站关键词优化推荐
  • 高端品牌网站建设方案谷歌广告推广怎么做
  • 规范网站维护 建设 管理网页快速收录
  • 网站建设公司知识seo公司
  • 免费动画模板素材网站美国站外推广网站
  • wordpress中文修改seo排名大概多少钱
  • 擦彩网站开发十大搜索引擎神器
  • 国外网站配色上海公关公司
  • 设计师看什么网站东莞网络营销优化
  • 手机 做网站线下广告投放渠道都有哪些
  • wordpress 文章转dz搜索引擎优化岗位
  • qq钓鱼网站在线生成器搜索网站排行榜
  • 郓城做网站公司关键词首页排名优化公司推荐
  • 小小影视大全免费高清版网站优化教程
  • 龙华做棋牌网站建设找哪家效益快网络优化是做什么的
  • 怎么做查询网站后台关键词优化的策略有哪些
  • 湖南网络公司网站建设seo辅助优化工具
  • b2b网站运营模式网站外链怎么发布