日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

浅谈SpringBoot的基本概念与简单的使用与yml文件的基本使用, 整合Redis,整合MyBatis

發布時間:2024/4/15 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈SpringBoot的基本概念与简单的使用与yml文件的基本使用, 整合Redis,整合MyBatis 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SpringBoot

什么是SpringBoot

SpringBoot提供了一種快速使用Spring的方式,基于約定優于配置的思想,可以讓開發人員不必在配置與邏輯業務之間進行思維的切換,全身心的投入到邏輯業務的代碼編寫中,從而大大提高了開發的效率,一定程度上縮短了項目周期。2014 年 4 月,Spring Boot 1.0.0 發布。Spring的頂級項目之一(https://spring.io)。

SpringBoot 功能

1) 自動配置
Spring Boot的自動配置是一個運行時(更準確地說,是應用程序啟動時)的過程,考慮了眾多因素,才決定Spring配置應該用哪個,不該用哪個。該過程是SpringBoot自動完成的。
2) 起步依賴
起步依賴本質上是一個Maven項目對象模型(Project Object Model,POM),定義了對其他庫的傳遞依賴,這些東西加在一起即支持某項功能。
簡單的說,起步依賴就是將具備某種功能的坐標打包到一起,并提供一些默認的功能。
3) 輔助功能
提供了一些大型項目中常見的非功能性特性,如嵌入式服務器、安全、指標,健康檢測、外部配置等。

簡單來說:Spring Boot 并不是對 Spring 功能上的增強,而是提供了一種快速使用 Spring 的方式。

SpringBoot的簡單入門(代碼詳細解釋,yml格式文件書寫方法)

SpringBoot在創建項目時,使用jar的打包方式。
SpringBoot的引導類,是項目入口,運行main方法就可以啟動項目。
使用SpringBoot和Spring構建的項目,業務代碼編寫方式完全一樣。

構建maven項目

pom.xml

主要引入springboot父項目

<!-- 導入springboot父工程spring-boot-starter-parent,我們自己的的helloworld就成為springboot的子工程會自動引入父工程的依耐--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent>

主啟動

package com.fs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/* 引導類,springBoot項目的入口 放在項目的根目錄就會自動@ComponentScan(啟動類已經啟動類下的包)*/ @SpringBootApplication public class HelloWorld {public static void main(String[] args) {SpringApplication.run(HelloWorld.class,args);} }

springboot的配置

推薦使用yml

YAML

YAML全稱是 YAML Ain’t Markup Language 。YAML是一種直觀的能夠被電腦識別的的數據數據序列化格式,并且容易被人類閱
讀,容易和腳本語言交互的,可以被支持YAML庫的不同的編程語言程序導入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP
等。YML文件是以數據為核心的,比傳統的xml方式更加簡潔。
YAML文件的擴展名可以使用.yml或者.yaml。

YAML:基本語法

? 大小寫敏感
? 數據值前邊必須有空格,作為分隔符
? 使用縮進表示層級關系
? 縮進時不允許使用Tab鍵,只允許使用空格(各個系統 Tab對應的 空格數目可能不同,導致層次混亂)。
? 縮進的空格數目不重要,只要相同層級的元素左側對齊即可
? # 表示注釋,從這個字符一直到行尾,都會被解析器忽略。

YAML:數據格式


YAML:小結

1) 配置文件類型
? properties:和以前一樣
? yml/yaml:注意空格
2) yaml:簡潔,以數據為核心
? 基本語法
? 大小寫敏感
? 數據值前邊必須有空格,作為分隔符
? 使用空格縮進表示層級關系,相同縮進表示同一級
? 數據格式
? 對象
? 數組: 使用 “- ”表示數組每個元素
? 純量
? 參數引用
? ${key}

讀取配置文件內容

  • @Value
  • Environment:程序啟動時配置的環境變量,使用此方式獲取
  • @ConfigurationProperties(prefix = “person”) 把一組屬性注入到一個對象中
代碼實現

application.yml

