美文网首页Spring Boot
Spring Boot整合quartz学习1

Spring Boot整合quartz学习1

作者: 不知所措的STRANGER | 来源:发表于2018-10-21 13:56 被阅读0次

    1、om依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>time</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>time</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.10</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--thymeleaf模板引擎依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>bootstrap</artifactId>
                <version>3.3.7-1</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
                <scope>true</scope>
            </dependency>
    
            <dependency>
                <groupId>com.spring4all</groupId>
                <artifactId>swagger-spring-boot-starter</artifactId>
                <version>1.8.0.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>swagger-bootstrap-ui</artifactId>
                <version>1.8.4</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-quartz</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
                <!-- 这是spring boot devtool plugin -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <!-- 如果没有该项配置devtools不会起作用-->
                        <fork>true</fork>
                        <!--支持静态资源热部署-->
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
            </plugins>
    
        </build>
    
    
    </project>
    

    2、entity

    SportUser.class

    package com.example.demo.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.class

    package com.example.demo.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;
        }
    }
    

    3、dao层

    LoginRepository

    package com.example.demo.dao;
    
    import com.example.demo.entity.SportUser;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;
    
    public interface LoginRepository extends JpaRepository<SportUser,Integer> {
        @Query("from SportUser u where u.account = :account")
        SportUser findUser(@Param("account") String account);
    }
    

    StepRepository

    package com.example.demo.dao;
    
    import com.example.demo.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);
    
    }
    

    4、Service + Impl

    LoginService

    package com.example.demo.service;
    
    
    import com.example.demo.entity.SportUser;
    import com.example.demo.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();
    }
    

    LoginServiceImpl

    package com.example.demo.service;
    
    
    import com.example.demo.dao.LoginRepository;
    import com.example.demo.dao.StepRepository;
    import com.example.demo.entity.SportUser;
    import com.example.demo.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();
        }
    
    }
    

    SportJob

    package com.example.demo.service;
    
    import com.example.demo.dao.StepRepository;
    import com.example.demo.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 = "0 26 8 * * ?")
        public void execute() throws Exception{
            Date date = new Date();
            Step[] steps = {
                    new Step("10086",date,"8475"),
                    new Step("10011",date,"7847"),
                    new Step("10012",date,"6541"),
                    new Step("10013",date,"6489"),
                    new Step("10014",date,"6488"),
            };
            for (int i = 0 ; i < 5 ;i++){
                stepRepository.save(steps[i]);
            }
        }
    }
    

    5、Controller

    LoginController

    package com.example.demo.controller;
    
    
    import com.example.demo.dao.StepRepository;
    import com.example.demo.entity.SportUser;
    import com.example.demo.service.LoginService;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    
    
    @Controller
    @RequestMapping()
    public class LoginController {
        @Resource
        private LoginService loginService;
    
        @Resource
        private StepRepository stepRepository;
    
        @RequestMapping(value = "/SportUser")
        public String show(HttpServletRequest request, ModelMap modelMap) {
            String account = request.getParameter("account");
            String password = request.getParameter("password");
            SportUser sportUser = loginService.check(account,password);
            modelMap.addAttribute("sportUser",sportUser);
            modelMap.addAttribute("userStep",stepRepository.findUser(account));
            modelMap.addAttribute("steps",stepRepository.find());
            return "home";
        }
    
        @RequestMapping("/login")
        public String login(){
            return "login";
        }
    
    }
    

    6、config

    QuartzConfig

    package com.example.demo.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();
        }
    }
    

    SpringJobFactory

    package com.example.demo.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;
        }
    }
    

    7、templates

    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>
            html,body{
                width: 100%;
                height: 100%;
                background: url(img/bg11.png) bottom center no-repeat #efeff4 ;
                background-size: 100% 100%;
            }
            .avatar{
                width: 70px;
                height: 70px;
            }
            .tab{
                margin-top: 10px;
                background-color:WhiteSmoke;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h5 style="color: black">______________________________________________________________________________________________________________________________________________________</h5>
            <h6 style="font-size: 25px;color: white">
            欢迎访问绿色出行系统,希望每个人都能乐于步行,放下杂念,为世界贡献一抹绿色!
            </h6>
            <h5 style="color: black">______________________________________________________________________________________________________________________________________________________</h5>
    
            <div class="row">
    
                <div class="col-md-4"  style="padding-top: 50px">
                    <div>
                        <img th:src="${sportUser.avatar}" class="img-circle avatar">
                    </div>
                    <h1 th:text="${sportUser.account}" style="color: white"></h1>
    
                    <div style="padding-top: 50px">
                        <!--<div class="thumbnail" style="background:#5A9A4C ">-->
                        <div>
                            <h2 style="color: white">运动排行榜</h2>
                            <div class="row">
                                <div class="col-md-6">
                                    <div style="margin-left: 24px">
                                        <h4 style="color: white">账  号</h4>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div style="margin-left: 20px">
                                        <h4 style="color: white">步  数</h4>
                                    </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;font-size: 22px">
                                                    <p th:text="${step.account}"></p>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div style="margin-left: 50px;font-size: 22px">
                                                    <p th:text="${step.number}"></p>
                                                </div>
                                            </div>
                                        </div>
    
                                    </div>
                                </div>
                    </div>
    
                </div>
            </div>
        </div>
                <div class="col-md-1">
                </div>
                <div class="col-md-7">
                    <div class="row">
                        <div style="padding-top: 50px">
                            <h1 style="color: white">
                                今日运动良好,步数为:
                            </h1>
                        </div>
                        <p style="margin-right: 20px; margin-top:20px;font-size: 100px;color: gold" th:text="${userStep.number}"></p>
    
    
                        <div>
                            <h6 style="color: white">__________________________________________________________________________________________________________</h6>
                            <h3 style="color: white">
                                以下是根据调查得出的结论,你可以根据调查结果评析运动情况:
                            </h3>
                            <h3></h3>
                            <h3 style="color: #efeff4">
                                当你步行0-5000时,请注意健康情况了,懒惰使人肥胖!
                            </h3>
                            <h3></h3>
                            <h3 style="color: #efeff4">
                                当你步行5000-10000时,请注意,你已经完成日常锻炼!
                            </h3>
                            <h3></h3>
                            <h3 style="color: #efeff4">
                                当你步行>10000时,请注意,你已经超神了,感谢你为绿化做出的贡献!
                            </h3>
                            <h3></h3>
    
                        </div>
    
                    </div>
                </div>
        </div>
    </div>
    </body>
    </html>
    

    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>
            html,body{
                width: 100%;
                height: 100%;
                background: url(img/bg.jpg) bottom center no-repeat #efeff4 ;
                background-size: 100% 100%;
            }
            form#login {
                -webkit-border-radius: 5px;
                border-radius: 5px;
                width: 500px;
                margin: auto;
                padding-top: 300px;
            }
            form#login tr td{
                padding-top: 40px;
            }
    
            input#button{
                width:100px;
                height:40px;
                color: white;
                background-color: #99AE6B;
                /*border: 1px solid black;*/
                border: 1px solid #99AE6B;
                text-decoration: none;
            }
        </style>
    </head>
    <body>
    <div>
        <form action="/SportUser" method="post" id="login">
            <table align="center">
                <tr>
                    <td></td>
                    <td style="font-size: 35px;color: orange">请绿色出行!</td>
                </tr>
                <tr>
                    <td style="font-size: 24px;color: white">请输入账号:</td>
                    <td><input type="text" name="account" ></td>
                </tr>
                <tr>
                    <td style="font-size: 24px;color: white">请输入密码:</td>
                    <td><input type="password" name="password" ></td>
                </tr>
                <tr>
                    <td>    </td>
                    <td ><input type="submit" id="button" value="登 录" style="font-size: 24px"></td>
                </tr>
            </table>
        </form>
    </div>
    </body>
    </html>
    

    8、application.properties

    server.port = 8080
    spring.thymeleaf.cache=false
    
    #在application.properties文件中配置mysql连接配置信息
    spring.datasource.url=jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    ##连接池中最大的活跃连接数
    spring.datasource.tomcat.max-active=20  
    ##连接池中最大、最小的空闲连接数
    spring.datasoure.max-idle=8
    spring.datasoure.min-idle=8
    ##初始化连接数
    spring.datasoure.initial=10
    
    #在application.properties文件中配置JPA配置信息
    spring.jpa.database=mysql
    # 显示SQL语句
    spring.jpa.show-sql=true
    ##指定DDL mode (none, validate, update, create, create-drop)
    spring.jpa.properties.hibernate.hbm2ddl.auto=update
    

    相关文章

      网友评论

        本文标题:Spring Boot整合quartz学习1

        本文链接:https://www.haomeiwen.com/subject/irdbzftx.html