1.用官網位址https://start.spring.io/ 快速搭建項目
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL9MGRNpXUE9EeNRVT3V1MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2YzNyUzNyQTMxADOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
選擇需要的包
生成原始項目代碼,解壓導入IDE
2.配置
package com.horse.red.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/sayHello")
public String sayHello(){
System.out.println("hello spring boot");
return "hello spring boot";
}
}
在啟動網站前,由于spring boot 是預設自動注冊加載資料庫相關的類檔案的,是以為了不報錯,我們需要打開資料庫并在resource目錄下的application.property中加入資料庫配置相關檔案,這裡以mysql為例子,配置檔案如下:
application.properties中配置:
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
3.運作項目:
http://localhost:8080/sayHello
4.傳參數
/* url傳參,通路的路徑類似這樣:localhost:8080/getParamDemo1/1
* 方法體中的參數要在前面加注釋,@PathVariable,代表url中的參數
*/
@RequestMapping(path = {"/getParamDemo1/{id}"})
public String getParamDemo1(@PathVariable("id") int userId){
System.out.println("get param " + userId);
return "success get param";
}
http://localhost:8080/getParamDemo1/1
/**
* 當然,你也可以通過這種傳參方式:localhost:8080/getParamDemo?param1=1或者直接表單送出參數
* 當然,同時方法中參數聲明的注釋也要變成@RequestParam,代表請求參數,required屬性說明了參數是否是必須的
*/
@RequestMapping(path = {"/getParamDemo2"})
public String getParamDemo2 (@RequestParam(value="param1",required = false) int param){
System.out.println("get param " + param);
return "success get param";
}
http://localhost:8080/getParamDemo2?param1=1
5.建構restful程式設計風格
利用上面說的url中的值(類似這個:path="/member/{userid}")進行資源定位,也非常符合resultful的風格要求,例如這path="/getParamDemo1/{userid}"的配置就是對應的就是對會員級别的某個使用者(由userid定位)進行某些操作,如果要删除該使用者,則對應http請求的delete請求即可。
@RequestMapping(path = {"/getParamDemo2"},method = RequestMethod.GET)
public String getParamDemo2 (@RequestParam(value="param1",required = false) int param){
System.out.println("get param " + param);
return "success get param";
}
6.spring boot中配置資料庫架構(以mybatis為例子)
application.properties
#下面這條配置聲明了mybatis的配置檔案路徑,classpath對應的是和這個檔案統計的resources mybatis.config-location=classpath:mybatis-config.xml
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<!-- Continue going here -->
</configuration>
package com.horse.red.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface HelloDao {
@Select({"select name from user_info "})
List<String> queryDemo();
}
請求方法
@Autowired
private HelloDao helloDao;
@RequestMapping("/sayHello")
public String sayHello(){
System.out.println("hello spring boot");
List<String> names = helloDao.queryDemo();
System.out.println(names.toString());
return "hello spring boot";
}
如果出現報錯:
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
解決辦法:
url後面增加:
jdbc.url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
試了很多次這個方法都不管用後來直接修改了時區。
修改資料庫時區:
set global time_zone = '+8:00'; ##修改mysql全局時區為中原標準時間,即我們所在的東8區
set time_zone = '+8:00'; ##修改目前會話時區
flush privileges; #立即生效
7.springboot跳轉html頁面
首先在pom檔案中引入模闆引擎jar包,即:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
以及web支援
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在application.properties中配置模闆引擎
spring.thymeleaf.prefix=classpath:/templates/
是讓controller層到templates檔案夾尋找xx.html(src/main/resources/templates)
在templates下建立index.html檔案
controller層
package com.horse.red.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/test")
public String test(){
return "index";
}
}
在浏覽器中位址欄輸入:http://localhost:8080/test