# springboot的配置文件 springboot項目啟動的時候會自動加載classes目錄下的application.yml或者properties文件 # yml 簡潔,以數據為核心 # 一個工程支持多個配置文件,包括properties和yml和yaml # 當屬性沖突時 會按照properties>yml>yaml 的優先級生效 # 配置tomcat的端口 不配置springboot默認配置8080 server:port: 80805 # 給項目起一個名稱 spring:application:name: HelloWorld# 配置多環境配置方式1:多文件方式 (在resources下創建yml配置文件,也可以是properties,開發,測試,生產等)# 文件名字必須為application-環境名.yml/properties# 只要主配置文件中寫了profiles.active: 環境名 springboot就會根據環境名去classes目錄找application-環境名.yml的文件profiles:active: dev# 配置多環境方式2:yml文檔方式(了解,不建議使用) 也可以下面這樣使用--- 分割文件設置多環境 #--- #server: # port: 8081 #spring: # profiles: dev # 開發環境 #--- #server: # port: 8082 #spring: # profiles: test # 測試環境 #--- #server: # port: 8083 #spring: # profiles: pro # 生產環境 #---# yml的數據格式 定義后可以通過java代碼來獲取 # 定義一個參數 sex:# 對象(map) 鍵值對的集合 person:name: xiaofuage: 18sex: ${sex} # 參數引用(引用的參數一定要提前定義) # 對象的行內寫法(一般不會使用,了解即可) person2: {name: xiaohua,age: 18}# 數組 按次排序列 address:- 重慶- 開縣 #數組的行內寫法(一般不會使用,了解即可) address2: [重慶,開縣]# 純量: 單個的 name: 'HelloWorld \n xiaofu' #單引號不會識別轉義字符,會原樣輸出 name2: "HelloWorld \n xiaofu" # 雙引號會識別轉義字符

使用@ConfigurationProperties 方式注意事項

@Component //讀取配置文件中一組屬性,存放在我們的person對象中 @ConfigurationProperties(prefix = "person")//注入yml配置文件配置的對象.需要指定前綴 public class Person {private String name;private Integer age;private String sex; get set方法 }
測試類代碼

需要在pom文件整合junit

④ 添加測試相關注解
? @RunWith(SpringRunner.class)
? @SpringBootTest(classes = 啟動類.class)

package com.fs;import com.fs.pojo.Person; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.env.Environment; import org.springframework.test.context.junit4.SpringRunner;/* 編寫測試類 添加測試相關注解 ? @RunWith(SpringRunner.class) ? @SpringBootTest(classes = 啟動類.class) 編寫測試方法*/ //指定測試運行容器 @RunWith(SpringRunner.class) //@SpringBootTest//若這個測試類的包與啟動類的包是一樣的,那么就可以省略classes = HelloWorld.class @SpringBootTest(classes = HelloWorld.class)//就會去加載主啟動類的配置文件以及掃描的包 class HelloWorldTest {//測試讀取yam文件的配置@Value("${name}")private String name;@Value("${person.name}")private String personName;//使用sprinboot對象獲取yml文件屬性@Autowiredprivate Environment environment;//使用@ConfigurationProperties注解在實體類上自動映射@Autowiredprivate Person person;@Testvoid contextLoads() {System.out.println(name);//HelloWorld \n xiaofuSystem.out.println(personName);//xiaofuSystem.out.println("-----------------------------------------");//使用對象.getProperty方法獲取System.out.println(environment.getProperty("person.sex"));//男System.out.println(environment.getProperty("address2[0]"));//重慶System.out.println(environment.getProperty("name2"));//這個會解析轉義所以輸出的HelloWorld 換行了的 xiaofuSystem.out.println("-------------------------");System.out.println(person);//Person{name='xiaofu', age=18, sex='男'}}}

SpringBoot多環境屬性配置profile

profile
我們在開發Spring Boot應用時,通常同一套程序會被安裝到不同環境,比如:開發、測試、生產等。其中數據庫地址、服務
器端口等等配置都不同,如果每次打包時,都要修改配置文件,那么非常麻煩。profile功能就是來進行動態配置切換的。
1) profile是用來完成不同環境下,配置動態切換功能的。
2) profile配置方式
? 多profile文件方式:提供多個配置文件,每個代表一種環境。
? application-dev.properties/yml 開發環境
? application-test.properties/yml 測試環境
? application-pro.properties/yml 生產環境
? yml多文檔方式:
? 在yml中使用 — 分隔不同配置
3) profile激活方式
? 配置文件: 再配置文件中配置:spring.profiles.active=dev
? 虛擬機參數:在VM options 指定:-Dspring.profiles.active=dev
? 命令行參數:java –jar xxx.jar --spring.profiles.active=dev

內部配置加載順序

