目錄
- 環(huán)境搭建
- 后端
2.1 數(shù)據(jù)庫(kù)設(shè)計(jì)
2.2 SpringBoot + Mybatis
2.3 SpringBoot+RestfulAPI - 前端
3.1 VueJS 2.0 + Webpack工程介紹
3.2 Admin-LTE介紹及使用
3.3 VueJS一些基礎(chǔ)知識(shí)
3.4 項(xiàng)目中用到的和VueJS的開(kāi)源組件
1 前言
Spring boot寫(xiě)REST服務(wù)芒澜,和Spring幾乎沒(méi)有什么區(qū)別该窗。
2 開(kāi)始
2.1 加入依賴(lài)及Plugin
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Spring Boot Test 依賴(lài) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
2.2 Main class [Application]
@SpringBootApplication(scanBasePackages="com.xxx.xxx")
// mapper 接口類(lèi)掃描包配置
@MapperScan("com.xxx.xxx.dao")
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args);
}
}
2.3 寫(xiě)一個(gè)controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class SummaryController {
@Autowired
private SummaryService summaryService;
@RequestMapping(value = "/summary")
public ResponseEntity<SummaryResult> getSummary(@RequestParam(value = "reportId") String reportId) {
SummaryResult results = (summaryService.getSummaryResult(reportId));
return new ResponseEntity<>(results, HttpStatus.OK);
}
}
這里值得注意的是
import org.springframework.http.ResponseEntity;
ResponseEntity: 代表了整個(gè)HTTP返回⌒胶矗可以指定Status Code状勤,headers谴仙,body央星。它也有很多構(gòu)造方法來(lái)使用啃洋。
2.4 Service層summaryService
import com.xxxx.xxxx.dao.SummaryMapper;
import com.xxxx.xxxx.dto.SummaryResult;
import com.xxxx.xxx.model.Summary;
import com.xxx.xxx.service.SummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DecimalFormat;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: professor
* Date: 2017/6/2
* Time: 下午1:09
*/
@Service
public class SummaryServiceImpl implements SummaryService {
@Autowired
private SummaryMapper summaryMapper;
@Override
public SummaryResult getSummaryResult(String reportid) {
DecimalFormat df = new DecimalFormat("#.##");
Integer total_cases = 0;
Integer case_passed = 0;
SummaryResult result = new SummaryResult();
List<Summary> summaryList = summaryMapper.selectByReportId(reportid);
for (Summary s : summaryList) {
total_cases += s.getCaseNum();
case_passed += s.getCasePassed();
}
result.setTotal_cases(total_cases);
result.setCase_passed(case_passed);
Float radio = (case_passed * 100.0f) / total_cases;
result.setPass_radio(df.format(radio));
return result;
}
}
就是使用summaryMapper.selectByReportId方法來(lái)從數(shù)據(jù)庫(kù)中獲取信息传货,并返回計(jì)算結(jié)果。
如此依賴(lài)一個(gè)REST服務(wù)就寫(xiě)好了宏娄。寫(xiě)過(guò)應(yīng)該是:
http://127.0.0.1:7171/api/summary?reportId=3](http://127.0.0.1:7171/api/summary?reportId=3)
{
"total_cases": 902,
"case_passed": 707,
"pass_radio": "78.38"
}
注意這里的7171是我在工程的application.properties文件里顯式指定的HTTP服務(wù)端口:
server.address=0.0.0.0
server.port=7171