初識Spring Boot,開發社群讨論交流網站首頁。
文章目錄
- 初識Spring Boot,開發社群讨論交流網站首頁。
- 1、項目簡介
- 2、 搭建開發環境
-
- JDK
- Apache Maven
- IntelliJ IDEA
- 建立SpringBoot項目
- Spring Boot 入門示例
- 3、Spring入門
-
- Spring全家桶
- Spring Framework
- Spring IoC
- 4、Spring MVC入門
-
- HTTP
- Spring MVC
- Thymeleaf
- 5、Mybatis入門
-
- 安裝資料庫
- MyBatis
- 6、開發社群首頁
- 7、項目調試技巧
- 8、版本控制
- 9、記錄
1、項目簡介
開發一個社群讨論交流網站 社交網站
play-community 玩轉讨論交流社群
技術架構
- Spring Boot
- Spring、Spring MVC、MyBatis - SSM
- Redis、Kafka、Elasticsearch
- Spring Security、Shiro、Spring Actuator
- Swagger
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.
Spring Boot : https://spring.io/projects/spring-boot#overview
項目開發環境
- 建構工具:Apache Maven
- 內建開發工具:IntelliJ IDEA
- 資料庫:MySQL、Redis
- 應用伺服器:Apache Tomcat
- 版本控制工具:Git
2、 搭建開發環境
JDK
JDK(Java Development Kit)
搜尋JDK并下載下傳
https://www.oracle.com/java/technologies/javase-downloads.html
安裝JDK 網上有很多教程的找一個安裝下即可
隻要JDK8以及8以上的就行
檢測JDK安裝是否成功
運作java指令
java -version
運作javac指令
java -version
我安裝的是OpenJDK - 11
C:\Users\x1c>java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
C:\Users\x1c>javac -version
javac 11.0.2
C:\Users\x1c>
Apache Maven
Maven
http://maven.apache.org
http://maven.apache.org/download.cgi
阿裡雲 https://maven.aliyun.com/mvn/view
Maven
- 可以幫助我們建構項目、管理項目中的jar包
- Maven倉庫:存放構件的位置
- 本地倉庫:預設是 ~/.m2/repository
- 遠端倉庫:中央倉庫、鏡像倉庫、私服倉庫
- 示例:安裝、配置、常用指令
Maven conf/settings.xml 加上阿裡鏡像
<mirrors>
<!--阿裡雲倉庫-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
安裝Maven 網上有很多安裝下載下傳教程 配置好環境變量
Microsoft Windows [版本 10.0.17134.1425]
(c) 2018 Microsoft Corporation。保留所有權利。
C:\Users\x1c>mvn -version
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: D:\Environments\apache-maven-3.6.2\bin\..
Java version: 11.0.2, vendor: Oracle Corporation, runtime: D:\Environments\jdk-11.0.2
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
C:\Users\x1c>
一些Maven指令練習
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
[email protected] MINGW64 /f/project/community
$ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
[INFO] Scanning for projects...
------------------------------------------------
...
...
[INFO] BUILD SUCCESS
-------------------------------------------------------------------
[email protected] MINGW64 /f/project/community
$ ls
community/ community-1.1/ community-1.2/ mavendemo2/ my-app/
cd my-app
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
mvn compile
[email protected] MINGW64 /f/project/community/my-app
$ pwd
/f/project/community/my-app
[email protected] MINGW64 /f/project/community/my-app
$ ls
pom.xml src/
[email protected] MINGW64 /f/project/community/my-app
$ mvn compile
[INFO] Scanning for projects...
[INFO]
...
[INFO] BUILD SUCCESS
。。。
mvn clean
[email protected] MINGW64 /f/project/community/my-app
$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.awen.demo:my-app >------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ my-app ---
[INFO] Deleting F:\project\community\my-app\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.687 s
[INFO] Finished at: 2020-04-24T23:13:52+08:00
[INFO] ------------------------------------------------------------------------
mvn clean test
清理完 測試
Build the Project
mvn package
mvn package
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
[email protected] MINGW64 /f/project/community/my-app
$ mvn package
[INFO] Scanning for projects...
...
[INFO] BUILD SUCCESS
...
[email protected] MINGW64 /f/project/community/my-app
$ java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Hello World!
IntelliJ IDEA
IntelliJ IDEA
- 目前最流行的Java內建開發工具 IDE
- 示例:安裝、配置、建立項目
http://www.jetbrains.com/idea
建立SpringBoot項目
可以https://start.spring.io/ 建立,IDEA打開項目。
或者IntelliJ IDEA使用Spring Initializr建立Spring Boot項目
- 建立 Spring Boot 項目的引導工具
- 示例:建立“玩轉讨論交流社群”項目
建立項目的時候選好dependencies 或者找對應依賴可以去:https://mvnrepository.com/
eg:
or
Spring Boot好多了 之前配置地獄
Spring Boot 入門示例
Spring Boot 核心作用
- 起步依賴、自動配置、端點監控
-
示例
一個簡單的處理用戶端請求案例
package com.awen.community.controller HelloController
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/spring")
@ResponseBody
public String sayHello(){
return "Hello Spring!!!";
}
}
application.properties
server.port=8080
server.servlet.context-path=/community
http://localhost:8080/community/hello/spring
3、Spring入門
Spring全家桶
Spring全家桶
- Spring Framework
- Spring Boot
- Spring Cloud
- Spring Cloud Data Flow
https://spring.io
Spring Framework
Spring Core
- IoC、AOP
Spring Data Access
- Transactions、Spring MyBatis
Web Servlet
- Spring MVC
Integration
- Email、Scheduling、AMQP、Security
Spring IoC
耦合 Bean
IOC (Inversion of Control 控制反轉)
控制反轉,是一種面向對象程式設計的設計思想。
DI (Dependency Injection 依賴注入)
依賴注入,是IoC思想的實作方式。
IoC Container
IoC容器,是實作依賴注入的關鍵,本質上是一個工廠。
https://blog.csdn.net/qq_41569732/article/details/103962388
4、Spring MVC入門
HTTP
HTTP
- HyperText Transfer Protocol
- 用于傳輸HTML等内容的應用層協定
-
規定了浏覽器和伺服器之間如何通信,
以及通信時的資料格式。
- https://www.ietf.org
- https://developer.mozilla.org/zh-CN
Spring MVC
Spring MVC
-
三層架構
表現層、業務層、資料通路層
- MVC
- Model:模型層
- View:視圖層
- Controller:控制層
-
核心元件
前端控制器:DispatcherServlet
MVC Model Controller View
表現層 業務層 資料層
Web Server
Thymeleaf
Thymeleaf
-
模闆引擎
生成動态的HTML。
- Thymeleaf
倡導自然模闆,即以HTML檔案為模闆。
-
常用文法
标準表達式、判斷與循環、模闆的布局。
https://www.thymeleaf.org
5、Mybatis入門
安裝資料庫
安裝資料庫
- 安裝MySQL Server
https://dev.mysql.com/downloads/mysql
我安裝的是MySQL5.7 https://downloads.mysql.com/archives/community/
- 安裝MySQL Workbench
https://dev.mysql.com/downloads/workbench
圖形化的
我安裝的是Navicat https://www.navicat.com/en/
可以直接使用MySQL就行的
mysql
C:\Users\x1c>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
C:\Users\x1c>
MyBatis
核心元件
- SqlSessionFactory:用于建立SqlSession的工廠類。
- SqlSession:MyBatis的核心元件,用于向資料庫執行SQL。
- 主配置檔案:XML配置檔案,可以對MyBatis的底層行為做出詳細的配置。
- Mapper接口:就是DAO接口,在MyBatis中習慣性的稱之為Mapper。
-
Mapper映射器:用于編寫SQL,并将SQL和實體類映射的元件,采用XML、注解均可實作。
• 示例
- 使用MyBatis對使用者表進行CRUD操作。
Mybatis
http://www.mybatis.org/mybatis-3
https://mybatis.org/mybatis-3/zh/index.html
什麼是 MyBatis?
MyBatis 是一款優秀的持久層架構,它支援自定義 SQL、存儲過程以及進階映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設定參數和擷取結果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 對象)為資料庫中的記錄。
http://www.mybatis.org/spring
What is MyBatis-Spring?
MyBatis-Spring integrates MyBatis seamlessly with Spring. This library allows MyBatis to participate in Spring transactions, takes care of building MyBatis mappers and SqlSessions and inject them into other beans, translates MyBatis exceptions into Spring DataAccessExceptions, and finally, it lets you build your application code free of dependencies on MyBatis, Spring or MyBatis-Spring.
Spring Boot http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
6、開發社群首頁
開發社群首頁
- 開發流程
- 1次請求的執行過程
- 分步實作
- 開發社群首頁,顯示前10個文章
- 開發分頁元件,分頁顯示所有的文章
7、項目調試技巧
- 響應狀态碼的含義
- 服務端斷點調試技巧
- 用戶端斷點調試技巧
- 設定日志級别,并将日志輸出到不同的終端
- https://logback.qos.ch
8、版本控制
版本控制
- 認識Git
- Git簡介
- Git的安裝與配置
- Git常用指令
- 将代碼送出至本地倉庫
- 将代碼上傳至遠端倉庫
- IDEA內建Git
-
在IDEA中配置并使用Git
https://git-scm.com
9、記錄
1、高薪求職項目課 - vol.7 - https://www.nowcoder.com/courses/semester/senior
是記錄這個社群項目的筆記。
Github : https://github.com/liuawen/play-community
Thanks