Springboot程序啟動時,會從以下位置加載配置文件:

  • file:./config/:當前項目下的/config目錄下
  • file:./ :當前項目的根目錄
    3.== classpath:/config/:classpath的/config目錄==
  • classpath:/ :classpath的根目錄
    加載順序為上文的排列順序,高優先級配置的屬性會生效
  • 外部配置加載順序
    通過官網查看外部屬性加載順序:
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

    SpringBoot整合Redis

    ① 搭建SpringBoot工程
    ② 引入mybatis起步依賴,添加mysql驅動
    ③ 編寫DataSource和MyBatis相關配置
    ④ 定義表和實體類
    ⑤ 編寫dao和mapper文件/純注解開發
    ⑥ 測試

    pomxml

    <?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><!-- 導入springboot父工程spring-boot-starter-parent,我們自己的的helloworld就成為springboot的子工程會自動引入父工程的依耐--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.com.fs</groupId><artifactId>springboot-redis</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- 引入web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> <!-- 引入redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency> <!-- 整合junit--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

    application.yml

    # 當不知道配置的參數時候,idea雙擊shift 輸入***Properties 就可以看到有這些配置屬性類 spring:redis:host: 47.112.174.148 # redis的主機名port: 6379 #redis端口號

    主啟動

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

    測試類

    //注入redisTemplate 這是spring寫的 //Key:value 使用原生JdkSerializationRedisSerializer的序列化,存入的字符串會亂碼 @Autowired private RedisTemplate redisTemplate;//stringRedisTemplate這個是將對象快速的序列化到redis中 //Key:value都是字符串的序列化方式 這樣序列化對象到redis中不會亂碼 @Autowired private StringRedisTemplate stringRedisTemplate;

    stringRedisTemplate這個是將對象快速的序列化到redis中,Key:value都是字符串的序列化方式 這樣序列化對象到redis中不會亂碼

    package com.fs;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootRedis.class) public class SpringBootRedisTest {//注入redisTemplate 這是spring寫的//Key:value 使用原生JdkSerializationRedisSerializer的序列化,存入的字符串會亂碼@Autowiredprivate RedisTemplate redisTemplate;//stringRedisTemplate這個是將對象快速的序列化到redis中//Key:value都是字符串的序列化方式 這樣序列化對象到redis中不會亂碼@Autowiredprivate StringRedisTemplate stringRedisTemplate;//測試存入@Testpublic void testAddRedis(){//存入數據 // redisTemplate.opsForValue().set("name","xiaofu");stringRedisTemplate.opsForValue().set("name","xiaofu");}//測試獲取@Testpublic void testGetRedis(){System.out.println("----------------------------------------");//取出數據 // System.out.println(redisTemplate.opsForValue().get("name"));System.out.println(stringRedisTemplate.opsForValue().get("name"));} }

    SpringBoot整合MyBatis

    ① 搭建SpringBoot工程
    ② 引入mybatis起步依賴,添加mysql驅動
    ③ 編寫DataSource和MyBatis相關配置
    ④ 定義表和實體類
    ⑤ 編寫dao和mapper文件/純注解開發
    ⑥ 測試

    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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!-- 導入springboot父工程spring-boot-starter-parent,我們自己的的helloworld就成為springboot的子工程會自動引入父工程的依耐--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.com.fs</groupId><artifactId>springboot-MyBatis</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- 引入web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> <!-- 引入MyBatis--><!-- MyBatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- druid--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.20</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency> <!-- 整合junit--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies> </project>

    application.yml

    spring:datasource:username: rootpassword: rooturl: jdbc:mysql://47.112.174.148:3306/healthdriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource #自定義數據源# 配置MyBatis文件路徑 mybatis:mapper-locations: classpath:com/fs/dao/*Dao.xml # dao映射路徑type-aliases-package: com.fs.pojo

    主啟動

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

    dao Mapper 接口

    package com.fs.mapper;import com.fs.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;import java.util.List; /* 注解的方式*/ @Mapper public interface UserMapper {@Select("select * from t_user")public List<User> findAll(); } package com.fs.mapper;import com.fs.pojo.User; import org.apache.ibatis.annotations.Mapper;//xml方式 @Mapper public interface UserDao {User findById(Integer id); }

    Mapper.xml映射文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.fs.mapper.UserDao"><!-- 查詢用戶byid--><select id="findById" resultType="com.fs.pojo.User" parameterType="integer">select * from `t_user` where id = #{id}</select></mapper>

    pojo

    public class User implements Serializable{private Integer id; // 主鍵private Date birthday; // 生日private String gender; // 性別private String username; // 用戶名,唯一private String password; // 密碼private String remark; // 備注private String station; // 狀態private String telephone; // 聯系電話get set }

    測試類

    package com.fs;import com.fs.mapper.UserDao; import com.fs.mapper.UserMapper; import com.fs.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import java.util.List;@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootMyBatis.class) public class SpringBootMyBatisTest {//注入我們的注解方式@Autowiredprivate UserMapper userMapper;//注入xml的@Autowiredprivate UserDao userDao;@Testpublic void findAll() {List<User> all = userMapper.findAll();for (User user : all) {System.out.println(user);}}@Testpublic void findById() {User byId = userDao.findById(1);System.out.println(byId);} }

    測試結果

    超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

    總結

    以上是生活随笔為你收集整理的浅谈SpringBoot的基本概念与简单的使用与yml文件的基本使用, 整合Redis,整合MyBatis的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。