SpringBoot框架搭建
- 初始化搭建
① file->new->project->spring Initializr
②next-> (修改name爲自己想要的名字name)
- (我之前有個模就直接創建了,沒有模型的)
Web 選個 web
TemPlateEngines 選個 thymeleaf
Sql 選個 mysql jdbc mybatis
- 更改maven設置
- File-setting-maven
Maven-home-diretory 設置成本地maven
Using-setting-file:設置成本地的setting.xml
- File-project-structure
設置jdk 1.8(自適應)
- 將模板中的(build 和dependies)複製進去 pom.xml 中 ojbdc可能有問題,找個ojdbc6放在lib目錄下,引入(這種打包的時候可能有問題,打包的項目可能獲取不到OJDBC,需要maven命令直接導入本地倉庫jar包)
依賴選擇自動導入 auto-import
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc</artifactId> <version>6</version> <scope>system</scope> <systemPath>${project.basedir}/lib/ojdbc6.jar</systemPath> </dependency>
- 在application.yml (如果是.propertie後綴可以改爲yml後綴)中加入數據庫連接,我的是mysql 然後啓動項目,如果成功啓動,則搭建成功。
server: port: 8088 spring: datasource: name: testssm url: jdbc:mysql://127.0.0.1:3306/testssm?serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver
- 文件建立及存放
也沒啥好說的其實,搭建完之後按自己的喜好建立文件就行了。
- 創建接口及相關文件
主要說下Controller,一般都是前後端分離的,Controller接口用來返回數據,也可以返回頁面。
- 返回頁面的話
Controller中就是 @Controller
頁面是放在resouces->templates下的,我這裏返回的頁面是index.html文件。
這裏的數據可以在index中用${}取值。
- 返回數據用@restController
4、測試訪問及效果
文中我的是 http://localhost:8088/hello/springboot2
(有時候訪問不到是因爲template沒有被編譯到target下,多拿小錘子敲一下或者重啓一下。)
返回數據的就需要寫接口一般有
Controller(java)層、service(interface)層、serviceImpl(java)層、mapper(sevice)層、xml(xml)層、domain層(java)層。