spring boot可以轻松创建可以运行的,独立的,生产机的基于spring的应用程序,大多数spirng boot应用程序需要很少的spring配置。
pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot-jsp</groupId>
<artifactId>springboot-jsp</artifactId>
<name>springboot-jsp :: 整合 JSP 案例</name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<properties>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
</properties>
<dependencies>
<!-- Spring Boot Freemarker 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Tomcat 支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- JSTl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
</project>
application.yml
# 数据源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
# FreeMarker 配置
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
# MyBatis 配置
mybatis:
mapper-locations:
- classpath:mapper/*.xml
# MyBatis 日志
logging:
level:
org.spring.springboot.dao: debug
在/src/main下新建目录webapp/WEB-INF/jsp
Controller
package org.spring.springboot.controller;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* 城市 Controller 实现 Restful HTTP 服务
*
* @author colg
*/
@Controller
public class CityController {
@Autowired
private CityService cityService;
@GetMapping("/api/city/{id}")
public String findOneCity(Model model, @PathVariable("id") Long id) {
model.addAttribute("city", cityService.findCityById(id));
return "city";
}
@GetMapping("/api/city")
public String findAllCity(Model model) {
model.addAttribute("cityList", cityService.findAllCity());
return "cityList";
}
}
运行
http://localhost:8080/api/city/1