本文章來自【知識林】
項目文件結(jié)構(gòu)
新建的Springboot項目的文件結(jié)構(gòu)如下:
|-customer(項目名稱)
| - src
| | - main
| | | - java
| | | - resources
| | | | - static
| | | | - public
| | - test
| | | - java
| - pom.xml
| - customer.iml
customer:是項目名稱;
src/main/java:目錄下放置所有java文件(源代碼文件);
src/main/resources:放置所有的配置文件、頁面文件、靜態(tài)資源文件;
src/main/resources/static:是靜態(tài)資源文件目錄,在這個目錄中的所有文件將可以被直接訪問,如果沒有這個文件夾可自行創(chuàng)建;
src/main/resources/public:作用和src/main/resources/static目錄一樣。
配置文件
Springboot把使用Spring來開發(fā)Web項目的很多配置進行了統(tǒng)一管理,且都配置了默認值。很多默認值是基本不用修改的,但也有部份配置是不能滿足實際需求的,所以需要修改這些配置。
Springboot默認支持兩種配置文件類型:.properties和.yml
比如將默認的8080端口修改為9090,則可以配置為:
application.properties :
server.port = 9090
application.yml :
server:
port: 9090
注意:Springboot會自動在src/main/resources/目錄下找application.properties或application.yml配置文件,找到后將應用此配置文件中的配置,否則使用其默認值。這兩種類型的配置文件有其一即可,也可兩者并存。
.properties配置文件的優(yōu)先級更高,將在application.properties中配置了server.port=9090同時也在application.yml中配置了server: port: 9091時,系統(tǒng)將使用.properties中的9090端口。
常用配置
server.port=9090 # 服務端口號
server.tomcat.uri-encoding=UTF-8 #以Tomcat為web容器時的字符編碼
spring.application.name=customer # 應用名稱,一般就是項目名稱,這個名稱在SpringCloud中比較關鍵
spring.profiles.active=dev #指定當前的活動配置文件,主要用于多環(huán)境多配置文件的應用中
spring.http.encoding.charset=UTF-8 #http請求的字符編碼
spring.http.multipart.max-file-size=10MB #設置文件上傳時單個文件的大小限制
spring.http.multipart.max-request-size=100MB #設置文件上傳時總文件大小限制
spring.thymeleaf.prefix=classpath:/templates/ #配置在使用Thymeleaf做頁面模板時的前綴,即頁面所在路徑
spring.thymeleaf.suffix=.html #設置在使用Thymeleaf做頁面模板時的后綴
spring.thymeleaf.cache=false #設置在使用Thymeleaf做頁面模板時是否啟用緩存
spring.mvc.static-path-pattern=/** #設置靜態(tài)資源的請求路徑
spring.resources.static-locations=classpath:/static/,classpath:/public/ #指定靜態(tài)資源的路徑
##以下是使用MySQL數(shù)據(jù)庫的配置
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect #指定數(shù)據(jù)庫方言
hibernate.show_sql=true #是否顯示sql語句
hibernate.hbm2dll.auto=update #設置使用Hibernate的自動建表方式
entitymanager.packagesToScan=com.zslin #設置自動掃描的包前綴
spring.datasource.url=jdbc:mysql://localhost:3306/customer?\
useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true #數(shù)據(jù)庫鏈接
spring.datasource.username=root #數(shù)據(jù)庫用戶名
spring.datasource.password=123 #數(shù)據(jù)庫用戶對應的密碼
spring.datasource.driver-class-name=com.mysql.jdbc.Driver #數(shù)據(jù)庫驅(qū)動名稱
hibernate.hbm2dll.auto有幾種配置:
create:每次加載Hibernate時都會刪除上一次生成的表,然后重新生成新表,即使兩次沒有任何修改也會這樣執(zhí)行,這就導致每次啟動都是一個新的數(shù)據(jù)庫,也是導致數(shù)據(jù)丟失的重要原因。create-drop:每次加載Hibernate時都會生成表,但當SessionFactory關閉時,所生成的表將自動刪除。update:最常用的屬性值,第一次加載Hibernate時創(chuàng)建數(shù)據(jù)表(前提是需要先有數(shù)據(jù)庫),以后加載HIbernate時只會根據(jù)model更新,即使model已經(jīng)刪除了某些屬性,數(shù)據(jù)表也不會隨之刪除字段。validate:每次加載Hibernate時都會驗證數(shù)據(jù)表結(jié)構(gòu),只會和已經(jīng)存在的數(shù)據(jù)表進行比較,根據(jù)model修改表結(jié)構(gòu),但不會創(chuàng)建新表。
以上是我在使用中比較常用的配置信息!
本文章來自【知識林】