pom依赖
<?xml version="1.0"encoding="UTF-8"?>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.example
time
0.0.1-SNAPSHOT
jar
time
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
<!-- lookup parent from repository -->
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
1.16.10
org.springframework.boot
spring-boot-starter-web
<!--thymeleaf模板引擎依赖-->
org.springframework.boot
spring-boot-starter-thymeleaf
org.webjars
bootstrap
3.3.7-1
org.springframework.boot
spring-boot-devtools
true
true
com.spring4all
swagger-spring-boot-starter
1.8.0.RELEASE
com.github.xiaoymin
swagger-bootstrap-ui
1.8.4
org.springframework.boot
spring-boot-starter-quartz
org.springframework.boot
spring-boot-maven-plugin
<!-- 这是spring boot devtool plugin -->
org.springframework.boot
spring-boot-maven-plugin
<!-- 如果没有该项配置devtools不会起作用-->
true
<!--支持静态资源热部署-->
true
entity
SportUser.class
package com.example.demo.entity;
importlombok.Data;
importjavax.persistence.*;
importjava.util.ArrayList;
importjava.util.List;
@Entity
@Data
publicclassSportUser {
@GeneratedValue
@Id
privateInteger id;
privateStringaccount;
privateStringpassword;
privateStringavatar;
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
@JoinColumn(name ="sportUser_id")
privateList steps =newArrayList<>();
publicSportUser() {
}
publicSportUser(Stringaccount,Stringpassword,Stringavatar) {
this.account = account;
this.password = password;
this.avatar = avatar;
}
publicStringgetAccount() {
returnaccount;
}
publicvoidsetAccount(Stringaccount) {
this.account = account;
}
publicStringgetPassword() {
returnpassword;
}
publicvoidsetPassword(Stringpassword) {
this.password = password;
}
publicStringgetAvatar() {
returnavatar;
}
publicvoidsetAvatar(Stringavatar) {
this.avatar = avatar;
}
}
Step.class
package com.example.demo.entity;
importlombok.Data;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.Id;
importjava.util.Date;
@Entity
@Data
publicclassStep {
@GeneratedValue
@Id
privateInteger id;
privateStringaccount;
privateDatedate;
privateStringnumber;
publicStep() {
}
publicStep(Stringaccount,Datedate,Stringnumber) {
this.account = account;
this.date = date;
this.number =number;
}
publicInteger getId() {
returnid;
}
publicvoidsetId(Integer id) {
this.id = id;
}
publicStringgetAccount() {
returnaccount;
}
publicvoidsetAccount(Stringaccount) {
this.account = account;
}
publicDategetDate() {
returndate;
}
publicvoidsetDate(Datedate) {
this.date = date;
}
publicStringgetNumber() {
returnnumber;
}
publicvoidsetNumber(Stringnumber) {
this.number =number;
}
}
dao层
LoginRepository
packagecom.example.demo.dao;
importcom.example.demo.entity.SportUser;
importorg.springframework.data.jpa.repository.JpaRepository;
importorg.springframework.data.jpa.repository.Query;
importorg.springframework.data.repository.query.Param;
publicinterfaceLoginRepositoryextendsJpaRepository{
@Query("from SportUser u where u.account = :account")
SportUser findUser(@Param("account")String account);
}
StepRepository
packagecom.example.demo.dao;
importcom.example.demo.entity.Step;
importorg.springframework.data.jpa.repository.JpaRepository;
importorg.springframework.data.jpa.repository.Query;
importorg.springframework.data.repository.query.Param;
importjava.util.List;
publicinterfaceStepRepositoryextendsJpaRepository{
@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);
}
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 getAll();
}
LoginServiceImpl
package com.example.demo.service;
importcom.example.demo.dao.LoginRepository;
importcom.example.demo.dao.StepRepository;
importcom.example.demo.entity.SportUser;
importcom.example.demo.entity.Step;
importorg.springframework.stereotype.Service;
importjavax.annotation.Resource;
importjava.util.List;
@Service
publicclassLoginServiceImplimplementsLoginService{
@Resource
private LoginRepository loginRepository;
@Resource
private StepRepository stepRepository;
@Override
publicListget(Stringaccount,Stringpassword) {
returnloginRepository.findAll();
}
@Override
public SportUser check(Stringaccount,Stringpassword) {
List users = loginRepository.findAll();
SportUser user =newSportUser();
for(inti=0;i
Stringname = users.get(i).getAccount();
Stringpass = users.get(i).getPassword();
if(name.equals(account)){
if(pass.equals(password)){
user = users.get(i);
}
}
}
returnuser;
}
@Override
publicList getAll() {
returnstepRepository.findAll();
}
}
SportJob
packagecom.example.demo.service;
importcom.example.demo.dao.StepRepository;
importcom.example.demo.entity.Step;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.scheduling.annotation.Scheduled;
importorg.springframework.stereotype.Component;
importjavax.annotation.Resource;
importjava.util.Date;
@Slf4j
@Component
publicclassSportJob {
@Resource
privateStepRepository stepRepository;
@Scheduled(cron ="0 26 8 * * ?")
publicvoidexecute()throwsException{
Date date =newDate();
Step[] steps = {
newStep("10086",date,"8475"),
newStep("10011",date,"7847"),
newStep("10012",date,"6541"),
newStep("10013",date,"6489"),
newStep("10014",date,"6488"),
};
for(inti =0; i <5;i++){
stepRepository.save(steps[i]);
}
}
}
Controller
LoginController
packagecom.example.demo.controller;
importcom.example.demo.dao.StepRepository;
importcom.example.demo.entity.SportUser;
importcom.example.demo.service.LoginService;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.ModelMap;
importorg.springframework.web.bind.annotation.RequestMapping;
importjavax.annotation.Resource;
importjavax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping()
publicclassLoginController{
@Resource
privateLoginService loginService;
@Resource
privateStepRepository stepRepository;
@RequestMapping(value = "/SportUser")
publicString 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")
publicString login(){
return"login";
}
}
config
QuartzConfig
packagecom.example.demo.config;
importorg.quartz.Scheduler;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
publicclassQuartzConfig{
@Autowired
privateSpringJobFactory springJobFactory;
@Bean
publicSchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(springJobFactory);
returnschedulerFactoryBean;
}
@Bean
publicScheduler scheduler() {
returnschedulerFactoryBean().getScheduler();
}
}
SpringJobFactory
packagecom.example.demo.config;
importorg.quartz.spi.TriggerFiredBundle;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.config.AutowireCapableBeanFactory;
importorg.springframework.scheduling.quartz.AdaptableJobFactory;
importorg.springframework.stereotype.Component;
@Component
publicclassSpringJobFactoryextendsAdaptableJobFactory{
@Autowired
privateAutowireCapableBeanFactorycapableBeanFactory;
@Override
protectedObjectcreateJobInstance(TriggerFiredBundlebundle)throwsException{
ObjectjobInstance =super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
returnjobInstance;
}
}
templates
home.html
<!DOCTYPE html>
主界面
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;
}
______________________________________________________________________________________________________________________________________________________
欢迎访问绿色出行系统,希望每个人都能乐于步行,放下杂念,为世界贡献一抹绿色!
______________________________________________________________________________________________________________________________________________________
<!--<div class="thumbnail" style="background:#5A9A4C ">-->
运动排行榜
账 号
步 数
今日运动良好,步数为:
__________________________________________________________________________________________________________
以下是根据调查得出的结论,你可以根据调查结果评析运动情况:
当你步行0-5000时,请注意健康情况了,懒惰使人肥胖!
当你步行5000-10000时,请注意,你已经完成日常锻炼!
当你步行>10000时,请注意,你已经超神了,感谢你为绿化做出的贡献!
login.html
<!DOCTYPE html>
登录界面
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:1pxsolid#99AE6B;
text-decoration: none;
}
请绿色出行!
请输入账号:
请输入密码:
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
网友评论