1、SpringBoot Starter講解html
簡介:介紹什麼是SpringBoot Starter和主要做用java
一、官網地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-starternode
二、starter主要簡化依賴用的mysql
spring-boot-starter-web ->裏面包含多種依賴git
三、幾個經常使用的startergithub
spring-boot-starter-activemqweb
spring-boot-starter-aopredis
spring-boot-starter-data-redisspring
spring-boot-starter-freemarkersql
spring-boot-starter-thymeleaf
spring-boot-starter-webflux
2、SpringBoot2.x常見模板引擎講解和官方推薦使用
簡介:介紹經常使用的SpringBoot2.x模板引擎和官方推薦案例
一、JSP(後端渲染,消耗性能)
Java Server Pages 動態網頁技術,由應用服務器中的JSP引擎來編譯和執行,再將生成的整個頁面返回給客戶端
能夠寫java代碼
持表達式語言(el、jstl)
內建函數
JSP->Servlet(佔用JVM內存)permSize
javaweb官方推薦
springboot不推薦 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations
二、Freemarker
FreeMarker Template Language(FTL) 文件通常保存爲 xxx.ftl
嚴格依賴MVC模式,不依賴Servlet容器(不佔用JVM內存)
內建函數
三、Thymeleaf (主推)
輕量級的模板引擎(負責邏輯業務的不推薦,解析DOM或者XML會佔用多的內存)
能夠直接在瀏覽器中打開且正確顯示模板頁面
直接是html結尾,直接編輯
xdlcass.net/user/userinfo.html
社會工程學
假裝
3、SpringBoot整合模板引擎freemarker實戰
簡介:SpringBoot2.x整合模板引擎freemarker實戰
一、Freemarker相關maven依賴
<!-- 引入freemarker模板引擎的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
二、Freemarker基礎配置
# 是否開啓thymeleaf緩存,本地爲false,生產建議爲true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
#類型
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
#文件後綴
spring.freemarker.suffix=.ftl
#路徑
spring.freemarker.template-loader-path=classpath:/templates/
三、創建文件夾
1)src/main/resources/templates/fm/user/
2)創建一個index.ftl
3)user文件夾下面創建一個user.html
四、簡單測試代碼編寫和訪問
4、SpringBoot2整合模板引擎thymeleaf實戰
講解:SpringBoot2.x整合模板引擎thymeleaf實戰
官網地址:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html
一、thymeleaf相關maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、thymeleaf基礎配置
#開發時關閉緩存,否則無法看到實時頁面
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#前綴
spring.thymeleaf.prefix=classpath:/templates/
#編碼
spring.thymeleaf.encoding=UTF-8
#類型
spring.thymeleaf.content-type=text/html
#名稱的後綴
spring.thymeleaf.suffix=.html
三、創建文件夾
1)src/main/resources/templates/tl/
2)創建一個index.html
四、簡單測試代碼編寫和訪問
注意:$表達式只能寫在th標籤內部
快速入門:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html
5、SpringBoot2.x持久化數據方式介紹
簡介:介紹近幾年經常使用的訪問數據庫的方式和優缺點
一、原始java訪問數據庫
開發流程麻煩
一、註冊驅動/加載驅動
Class.forName("com.mysql.jdbc.Driver")
二、創建鏈接
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","root","root");
三、建立Statement
四、執行SQL語句
五、處理結果集
六、關閉鏈接,釋放資源
二、apache dbutils框架
比上一步簡單點
官網:https://commons.apache.org/proper/commons-dbutils/
三、jpa框架
spring-data-jpa
jpa在複雜查詢的時候性能不是很好
四、Hiberante 解釋:ORM:對象關係映射Object Relational Mapping
企業大都喜歡使用hibernate
五、Mybatis框架
互聯網行業一般使用mybatis
不提供對象和關係模型的直接映射,半ORM
6、SpringBoot2.x整合Mybatis3.x註解實戰
簡介:SpringBoot2.x整合Mybatis3.x註解配置實戰
一、使用starter, maven倉庫地址:http://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
二、加入依賴(能夠用 http://start.spring.io/ 下載)
<!-- 引入starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
<scope>runtime</scope>
</dependency>
<!-- MySQL的JDBC驅動包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 引入第三方數據源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
三、加入配置文件
#mybatis.type-aliases-package=net.xdclass.base_project.domain
#能夠自動識別
#spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/movie?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
spring.datasource.password =password
#若是不使用默認的數據源 (com.zaxxer.hikari.HikariDataSource)
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
加載配置,注入到sqlSessionFactory等都是springBoot幫咱們完成
四、啓動類增長mapper掃描
@MapperScan("net.xdclass.base_project.mapper")
技巧:保存對象,獲取數據庫自增id
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
四、開發mapper
參考語法 http://www.mybatis.org/mybatis-3/zh/java-api.html
五、sql腳本
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL COMMENT '名稱',
`phone` varchar(16) DEFAULT NULL COMMENT '用戶手機號',
`create_time` datetime DEFAULT NULL COMMENT '建立時間',
`age` int(4) DEFAULT NULL COMMENT '年齡',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
相關資料:
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Configuration
https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples
整合問題集合:
https://my.oschina.net/hxflar1314520/blog/1800035
https://blog.csdn.net/tingxuetage/article/details/80179772
7、SpringBoot整合Mybatis實操和打印SQL語句
講解:SpringBoot2.x整合Mybatis3.x增刪改查實操, 控制檯打印sql語句
一、控制檯打印sql語句
#增長打印sql語句,通常用於本地開發測試
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
二、增長mapper代碼
@Select("SELECT * FROM user")
@Results({
@Result(column = "create_time",property = "createTime") //javaType = java.util.Date.class
})
List<User> getAll();
@Select("SELECT * FROM user WHERE id = #{id}")
@Results({
@Result(column = "create_time",property = "createTime")
})
User findById(Long id);
@Update("UPDATE user SET name=#{name} WHERE id =#{id}")
void update(User user);
@Delete("DELETE FROM user WHERE id =#{userId}")
void delete(Long userId);
三、增長API
@GetMapping("find_all")
public Object findAll(){
return JsonData.buildSuccess(userMapper.getAll());
}
@GetMapping("find_by_Id")
public Object findById(long id){
return JsonData.buildSuccess(userMapper.findById(id));
}
@GetMapping("del_by_id")
public Object delById(long id){
userMapper.delete(id);
return JsonData.buildSuccess();
}
@GetMapping("update")
public Object update(String name,int id){
User user = new User();
user.setName(name);
user.setId(id);
userMapper.update(user);
return JsonData.buildSuccess();
}
8、事務介紹和常見的隔離級別,傳播行爲
簡介:講解什麼是數據庫事務,常見的隔離級別和傳播行爲
一、介紹什麼是事務,單機事務,分佈式事務處理等
二、講解場景的隔離級別
Serializable: 最嚴格,串行處理,消耗資源大
Repeatable Read:保證了一個事務不會修改已經由另外一個事務讀取但未提交(回滾)的數據
Read Committed:大多數主流數據庫的默認事務等級
Read Uncommitted:保證了讀取過程當中不會讀取到非法數據。
三、講解常見的傳播行爲
PROPAGATION_REQUIRED--支持當前事務,若是當前沒有事務,就新建一個事務,最多見的選擇。
PROPAGATION_SUPPORTS--支持當前事務,若是當前沒有事務,就以非事務方式執行。
PROPAGATION_MANDATORY--支持當前事務,若是當前沒有事務,就拋出異常。
PROPAGATION_REQUIRES_NEW--新建事務,若是當前存在事務,把當前事務掛起, 兩個事務之間沒有關係,一個異常,一個提交,不會同時回滾
PROPAGATION_NOT_SUPPORTED--以非事務方式執行操做,若是當前存在事務,就把當前事務掛起。
PROPAGATION_NEVER--以非事務方式執行,若是當前存在事務,則拋出異常
9、SpringBoot整合mybatis之事務處理實戰
簡介:SpringBoot整合Mybatis之事務處理實戰
一、service邏輯引入事務 @Transantional(propagation=Propagation.REQUIRED)
二、service代碼
@Override
@Transactional
public int addAccount() {
User user = new User();
user.setAge(9);
user.setCreateTime(new Date());
user.setName("事務測試");
user.setPhone("000121212");
userMapper.insert(user);
int a = 1/0;
return user.getId();
}
10、源碼編譯安裝Redis4.x
簡介:使用源碼安裝Redis4.x和配置外網訪問
一、快速安裝 https://redis.io/download#installation
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar xzf redis-4.0.9.tar.gz
cd redis-4.0.9
make
啓動服務端:src/redis-server
啓動客戶端:src/redis-cli
二、默認是本地訪問的,須要開放外網訪問
1)打開redis.conf文件在NETWORK部分修改
註釋掉bind 127.0.0.1可使全部的ip訪問redis
修改 protected-mode,值改成no
11、SpringBoot2.x整合redis實戰講解
簡介:使用springboot-starter整合reids實戰
一、官網:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-redis
集羣文檔:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#cluster
二、springboot整合redis相關依賴引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
三、相關配置文件配置
#=========redis基礎配置=========
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6390
# 鏈接超時時間 單位 ms(毫秒)
spring.redis.timeout=3000
#=========redis線程池設置=========
# 鏈接池中的最大空閒鏈接,默認值也是8。
spring.redis.pool.max-idle=200
#鏈接池中的最小空閒鏈接,默認值也是0。
spring.redis.pool.min-idle=200
# 若是賦值爲-1,則表示不限制;pool已經分配了maxActive個jedis實例,則此時pool的狀態爲exhausted(耗盡)。
spring.redis.pool.max-active=2000
# 等待可用鏈接的最大時間,單位毫秒,默認值爲-1,表示永不超時
spring.redis.pool.max-wait=1000
四、常見redistemplate種類講解和緩存實操(使用自動注入)
一、注入模板
@Autowired
private StirngRedisTemplate strTplRedis
二、類型String,List,Hash,Set,ZSet
對應的方法分別是opsForValue()、opsForList()、opsForHash()、opsForSet()、opsForZSet()
12、SpringBoot定時任務schedule講解
簡介:講解什麼是定時任務和常見定時任務區別
一、常見定時任務 Java自帶的java.util.Timer類
timer:配置比較麻煩,時間延後問題
timertask:不推薦
二、Quartz框架
配置更簡單
xml或者註解
三、SpringBoot使用註解方式開啓定時任務
1)啓動類裏面 @EnableScheduling開啓定時任務,自動掃描
2)定時任務業務類 加註解 @Component被容器掃描
3)定時執行的方法加上註解 @Scheduled(fixedRate=2000) 按期執行一次
十3、SpringBoot經常使用定時任務配置實戰
簡介:SpringBoot經常使用定時任務表達式配置和在線生成器
一、cron 定時任務表達式 @Scheduled(cron="*/1 * * * * *") 表示每秒
1)crontab 工具 https://tool.lu/crontab/
二、fixedRate: 定時多久執行一次(上一次開始執行時間點後xx秒再次執行;)
三、fixedDelay: 上一次執行結束時間點後xx秒再次執行
四、fixedDelayString: 字符串形式,能夠經過配置文件指定
十4、SpringBoot2.x異步任務實戰(核心知識)
簡介:講解什麼是異步任務,和使用SpringBoot2.x開發異步任務實戰
一、什麼是異步任務和使用場景:適用於處理log、發送郵件、短信……等
下單接口->查庫存 100
餘額校驗 150
風控用戶100
....
二、啓動類裏面使用@EnableAsync註解開啓功能,自動掃描
三、定義異步任務類並使用@Component標記組件被容器掃描,異步方法加上@Async
注意點:
1)要把異步任務封裝到類裏面,不能直接寫到Controller
2)增長Future<String> 返回結果 AsyncResult<String>("task執行完成");
3)若是須要拿到結果 須要判斷所有的 task.isDone()
四、經過注入方式,注入到controller裏面,若是測試先後區別則改成同步則把Async註釋掉
十5、新日誌框架LogBack介紹
簡介:日誌介紹和新日誌框架Logback講解
1.經常使用處理java的日誌組件 slf4j,log4j,logback,common-logging 等
二、logback介紹:基於Log4j基礎上大量改良,不能單獨使用,推薦配合日誌框架SLF4J來使用
logback當前分紅三個模塊:logback-core,logback-classic和logback-access;
logback-core是其它兩個模塊的基礎模塊
三、Logback的核心對象:
Logger:日誌記錄器
Appender:指定日誌輸出的目的地,目的地能夠是控制檯,文件
Layout:日誌佈局 格式化日誌信息的輸出
四、日誌級別:DEBUG < INFO < WARN < ERROR
===========log4j示例===========
### 設置###
log4j.rootLogger = debug,stdout,D,E
### 輸出信息到控制擡 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 輸出DEBUG 級別以上的日誌到=D://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 輸出ERROR 級別以上的日誌到=D://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
===========logback============
四、Log4j日誌轉換爲logback在線工具(支持log4j.properties轉換爲logback.xml,不支持 log4j.xml轉換爲logback.xml)
https://logback.qos.ch/translator/
十6、SpringBoot2.x日誌講解和Logback配置實戰
簡介:講解SpringBoot2.x整合Logback配置實戰
一、官網介紹:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-logging
各個組件案例:https://logback.qos.ch/manual/index.html
二、分析SpringBoot啓動日誌
1)默認狀況下,Spring Boot將日誌輸出到控制檯
三、整合Logback實戰
1)建立 日誌文件logback-spring.xml,官方推薦 -spring.xml結尾
默認加載加載配置順序 logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
註釋:
<configuration> 子節點
<appender></appender>
<logger></logger>
<root></root>(要加在最後)
十7、搜索知識和搜索框架elasticsearch介紹
簡介:經過京東電商 介紹什麼是搜索引擎,和開源搜索框架ElasticSearch6.x新特性介紹
前言:介紹ES的主要特色和使用場景,新特性講解
mysql:like 模糊,性能問題,
solr:針對企業,Lucene
elasticsearch:針對數據量特別大,PB,TB
純java開發,springboot使用,5.6版本
es升級4->5版本,改動大,可是5版本後,改動不大
elasticSearch主要特色
一、特色:全文檢索,結構化檢索,數據統計、分析,接近實時處理,分佈式搜索(可部署數百臺服務器),處理PB級別的數據
搜索糾錯,自動完成
二、使用場景:日誌搜索,數據聚合,數據監控,報表統計分析
三、國內外使用者:維基百科,Stack Overflow,GitHub
新特性講解
一、6.2.x版本基於Lucene 7.x,更快,性能進一步提高,對應的序列化組件,升級到Jackson 2.8
mysql:database table rocord
es : index type(只能存在一個) document
二、推薦使用5.0版本推出的Java REST/HTTP客戶端,依賴少,比Transport使用更方便,在基準測試中,性能並不輸於Transport客戶端,
在5.0到6.0版本中,每次有對應的API更新, 文檔中也說明,推薦使用這種方式進行開發使用,全部可用節點間的負載均衡
在節點故障和特定響應代碼的狀況下進行故障轉移,失敗的鏈接處罰(失敗的節點是否重試取決於失敗的連續次數;失敗的失敗次數越多,客戶端在再次嘗試同一節點以前等待的時間越長)
三、(重要)再也不支持一個索引庫裏面多個type,6.x版本已經禁止一個index裏面多個type,因此一個index索引庫只能存在1個type
官方文檔:
一、6.0更新特性
https://www.elastic.co/guide/en/elasticsearch/reference/6.0/release-notes-6.0.0.html#breaking-java-6.0.0
二、6.1更新特性
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/release-notes-6.1.0.html
十8、快速部署ElastcSearch5.6.x
簡介:講解爲何不用ES6.x版本,及本地快速安裝ElasticSeach和場景問題處理
配置JDK1.8
使用wget 下載elasticsearch安裝包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.tar.gz
解壓
tar -zxvf elasticsearch-5.6.8.tar.gz
官網:https://www.elastic.co/products/elasticsearch
外網訪問配置:
config目錄下面elasticsearch.yml
修改成 network.host: 0.0.0.0
配置es出現相關問題處理(阿里雲、騰訊雲,亞馬遜雲安裝問題集合):
一、問題一
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/software/temp/elasticsearch-6.2.2/hs_err_pid1912.log
解決:內存不夠,購買阿里雲的機器能夠動態增長內存
二、問題二
[root@iZwz95j86y235aroi85ht0Z bin]# ./elasticsearch
[2018-02-22T20:14:04,870][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.2.jar:6.2.2]
解決:用非root用戶
添加用戶:useradd -m 用戶名 而後設置密碼 passwd 用戶名
三、問題三
./elasticsearch
Exception in thread "main" java.nio.file.AccessDeniedException: /usr/local/software/temp/elasticsearch-6.2.2/config/jvm.options
解決:權限不夠 chmod 777 -R 當前es目錄
常見配置問題資料:https://www.jianshu.com/p/c5d6ec0f35e0
十9、ElasticSearch5.6測試數據準備
簡介: ElasticSearch5.6.x簡單測試
一、步驟 https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html
二、使用POSTMAN 工具
基礎
查看集羣狀態:localhost:9200/_cat/health?v
查看索引列表:localhost:9200/_cat/indices?v
二10、SpringBoot2.x整合elasticsearch5.6.x
簡介:SpringBoot2.x整合elasticSearch5.6.8實戰
Spring Data Elasticsearch文檔地址
https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
版本說明:SpringBoot整合elasticsearch
https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix
一、添加maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
二、接口繼承ElasticSearchRepository,裏面有不少默認實現
注意點:
索引名稱記得小寫,類屬性名稱也要小寫
新建實體對象article
加上類註解 @Document(indexName = "blog", type = "article")
三、配置文件:
# ELASTICSEARCH (ElasticsearchProperties)
spring.data.elasticsearch.cluster-name=elasticsearch # Elasticsearch cluster name.
spring.data.elasticsearch.cluster-nodes=localhost:9300 # Comma-separated list of cluster node addresses.
spring.data.elasticsearch.repositories.enabled=true # Whether to enable Elasticsearch repositories.
四、QueryBuilder使用
https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.3/query-dsl-queries.html
//單個匹配,搜索name爲jack的文檔
QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "搜");
四、查看es數據
查看索引信息:http://localhost:9200/_cat/indices?v
查看某個索引庫結構:http://localhost:9200/blog
查看某個對象:http://localhost:9200/blog/article/1
教程會持續更新。。。。
更多學習資料可參考:https://xdclass.net/html/course_catalogue.html?video_id=4