Spring boot開發步驟 (包含jsp、mybatis、FastJSON整合)
阿新 • • 發佈:2018-12-12
- 專案建立好後 先匯入jar 包
/*
注意 這個不能放在dependencies內部
必須放在dependencies外部
*/
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
/* 這個是放在dependencies內部 */ <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 測試 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> <!--連線池物件--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.19</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- --------------------------------------------------------- --> <!--embed內嵌的tomcat提供jsp解析--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!--jstl標籤庫 用來在頁面上展示值的 --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--整合jsp--> </dependencies>
- 建立 SpringApplication 類
注意 這個類必建立在和dao service entity 同包下
@SpringBootApplication
@MapperScan("com.yangyang.dao")//建立代理物件 找到你的dao物件
public class APP {
public static void main(String[] args) {
SpringApplication.run(APP.class,args);
}
}
- @SpringBootApplication:
組合註解,由@
@ComponentScan(value = “****”):元件掃描 掃描Service、Controller註解。 預設掃描同級的所有包
@EnableAutoConfiguration:開啟自動配置
@Configuration 加在類上,宣告該類為配置類
@Bean 加在方法上把返回值交給工廠管理
- Controller
//@RestController
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private CounterService counterService;
@RequestMapping("finAll")
public String finAll(HttpServletRequest request){
List<Counter> counters = counterService.finAll();
request.setAttribute("user",counters)
return "index";//要跳轉的頁面
}
}
@RestController:這也是一個組合註解
由@[email protected] 組合
@Controller:當前類為控制器
@ResponseBody :當前返回值為json 底層採用 jackson來實現
有了 2、3之後就可以用網頁訪問了
注意
- 訪問的時候 springboot預設沒有專案名
- springboot 預設埠號為 8080
此處啟動有可能報錯 :
- 埠號衝突
原因: 可能本機裝了oracle資料庫,oracle 預設埠為8080 所以埠號衝突
解決方案:建立 Springboot 的配置檔案
- springboot配置檔案
application.yml
server:
context-path: /app # 專案名 網頁訪問用 注意:**屬性和值中間必須加空格
#建議和你的專案名保持一致
port: 9999 #埠號
jsp-servlet:
init-parameters:
development: true
#mybatis 相關配置 資料來源
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mage
type: com.alibaba.druid.pool.DruidDataSource #資料來源 阿里的資料來源
mvc:
view:
prefix: /
suffix: .jsp
#配置post請求的中文亂碼
http:
encpding:
charset: utf-8
force: true
#檔案上傳
multipart:
#單個檔案的最大值
max-file-size: 100MB
#一次請求的最大值
max-request-size: 1000MB #最好是 max-file-size的十倍
mybatis:
mapper-locations: com/yangyang/Mapper/*.xml #指定Mapper檔案的位置
type-aliases-package: com.yangyang.entity #起別名
配置檔案這裡,有一個配置檔案切分:
切分是把配置檔案拆分成一個主配置檔案和若干個小配置檔案,
小配置檔案大概分為:測試環境的配置檔案,和生產環境的配置檔案。
spring:
profiles:
#小配置檔名application-test.yml,這裡寫 tst
active:
- 寫DAO、Service
這裡就不過多贅述了,還是像springmvc那樣~
springboot在整合jsp後 頁面不支援 熱部署
在springboot 的設定專案名同級的地方 寫上
server:
context-path: /app
port: 9999
#寫在這 對 就寫在這
jsp-servlet:
init-parameters:
development: true
#jsp頁面中的取值
匯入jstl標籤庫
之後 在jsp頁面,下面以上述questAll方法為例
<%@page pageEncoding="utf-8" contentType="text/html;utf-8" isELIgnored="false"%>
<%@ prefic="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<c:forEach items="${requestScope.user}" var="user">
名字:${user.name}<br/>
密碼:${user.password}<br/>
</c:forEach>
</body>
</html>
Spring Boot 整合FastJSON
- 導jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15 </version>
</dependency>
- 將下面程式碼複製進 帶有@Configuration的類中
@Bean
public HttpMessageConverters fastjsonHttpMessageConverter(){
//定義一個轉換訊息的物件
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//新增fastjson的配置資訊 比如 :是否要格式化返回的json資料
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//在轉換器中新增配置資訊
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
如何以外掛的形式來啟動springboot
在pom.xml中 < build >標籤中插入
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
下載完成後