項目演示地址:http://www.mawen.co/
快速搭建sprintboot項目
運行第一個springboot項目
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@GetMapping("/greeting")
//其中第一個name為key,第二個String name是用來接收值的
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";//返回template目錄查找頁面
}
}
一個錯誤是:
model.addAttribute("name", name);//兩個name的位置寫反了導致運行的時候頁面識別不到出現null
使用github托管項目
{% asset_img 2019-08-10_08-49-47.png %}
設計使用idea中terminal終端時,由於之前把git重新安裝在D盤,而idea識別的是C盤里面卸載不干凈的git目錄,因此出現
'git' 不是內部或外部命令,也不是可運行的程序 或批處理文件。
解決方法:Idea中Terminal命令不能執行git命令,因為是默認是cmd,按照下圖改為bash就可以了
{% asset_img 20170611035122285.jpg %}
git add .
添加當前所有內容到暫存區里面區
$ git commit -m "add README"
添加這條記錄並描述
git commit --amend --no-edit
--amend 表示追加
--no-edit 表示不編輯
git push
使用這個命令后github上才會出現相應的文件
明確需求
初識Bootstrap
通過快速的前端框架搭好頁面
介紹十二等份柵格系統實現響應式布局
Bootstrap編寫導航欄樣式
引入三個包:樣式文件,css文件,js文件
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 可選的 Bootstrap 主題文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>碼匠社區</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<script src="js/bootstrap.min.js" type="application/javascript"></script>
</head>
<body>
</body>
</html>
注冊Github app
圖解Github登錄流程
編寫時序圖:表示對象和對象通過時間消息的路線
{% asset_img 2019-08-10_10-42-05.png %}
Github登錄之調用authorize
GET https://github.com/login/oauth/authorize
<li><a href="https://github.com/login/oauth/authorize?client_id=251734b4a67768f93428&redirect_uri=http://localhost:8887/callback&scope=user&state=1">登錄</a></li>
演示結果:
http://localhost:8887/callback?code=2bbe5affd7594f4d8a8c&state=1
Github登錄之獲取code
@Controller
public class AuthorizeController {
@GetMapping("/callback")
public String callback(@RequestParam(name = "code") String code,
@RequestParam(name = "state") String state){
return "index";
}
}
Github登錄之獲取用戶信息
**Post to a Server¶**
public static final MediaType JSON
= MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
@Controller 把當前的類作為路由API的承載者
@Component 僅僅把當前類初始化到spring的上下文(可以理解為不需要實例化該類的對象)
參數
| 名稱 | 類型 | 描述 |
|---|---|---|
client_id |
string |
需要。您從GitHub收到的GitHub應用程序的客戶端ID。 |
client_secret |
string |
需要。您從GitHub收到的GitHub應用程序的客戶機密。 |
code |
string |
需要。您收到的代碼作為對第1步的回復。 |
redirect_uri |
string |
應用程序中的URL,用於在授權后發送用戶。 |
state |
string |
您在步驟1中提供的不可思議的隨機字符串。 |
需要的okhttp的依賴
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.1</version>
</dependency>
maven倉庫 查找fastjson
配置Application.properties
@Value("${github.client.id}")
private String clientId;
@Value("${github.client.secret}")
private String clientSecret;
@Value("${github.redirect.uri}")
private String redirectUri;
github.redirect.uri=http://localhost:8887/callback
github.client.secret=395672ecc7481d4aa5a6cee4945d11a09bbe0a4d
github.client.id=251734b4a67768f93428
細說session和cookies的原理及實現
cookie類似銀行卡,而session類似銀行
每次通過瀏覽器的cookie去訪問服務器的session,即拿卡向銀行取錢
圖解MySQL
小匠老師闡述了對於程序員來說畫圖的重要性,這讓我想起來我的uml老師發哥,摸了一學期的魚,應付完考試什么關系全都忘了...
初識H2數據庫
此數據庫可以在嵌入模式或服務器模式下使用。要在嵌入模式下使用它,您需要:
- 添加
h2*.jar到類路徑(H2沒有任何依賴項)- 使用JDBC驅動程序類:
org.h2.Driver- 數據庫URL 在用戶主目錄中
jdbc:h2:~/test打開數據庫test- 將自動創建一個新數據庫
Maven依賴
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<scope>test</scope>
</dependency>
集成MyBatis並實現插入操作
2019-08-12 16:37:32.452 INFO 2064 --- [nio-8887-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-08-12 16:37:32.452 INFO 2064 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
第一次 出現上面這種錯誤,表現為控制台出現這兩串代碼后網頁顯示跳轉找不到頁面或者頁面錯誤,最后發現是修改了application.properities文件的端口號,我修改成了8888,然而原本的controller里面代碼的端口號還是8887,這就導致后面找不到頁面然后出錯。
配置H2數據庫
CREATE USER IF NOT EXISTS sa PASSWORD '123';
ALTER USER sa admin true ;
create table user
(
id int auto_increment,
account_id varchar(100),
name varchar(50),
token char(36),
gmt_create bigint,
gmt_modified bigint,
constraint user_pk
primary key (id)
);
由於H2 數據庫每次只能准許一個用戶進行操作,否則就會出錯,這一點很惱人。
ERROR:500,服務器異常
H2數據庫的賬戶名和密碼錯誤:需要在第一次初始化H2數據庫的時候就配置好數據庫的用戶名還有密碼,按照教程配置
username:sa
password:123<hidden>
@Mapper
public interface UserMapper {
@Insert("insert into user (name,account_id,gmt_create,gmt_modified) values (#{name},#{accountId},#{gmtCreate},#{gmtModified})")
void insert(User user);
上面這個錯誤會報找不到account_id,實際上是后面的accountId我寫成了和前面一樣的account_id,mapper這里用的是user對象的屬性和數據庫的字段不一樣的時候要好好填寫。
還有一個低級錯誤是插入成功后數據庫顯示某個字段為null,結果是mapper里面的sql語句沒有寫進去。
后面總算是搞定數據庫這一個大坑了。
實現登錄狀態持久化獲取
訪問主頁的時候,獲取token然后查詢數據庫
如果數據庫有,那么直接登錄
小tips:編寫mapper的時候,如果參數是個類,則spring會自動綁定變量到sql,如果是某個變量或者參數,則需要一個注解@Param("name")
集成 Flyway Migration
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.2.4</version>
<configuration>
<url>jdbc:h2:file:./target/foobar</url>
<user>sa</user>
</configuration>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
</dependencies>
</plugin>
注意:需要大寫V遞增開頭
rm ~/community.*
mvn flyway:migrate
使用Bootstrap編寫發布問題頁面
完成發布文章功能
create table question
(
id int auto_increment,
title varchar(50),
description text,
gmt_create bigint,
gmt_modified bigint,
creator int,
constraint question_pk
primary key (id)
);
添加lombok支持
這就是個減少setter和getter方法的使用的工具
需要安裝在idea安裝相應的plugins
完成首頁問題列表功能
如果發現寫的樣式沒有實現,那么可能是沒引用css文件
問題答疑
自動部署
講了一個livereload熱部署的插件以及依賴安裝,可以實現自動重啟服務的功能
分頁原理和實現
看到第60分鍾,頂不住了,不學了今天媽的
完善導航欄並進行頁面拆解
導入jquery
通過thymeleaf抽取導航欄成單個文件
用到fragment insert兩個th標簽
個人資料發布問題列表實現
還是分頁的問題 看得心態爆炸 媽的
堅持堅持...
攔截器
把cookie這部分放到攔截器,使代碼復用
通過原碼分析靜態資源無法加載的問題
修復登錄功能
完成更新功能
逆向工程:集成 MyBatis Generator
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
connectionURL="jdbc:db2:TEST"
userId="db2admin"
password="db2admin">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="test.xml" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>
主要是根據官方文檔學會看各種標簽代表的意義。
善用查詢快捷鍵以及翻譯插件
遇到一個bug:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): life.majiang.community.community.mapper.UserMapper.selectByExample
結果發現就是配置文件里面classpath寫成了class
mybatis.mapper-locations=classpath:mapper/*.xml
真的是coding5分鍾,debug兩小時...
使用 ControlerAdvice 和 ExceptionHandler 通用處理異常
實現閱讀數功能
初識API
側邊欄文件
紅色:表示沒有放到暫存空間
綠色:沒變化的
藍色:有變化的
異常處理
49
