spring boot 0605

在設定檔裡面把這行註解調,否則會自動建表,但已經有表格就不會幫你建立表格

在前面加#來註解
spring.jpa.hibernate.ddl-auto=update


  1. servlet+ jsp +jdbc
  2. spring + springMVC+ hibernate
  3. spring boot

大多銀行都還在用1
但每一種都要會,不能只會一種


約定優於配置,把一些常用的先預設好
port號 8080
content text /

https://spring.io/tools

sts4

上課雲端硬碟
https://drive.google.com/drive/folders/1IwWzkXPVnPa_Uba7Ces0sMrDu3u_S-ct?usp=sharing


優先去marketplace 安裝thymeleaf

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

接下來就能把Web底下的CSS/HTML的encoding 調整為UTF-8

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

也可以直接根據P.18 頁的一次加入所有需要的mvn依賴

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

右鍵run as 選第二個或第四個

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

不小心按到兩次啟動可以更換port,通常等五分鐘就換回來

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

所以他是在那個包裹下面喔

之後model/controller都加在這層下面


hibernate.dialect 指定資料庫還有語法

https://docs.jboss.org/hibernate/orm/6.1/javadocs/

Hibernate最重要的功能就是跨資料庫
透過Java產生JDBC跟資料庫
不用下SQL指令

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

SpringBoot 內建HikariPool 連線池

.properties設定檔 要注意空格,也會被讀進去

@ResponseBody 不會回傳view ,只會在頁面上顯示回傳的字串

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

ResfulAPI 就是回傳json給前端,這樣前端只要能接收字串就能跨平台/裝置


https://code101.app/

泛型<>內不能有primitive type

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

又再亂跳,設定這個

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

測試用的,上班時候要註解掉這一行

會自動幫你建立table

先停止server在建立bean,否則一存檔就會被建立table

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

jpa是hibernate 的上一層

所以方法名稱不一樣

https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/support/SimpleJpaRepository.html

實際幫你做事情的jpa SimpleJpaRepository

但只能讀取 ,查詢不會複寫掉交易的read only屬性

save 則會複寫掉

executeUpdate 都會複寫掉 @Transactional(readOnly=true) >新增修改刪除

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

真正做事的

dao 也是一個bean ,@Repository 是給spring 看的

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

新增時候不需要id ,因為id透過資料庫新增

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

hibernate生命週期
hibernate lifecycle

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

@PersistenceContext 取代原本的@Autowired

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

?1 代表第一個參數

public List<Customer> findByPage

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

回家要importㄉ化

直接import maven專案

備份則是在專案右鍵壓縮成zip/7zip就好


0606

下載jQuery 方式
右鍵-在新分頁開啟連結/在右鍵-另存新檔


https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords


模糊搜尋再sql要 單引號,在jpa 不用,直接%%包起來

hibernate 開始就沒用""

一開始jdbc 模糊搜尋是用原生sql所以有加單引號

https://youmightnotneedjquery.com/

上課用Bootstrap v5.2.3


下載jQuery 方式
右鍵-在新分頁開啟連結/在右鍵-另存新檔


55頁
一個E是 周二
四個E是星期二
不加EEEE 就是單純的年月日時分秒

顯示所有留言


論壇就是把上面改成文章頁面,下面放留言


request-> controller->model->controller->view

response->view->controller->model->controller->view

三層架構

  1. 表現層(Presentation Layer)
    controller
  2. 業務邏輯層(Business Layer)
    service
  3. 資料存取層(Data Access Layer)
    dao

model 跟view 不會直接去觸碰


分頁的方法
Pageable

在controller那邊有page物件傳過來

如果是在本頁就是 false

就只印字串給他

一個是拿page物件做判斷

一個是拿剛剛的page.number ,就是getNumber()

page.number就是getNumber()

pageNumber是頁數/for each 迴圈的頁碼

物件.屬性就是getter拿到屬性的值


th:field表示可在表單上編輯的屬性


0607

在開啟狀態中,重新啟動Server

要按紅色正方形 綠色箭頭的按鈕

sweetalert

點擊事件綁定按鈕和form表單

那上面怎呼叫


圖片可以存資料庫或是C槽本地端

存在資料庫不用擔心本地端路徑還有圖片名稱會撞到

雲端存aws s3 /azure blob storage ,但是要錢

用spring boot沒有建成table的原因
model沒有在最高層
或是沒有打entity

圖片實務上比較少再做編輯功能,但要給人新增和刪除,舊的圖片可以保留在相簿

圖片獨立出來做UD

專題結構

以各自功能先做分類

Repositry是啥

dao和model

編碼種類

  • 在HTML中,enctype 是 form 標籤的一個屬性,表示編碼類型。它定義了用戶端(即瀏覽器)如何將表單數據發送到服務器。

  • enctype="multipart/form-data" 這個值是在你需要在表單中包含檔案上傳時使用的。當你需要將表單中的二進制數據或非ASCII字符數據發送到服務器時,也應該使用這種編碼類型。

  • 基本上,當你在表單中有一個 type="file" 的 input 標籤時,你就應該使用 enctype="multipart/form-data"。

其他的 enctype 屬性值還包括:

  • application/x-www-form-urlencoded:這是預設的編碼類型。如果 form 標籤中沒有設定 enctype 屬性,那麼這種編碼方式會被使用。它適合那些不包含二進制數據的表單。

  • text/plain: 這種編碼類型用於將表單數據作為純文本(plain text)發送。使用這種編碼類型,表單數據不會被編碼。這種編碼類型很少使用,並且很少有實際應用場景。

https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.web


設定檔有使用到MultipartFile 相關的設定,就要使用這個資料型態

一個http request 只會有一個content type

所以一個頁面需要一個以上controller,上傳圖片了話

一個頁面 html / 一個圖片的 controller

專題不要抓全部的方法,要分頁(pageable)

因為實際上內容不會都在同一頁

只會讓每一頁顯示一定的資料筆數


密碼加密和解密

https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/crypto.html

資料庫密碼要加密

<dependency>
<groupId>org.springframework.security</groupId>
   <artifactId>spring-security-crypto</artifactId>
  </dependency>

要密碼控管時候直接autowired就好

https://docs.spring.io/spring-security/reference/features/authentication/password-storage.html

@Configuration +@bean
密碼控管的設定檔,可以寫在進入點的class
只是實際上會比較麻煩


Table建議在資料庫建

因為table 工作上比較複雜,自動建無法滿足需求


model 只能到下一頁, session是每一頁(存一個id)

不在session 就是在http response裡面

只要沒登出或是還沒到30分鐘就維持那個使用者的session

model就是response 回傳過去就消失了


HttpSession session 是原生的 /
@sessionAttribute 是spring 控管的

一樣功能


cookie 可以存4KB的任何東西 (內容)
SESSION只知道id 跟SERVER端連動
localStorage


cookie 可以存4KB 任何東西,可以存內容
cookie 跟localstorage做比較
localStorage 可以存4KB以上

session 只能存在瀏覽器裡面 , 只存id



設定環境變數的時候

java 8 才要加/bin

11/17 不用

只要寫 %JAVA_HOME% 就好


放置JSP的位置

  • 設定檔

  • maven 依賴

pom.xml 兩個依賴要新增


https://projectlombok.org/

lombok 的jar要放在跟sts.exe執行檔同個資料夾


下次ajax上課前

要複習DOM
innerHTML
getElementByID