Spring Boot + Quartz綜合練習(xí)

實(shí)體類(lèi)

  • SportUser.java
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;
    }
}
  • Step.java
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]);
        }
    }
}

界面

  • login.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>
        .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>
  • home.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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市泡态,隨后出現(xiàn)的幾起案子榜跌,更是在濱河造成了極大的恐慌戒财,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件械巡,死亡現(xiàn)場(chǎng)離奇詭異冀偶,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)供置,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)谨湘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)绽快,“玉大人芥丧,你說(shuō)我怎么就攤上這事》话眨” “怎么了续担?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)活孩。 經(jīng)常有香客問(wèn)我物遇,道長(zhǎng),這世上最難降的妖魔是什么憾儒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任询兴,我火速辦了婚禮,結(jié)果婚禮上起趾,老公的妹妹穿的比我還像新娘诗舰。我一直安慰自己,他們只是感情好训裆,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布眶根。 她就那樣靜靜地躺著,像睡著了一般边琉。 火紅的嫁衣襯著肌膚如雪属百。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,144評(píng)論 1 285
  • 那天变姨,我揣著相機(jī)與錄音族扰,去河邊找鬼。 笑死定欧,一個(gè)胖子當(dāng)著我的面吹牛别伏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播忧额,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼厘肮,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了睦番?” 一聲冷哼從身側(cè)響起类茂,我...
    開(kāi)封第一講書(shū)人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎托嚣,沒(méi)想到半個(gè)月后巩检,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡示启,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年兢哭,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片夫嗓。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡迟螺,死狀恐怖冲秽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情矩父,我是刑警寧澤锉桑,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站窍株,受9級(jí)特大地震影響民轴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜球订,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一后裸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧冒滩,春花似錦轻抱、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至士八,卻和暖如春容燕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背婚度。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工蘸秘, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蝗茁。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓醋虏,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親哮翘。 傳聞我的和親對(duì)象是個(gè)殘疾皇子颈嚼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)饭寺,斷路器阻课,智...
    卡卡羅2017閱讀 134,601評(píng)論 18 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,522評(píng)論 25 707
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料艰匙? 從這篇文章中你...
    hw1212閱讀 12,693評(píng)論 2 59
  • 所謂“楗”限煞,即閉塞的開(kāi)關(guān),即鎖员凝。內(nèi)署驻,指內(nèi)心世界。內(nèi)楗,字面意思是從內(nèi)心深處鎖住旺上。本篇意為通過(guò)適當(dāng)?shù)挠握f(shuō)探知君主的內(nèi)...
    Mr_Hz閱讀 757評(píng)論 0 0
  • BL萌芽源于《萌芽》瓶蚂。 讀書(shū)的時(shí)候喜歡買(mǎi)雜志,那時(shí)候我還是個(gè)初中生抚官。有一期描述了一段感情扬跋,用現(xiàn)在的話講就是一個(gè)寫(xiě)作...
    敬千帆閱讀 168評(píng)論 1 0