實(shí)體類(lèi)
package com.example.quartz.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Data
public class SportUser {
@GeneratedValue
@Id
private Integer id;
private String account;
private String password;
private String avatar;
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
@JoinColumn(name = "sportUser_id")
private List<Step> steps = new ArrayList<>();
public SportUser() {
}
public SportUser(String account, String password, String avatar) {
this.account = account;
this.password = password;
this.avatar = avatar;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
}
package com.example.quartz.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
@Entity
@Data
public class Step {
@GeneratedValue
@Id
private Integer id;
private String account;
private Date date;
private String number;
public Step() {
}
public Step(String account, Date date, String number) {
this.account = account;
this.date = date;
this.number = number;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
dao層
package com.example.quartz.dao;
import com.example.quartz.entity.SportUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface LoginRepository extends JpaRepository<SportUser,Integer> {
@Query("from SportUser u where u.account = :account")
SportUser findUser(@Param("account") String account);
}
package com.example.quartz.dao;
import com.example.quartz.entity.Step;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface StepRepository extends JpaRepository<Step,Integer> {
@Query("FROM Step a WHERE DATEDIFF(a.date,NOW())=0 ORDER BY a.number DESC")
List<Step> find();
@Query("from Step u WHERE DATEDIFF(u.date,NOW())=0 and u.account = :account")
Step findUser(@Param("account") String account);
}
config
package com.example.quartz.config;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class QuartzConfig {
@Autowired
private SpringJobFactory springJobFactory;
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(springJobFactory);
return schedulerFactoryBean;
}
@Bean
public Scheduler scheduler() {
return schedulerFactoryBean().getScheduler();
}
}
package com.example.quartz.config;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@Component
public class SpringJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
service
package com.example.quartz.service;
import com.example.quartz.entity.SportUser;
import com.example.quartz.entity.Step;
import com.example.quartz.entity.SportUser;
import com.example.quartz.entity.Step;
import java.util.List;
public interface LoginService {
List<SportUser> get(String account, String password);
SportUser check(String account, String password);
List<Step> getAll();
}
package com.example.quartz.service;
import com.example.quartz.dao.LoginRepository;
import com.example.quartz.dao.StepRepository;
import com.example.quartz.entity.SportUser;
import com.example.quartz.entity.Step;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class LoginServiceImpl implements LoginService {
@Resource
private LoginRepository loginRepository;
@Resource
private StepRepository stepRepository;
@Override
public List<SportUser> get(String account, String password) {
return loginRepository.findAll();
}
@Override
public SportUser check(String account, String password) {
List<SportUser> users = loginRepository.findAll();
SportUser user = new SportUser();
for (int i=0;i<users.size();i++){
String name = users.get(i).getAccount();
String pass = users.get(i).getPassword();
if (name.equals(account)){
if (pass.equals(password)){
user = users.get(i);
}
}
}
return user;
}
@Override
public List<Step> getAll() {
return stepRepository.findAll();
}
}
package com.example.quartz.service;
import com.example.quartz.dao.StepRepository;
import com.example.quartz.entity.Step;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
@Slf4j
@Component
public class SportJob {
@Resource
private StepRepository stepRepository;
@Scheduled(cron = "40 14 2 * * ?")
public void execute() throws Exception{
Date date = new Date();
Step[] steps = {
new Step("111",date,"4673"),
new Step("222",date,"1287"),
new Step("333",date,"3576"),
new Step("444",date,"1245"),
new Step("555",date,"7467"),
};
for (int i = 0 ; i < 5 ;i++){
stepRepository.save(steps[i]);
}
}
}
界面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<title>登錄界面</title>
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
<style>
.content{
background-color: rgb(61,55,67)
}
.navbar-n{
background-color: rgb(61,55,67)
}
.search{
margin-top: -25px;
margin-left: 950px;
font-size: 16px;
color: white
}
.content{
margin-top: -30px;
margin-left: 1050px;
font-size: 16px;
color: white
}
.user{
margin-top: -30px;
margin-left: 1150px;
width: 20px;
height: 20px
}
.login{
margin-top: -20px;
margin-left: 1177px;
font-size: 16px;
color: white
}
.wechat{
margin-top: -30px;
margin-left: 1300px;
width: 30px;
height: 30px
}
.weibo{
margin-top: -28px;
margin-left: 1340px;
width: 30px;
height: 30px
}
.twitter{
margin-top: -28px;
margin-left: 1380px;
}
.ins{
margin-top: -28px;
margin-left: 1420px;
}
.form{
height: 710px;
}
.panell{
margin-left: 560px;
margin-top: 150px;
width: 400px;
height: 342px;
background-color: rgb(88,76,96);
border-color: rgb(88,76,96)
}
.loginn{
font-size: 18px;
color: white;
}
.register{
margin-top: -36px;
margin-left: 50px;
font-size: 18px;
color: rgb(37,31,42)
}
.inputt{
height: 45px;
background-color: rgb(69,61,75);
border-color: rgb(69,61,75)
}
.input2{
color: white;
height: 45px;
background-color: rgb(69,61,75);
border-color: rgb(69,61,75);
width: 300px
}
.inputtt{
height: 45px;
background-color: rgb(69,61,75);
border-color: rgb(69,61,75);
}
.password{
height: 45px;
background-color: rgb(69,61,75);
border-color: rgb(69,61,75);
width: 308px;
color: white;
}
.foot{
background-color: rgb(221,221,221);
height: 90px
}
.way{
margin-left: 110px;
margin-top: 20px;
color: rgb(141,135,147);
font-size: 12px
}
.qq{
width: 18px;
height: 18px;
margin-left: 210px;
margin-top: -57px
}
.weibo1{
margin-top: -57px;
margin-left: 10px;
}
</style>
</head>
<body class="content">
<div>
<nav class="navbar navbar-default navbar-n">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img src="https://staticweb.keepcdn.com/showstatic/images/logo-16fc21eba7.png">
<p class="search">發(fā)現(xiàn)精選</p>
<p class="content">課程內(nèi)容</p>
<img src="img/user.png" class="user">
<p class="login">登錄/注冊(cè)</p>
<img src="img/wechat.png" class="wechat">
<img src="img/weibo.png" class="weibo">
<img src="img/twitter.png" class="twitter">
<img src="img/ins.png" class="ins">
</a>
</div>
</div>
<form action="/SportUser" method="post" id="login" class="form">
<div class="panel panel-default panell" >
<div class="panel-body">
<table>
<tr>
<td>
<p class="loginn">登錄</p>
<p class="register">注冊(cè)</p>
</td>
</tr>
<tr>
<td>
<div class="input-group" >
<span class="input-group-addon inputt" id="basic-addon1">+86</span>
<input type="text" name="account" class="form-control input2" placeholder="賬號(hào)" >
</div>
</td>
</tr>
<tr>
<td height="30px"></td>
</tr>
<tr>
<td>
<div class="input-group" >
<span class="input-group-addon inputtt" id="basic-addon2" >
<img src="img/password.png">
</span>
<input type="password" name="password" class="form-control password" placeholder="密碼" >
</div>
</td>
</tr>
<tr>
<td style="height: 30px"></td>
</tr>
<tr>
<td>
<button style="width: 350px" type="submit" id="button" class="btn btn-success">登錄</button>
</td>
</tr>
<tr>
<td style="height: 20px"></td>
</tr>
</table>
</div>
<div class="panel-footer foot">
<p class="way">其他登錄方式:</p>
<img src="img/qq.png" class="qq">
<img src="img/weibo.png" class="weibo1">
</div>
</div>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<title>主界面</title>
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
<style>
.avatar{
width: 70px;
height: 70px;
}
.navbar1{
background-color: rgb(61,55,67);
height: 65px;
border-color: rgb(2,2,2);
}
.content{
background-color: rgb(61,55,67);
}
.avatar1{
width: 50px;
height: 50px;
margin-left: 1250px;
margin-top: -42px;
}
.account{
margin-left: 1315px;
margin-top: -35px;
color: white;
font-size: 18px;
}
.number{
color: white;
font-size: 30px;
}
.num{
color: white;
font-size: 25px;
}
.step{
margin-left: 20px;
margin-top:20px;
font-size: 50px;
color:rgb(49,165,110);
}
.way{
color: white;
font-size: 22px;
}
.set{
padding-top: 20px;
background-color: rgb(88,79,96);
height: 380px
}
.thumbnail2{
background-color: rgb(88,79,96);
border-color: rgb(88,79,96);
}
.set2{
color: white;
font-size: 20px;
}
</style>
</head>
<body class="content" >
<div>
<nav class="navbar navbar-default navbar1" >
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img src="https://staticweb.keepcdn.com/showstatic/images/logo-16fc21eba7.png">
<img th:src="${sportUser.avatar}" class="img-circle avatar avatar1">
<p th:text="'' + ${sportUser.account}" class="account"></p>
</a>
</div>
</div>
</nav>
</div>
<div class="container">
<div class="row">
<div class="col-md-8">
<div style="padding-top: 50px">
<p class="number">
您今日的活動(dòng)量:
</p>
</div>
<p class="num">步數(shù):</p>
<p class="step" th:text="${userStep.number}"></p>
<p class="way">距離5.60公里</p>
<p class="way">熱量126千卡</p>
<p class="way">生命在于運(yùn)動(dòng)</p>
<P class="way">今日的步數(shù)還沒(méi)有達(dá)到10000步標(biāo)準(zhǔn)哦~繼續(xù)加油哦</P>
<p></p>
</div>
<div class="col-md-4" style="padding-top: 50px">
<div class="set">
<div class="thumbnail thumbnail2" >
<h1 style="color: white;" align="center">好友排行</h1>
<div class="row">
<div class="col-md-6">
<div style="margin-left: 20px">
<p class="set2">我的好友</p>
</div>
</div>
<div class="col-md-6">
<div style="margin-left: 20px">
<p class="set2">今日步數(shù)</p>
</div>
</div>
<div class="row">
<div th:each="step:${steps}">
<div style="margin-top: 30px">
<div class="col-md-6">
<div style="margin-left: 50px;">
<p style="color: rgb(221,221,221)" th:text="${step.account}"></p>
</div>
</div>
<div class="col-md-6">
<div style="margin-left: 50px">
<p style="color:rgb(49,165,110);" th:text="${step.number}"></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>