在pom.xml追加druid定义
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
在application.properties配置连接参数
spring.datasource.username=SCOTT
spring.datasource.password=TIGER
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
编写Configuration配置组件
@Configuration
public class DruidDataSourceConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource druid(){
//DruidDataSource ds = new DruidDataSource();
DataSource ds =
DataSourceBuilder.create().type(DruidDataSource.class).build();
return ds;
}
}
/dept GET : 查询所有dept信息
/dept/1 GET : 查询id=1的DEPT信息
/dept POST : 添加dept信息
/dept/1 PUT : 修改id=1的dept信息
/dept/1 DELETE : 删除id=1的dept信息
规则变型:查询利用GET、增删改用POST
/dept GET : 查询所有dept信息
/dept/1 GET : 查询id=1的DEPT信息
/dept/add POST : 添加dept信息
/dept/put POST : 修改id=1的dept信息
/dept/remove POST : 删除id=1的dept信息
在pom.xml添加spring-boot-starter-web定义
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
编写DeptController,利用@RestController、@RequestMapping标记
@RestController//@Controller+@ResponseBody
public class DeptController {
@Autowired
private DeptDao deptDao;
@RequestMapping(value="/dept",method=RequestMethod.GET)
public List<Dept> loadAll(){
return deptDao.loadAll();
}
@RequestMapping(value="/dept/{id}",method=RequestMethod.GET)
public MyResult loadDept(@PathVariable("id")int no){
MyResult result = new MyResult();
Dept dept = deptDao.loadById(no);
if(dept == null){
result.setStatus(0);
result.setMsg("未找到符合条件记录");
}else{
result.setStatus(1);
result.setMsg("查询成功");
result.setData(dept);
}
return result;
}
}
定义共同的返回对象MyResult.java
public class MyResult implements Serializable{
private int status;//处理结果状态
private String msg;//提示信息
private Object data;//返回的数据
//省略了set和get方法
}
/dept/list.do-->DispatcherServlet-->HandlerMapping-->ListController-->DeptDao -->ModelAndView-->ViewResolver-->/list.jsp
编写ListController
@Controller
public class ListController {
@Autowired
private DeptDao deptDao;
@RequestMapping("/dept/list.do")
public ModelAndView list(){
List<Dept> list = deptDao.loadAll();
ModelAndView mav = new ModelAndView();
mav.setViewName("list");
mav.getModel().put("depts", list);
return mav;
}
}
在application.properties添加view参数配置
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
在pom.xml中追加jstl和tomcat-embed-jasper引擎
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
编写list.jsp,使用JSTL和EL表达式显示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>部门列表</h1>
<table>
<tr>
<td>编号</td>
<td>部门名</td>
<td>地址</td>
</tr>
<c:forEach items="${depts}" var="dept">
<tr>
<td>${dept.deptno}</td>
<td>${dept.dname}</td>
<td>${dept.loc}</td>
</tr>
</c:forEach>
</table>
</body>
模板技术:velocity、freemarker、thymeleaf等。
JSP--》转译成Servlet--》编译Servlet--》运行--》生成HTML响应输出
模板文件+模板表达式(提取模型数据)--》生成HTML响应输出
velocity技术: *.vm+VTL表达式
freemarker技术: *.ftl+FTL表达式
thymeleaf技术: *.html+TH表达式
/template/list.do-->DispatcherServlet-->HandlerMapping-->TemplateController-->DeptDao-->返回ModelAndView-->thymeleaf模板文件 (src\main\resources\templates)
在pom.xml追加thymeleaf定义
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
编写Controller(与JSP响应Controller相同)
@Controller
public class TemplateController {
@Autowired
private DeptDao deptDao;
@RequestMapping("/template/list.do")
public ModelAndView list(){
List<Dept> list = deptDao.loadAll();
ModelAndView mav = new ModelAndView();
mav.setViewName("list");
mav.getModel().put("depts", list);
return mav;
}
}
在src/main/resources/templates添加模板文件
注意:html模板文件,开始和结束标记必须匹配;给< html>元素添加xmlns:th="http://www.thymeleaf.org"定义。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<h1>部门列表(thymeleaf模板)</h1>
<table>
<tr>
<td>编号</td>
<td>名称</td>
<td>地址</td>
</tr>
<tr th:each="dept:${depts}">
<td th:text="${dept.deptno}"></td>
<td th:text="${dept.dname}"></td>
<td th:text="${dept.loc}"></td>
</tr>
</table>
</body>
</html>