更改操作及代码
- Dao 层:
@Update("update tb_activity set title=#{title},category=#{category},startTime=#{startTime},endTime=#{endTime},"
+ "remark=#{remark} where id=#{id}")
int updateActivity(Activity entity);
@Select("select * from tb_activity where id=#{id}")
Activity doFindById(Long id);
- Service 层:
void updateActivity(Activity entity);
Activity findById(Long id);
- 实现类:
@Override
public void updateActivity(Activity entity) {
log.info("start {}",System.currentTimeMillis());
activityDao.updateActivity(entity);
log.info("end {}",System.currentTimeMillis());
}
@Override
public Activity doFindById(Long id) {
log.info("start {}",System.currentTimeMillis());
Activity act = activityDao.doFindById(id);
if(act==null) throw new NoSuchElementException("对象可能已经不存在");
log.info("end {}",System.currentTimeMillis());
return act;
}
- Controller 层:
@RequestMapping("doFindById")
public String doFindById(Long id,Model model) {
Activity act = activityService.doFindById(id);
model.addAttribute("act",act);
return "activity_edit";
}
@RequestMapping("doSaveActivity")
public String doSaveActivity(Activity entity) {
if(entity.getId()==null) {
activityService.saveActivity(entity);
}else {
activityService.updateActivity(entity);
}
return "redirect:activity_list";
}
@RequestMapping("activity_edit")
public String doActivityEditUI(Model model) {
model.addAttribute("act",new Activity());
return "activity_edit";
}
- acticity_list 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>The Activity Page</h1>
<button onclick="javascript:location.href='activity_edit'">创建活动</button>
<div>
<table>
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>category</th>
<th>startTime</th>
<th>endTime</th>
<th>state</th>
<th>operation</th>
</tr>
</thead>
<tbody>
<tr th:each="a:${list}">
<td th:text="${a.id}"></td>
<td th:text="${a.title}"></td>
<td th:text="${a.category}"></td>
<td th:text="${#temporals.format(a.startTime, 'yyyy/MM/dd HH:mm:ss')}"></td>
<td th:text="${#temporals.format(a.endTime, 'yyyy/MM/dd HH:mm:ss')}"></td>
<td th:text="${a.state==1?'进行中':'已结束'}"></td>
<td><a th:href="@{/activity/doFindById(id=${a.id})}">update</a></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
- acticity_edit 页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
ul li {list-style-type: none}
</style>
</head>
<body>
<h1>The activity Edit Page</h1>
<form action="doSaveActivity" method="post">
<ul>
<li><input type="hidden" name="id" th:value="${act.id}">
<li>标题
<li><input type="text" name="title" th:value="${act.title}">
<li>类型
<li><input type="text" name="category" th:value="${act.category}">
<li>开始时间
<li><input type="text" name="startTime" th:value="${#temporals.format(act.startTime, 'yyyy/MM/dd HH:mm:ss')}">
<li>结束时间
<li><input type="text" name="endTime" th:value="${#temporals.format(act.endTime, 'yyyy/MM/dd HH:mm:ss')}">
<li>备注
<li><textarea rows="5" cols="30" name="remark" th:text="${act.remark}"></textarea><!-- 如果不用th:text可以直接在标签中间写[[${act.remark}]] -->
<li><input type="submit" value="save">
</ul>
</form>
</body>
</html>
2. Spring Boot 应用加强实现
2.1 健康检查配置及测试
- 在项目中添加健康检查依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
健康检查分析:在浏览器中输入如下地址:
http://localhost/actuator/health
假如希望查看更多actuator选项,可以在Spring Boot中配置文件 application.properties 中添加如下语句:
management.endpoints.web.exposure.include=*
此时在浏览器地址栏可以输入:http://localhost/actuator/beans 查看所有的 Spring 容器中的 bean 信息。
说明:当需要以一种更好的结构化方式查看 bean 相关信息,可以对 Google 浏览器安装 JsonView 插件或者使用 Postman 等工具进行资源请求然后查询bean信息,还可以直接在 STS 工具的 Boot Dashboard 中选中项目,查看其属性(show properties).
2.2 热部署配置及实现
基于 Spring Boot 的 Web 项目,修改了某个类以后,默认不会自动重新部署和加载,需要我们手动重启服务器。假如我们希望项目可以自动部署,可以添加如下依赖,进行热部署实现。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
说明:当我们修改了 src/main/java 目录下的 java 文件或修改了 src/main/resources 目录下的配置文件时,默认都会重启你的 Web 服务器,但是修改了测试类或 html 文件不会自动重启和部署。