使用定时任务中ScheduledExecutorService实现步数统计
- 添加完整poml依赖
- 创建两个类,sport和walker
sport
@Entity
@Data
public class Sport {
@Id
@GeneratedValue
private Integer id;
private String account;
private Integer stepNumbers;
public Sport(String account, Integer stepNumbers) {
this.account = account;
this.stepNumbers = stepNumbers;
}
public Sport() {
}
}
walker
@Data
public class Walker {
@Id
@GeneratedValue
private Integer id;
private String account;
private String password;
private String nickname;
private String avatar;
public Walker(String account, String password, String nickname, String avatar) {
this.account = account;
this.password = password;
this.nickname = nickname;
this.avatar = avatar;
}
public Walker() {
}
}
- 添加repository类
sportrepository
public interface SportRepository extends JpaRepository<Sport, Integer> {
@Query(" from Sport order by stepNumbers desc ")
List<Sport> getAll();
Sport findSportByAccount(String account);
}
walkersportrepository
public interface WalkRepository extends JpaRepository<Walker, Integer> {
Walker findByAccountAndPassword(String account, String password);
Walker findByAccount(String account);
}
- 添加定时任务job
@Component
public class Jobs {
@Resource
private SportRepository sportRepository;
public static List<Sport> rankingList = null;
@Scheduled(cron = "0 48 21 * * ?")
public void ranking() {
rankingList = sportRepository.getAll();
System.out.println(rankingList);
}
public List<Sport> rank() {
return rankingList;
}
}
- 添加controller类
@Controller
@RequestMapping(value = "/user")
public class WalkController {
@Resource
private WalkRepository walkRepository;
@Resource
private SportRepository sportRepository;
private Walker walker = new Walker();
private Sport sport = new Sport();
private Jobs jobs = new Jobs();
@RequestMapping("/login")
public String login(HttpServletRequest request, HttpSession session) {
String username = request.getParameter("username");
String password = request.getParameter("password");
walker = walkRepository.findByAccountAndPassword(username, password);
sport = sportRepository.findSportByAccount(username);
String str = "";
if (walker != null) {
session.setAttribute("userLogin", walker);
session.setAttribute("steps", sport);
session.setAttribute("ranking", jobs.rank());
str = "home";
} else {
str = "index";
}
return str;
}
}
- 最后添加页面
home.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>主页</title>
<script type="text/javascript"
th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
<link
th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}"
rel="stylesheet"/>
</head>
<body>
<div >
<img th:src="${session.userLogin.avatar}" width="200px" height="200px" style="float: left"/>
</div>
<div>
<h3 th:text="${session.userLogin.nickname}"></h3>
<div>
步数:<p th:text="${session.steps.stepNumbers}"></p>
</div>
</div>
<div style="width: 300px">
<table class="table table-hover table-condensed">
<legend>
<strong>每日步数排行榜</strong>
</legend>
<thead>
<tr>
<th>账号</th>
<th>步数</th>
</tr>
</thead>
<tbody>
<tr th:each="sport : ${session.ranking}">
<td th:text="${sport.account}"></td>
<td th:text="${sport.stepNumbers}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" type="text/css" href="Login.css"/>
<style>
html {
width: 100%;
height: 100%;
overflow: hidden;
font-style: italic;
}
body {
width: 100%;
height: 100%;
font-family: 'Open Sans', sans-serif;
margin: 0;
background-color: salmon;
}
#login {
position: absolute;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
width: 300px;
height: 300px;
}
#login h1 {
color: #fff;
text-shadow: 0 0 10px;
letter-spacing: 1px;
text-align: center;
}
h1 {
font-size: 2em;
margin: 0.67em 0;
}
input {
width: 278px;
height: 18px;
margin-bottom: 10px;
outline: none;
padding: 10px;
font-size: 13px;
color: #fff;
text-shadow: 1px 1px 1px;
border-top: 1px solid #312E3D;
border-left: 1px solid #312E3D;
border-right: 1px solid #312E3D;
border-bottom: 1px solid #56536A;
border-radius: 4px;
background-color: #2D2D3F;
}
.but {
width: 300px;
min-height: 20px;
display: block;
background-color: #4a77d4;
border: 1px solid #3762bc;
color: #fff;
padding: 9px 14px;
font-size: 15px;
line-height: normal;
border-radius: 5px;
margin: 0;
}
</style>
</head>
<body style="text-align: center">
<p id="login">
<h1>登录界面</h1>
<form action="/user/login" method="post">
<input type="text" required="required" placeholder="用户名" name="username"/><br>
<input type="password" required="required" placeholder="密码" name="password"/>
<button class="but" type="submit" style="margin-left: 618px">登录</button>
</form>
</p>
</body>
</html>
网友评论