1、entity
@Data
@Entity
public class Walks {
@Id
@GeneratedValue
private Integerid;
/**
* 行走的步数
*/
private IntegerwalkCount;
/**
* 创建的时间
*/
private DatecreateTime;
public Walks() {
}
public Walks(Integer walkCount, Date createTime) {
this.walkCount = walkCount;
this.createTime = createTime;
}
}
@Component
public class SporterJob {
@Resource
private WalksServicewalksService;
@Scheduled(cron ="0 00 17 * * ? ")
public void updateTodayWalks()throws Exception {
walksService.updateWalks();
}
}
@Data
@Entity
@GenericGenerator(name ="sporter-uuid", strategy ="uuid")
public class Sporter {
/**
* 自增的UUID
*/
@Id
@GeneratedValue(generator ="sporter-uuid")
@Column(length =32)
private StringsporterId;
/**
* 账号
*/
private Stringaccount;
/**
* 密码
*/
private Stringpassword;
/**
* 昵称
*/
private Stringnickname;
/**
* 头像
*/
private Stringavatar;
/**
* 一对多关系,另一张表的外键
*/
@OneToMany(fetch = FetchType.EAGER , cascade = CascadeType.REMOVE)
@JoinColumn(name ="sporter_id")
private ListwalksList =new ArrayList<>();
public Sporter() {
}
public Sporter(String account, String password, String nickname, String avatar) {
this.account = account;
this.password = password;
this.nickname = nickname;
this.avatar = avatar;
}
public Sporter(String account, String password, String nickname, String avatar, List walksList) {
this.account = account;
this.password = password;
this.nickname = nickname;
this.avatar = avatar;
this.walksList = walksList;
}
}
2、dao
public interface SporterRepositoryextends JpaRepository {
/**
* 根据账号密码来查找个人信息
* @param account
* @param password
* @return
*/
SporterfindByAccountAndPassword(String account,String password);
}
public interface WalksRepositoryextends JpaRepository {
}
3、config
@Configuration
public class QuartzConfig {
@Autowired
private SpringJobFactoryspringJobFactory;
@Bean
public SchedulerFactoryBeanschedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean =new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(springJobFactory);
return schedulerFactoryBean;
}
@Bean
public Schedulerscheduler() {
return schedulerFactoryBean().getScheduler();
}
}
@Component
public class SpringJobFactoryextends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactorycapableBeanFactory;
/**
* 创建一个Job实列
* @param bundle
* @return
* @throws Exception
*/
@Override
protected ObjectcreateJobInstance(TriggerFiredBundle bundle)throws Exception {
Object jobInstance =super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
4、service
public interface WalksService {
/**
* 更新步数表中的数据
*/
void updateWalks();
}
public interface SporterService {
/**
* 登陆
* @param account
* @param password
* @return
*/
Sporterlogin(String account, String password);
/**
* 查找所有用户的信息,以及该用户下的步数信息
* @return
*/
ListfindAll();
}
@Service
public class WalksServiceImplimplements WalksService {
@Resource
private WalksRepositorywalksRepository;
/**
* 更新数据
*/
@Override
public void updateWalks() {
List list =walksRepository.findAll();
Random random =new Random();
for (int i=0;i
Walks walks = list.get(i);
walks.setWalkCount(random.nextInt(20000)+20000);
walks.setCreateTime(new Date());
walksRepository.saveAndFlush(walks);
}
}
}
@Service
public class SporterServiceImplimplements SporterService {
@Resource
private SporterRepositorysporterRepository;
/**
* 登陆
* @param account
* @param password
* @return
*/
@Override
public Sporterlogin(String account, String password) {
return sporterRepository.findByAccountAndPassword(account,password);
}
/**
* 查找所有的用户
* @return
*/
@Override
public ListfindAll() {
return sporterRepository.findAll();
}
}
5、contorller
@Controller
@RequestMapping(value ="/sporter")
public class SporterController {
@Resource
private SporterServicesporterService;
/**
* 登陆
* @return
*/
@GetMapping()
public Stringtest(){
return "login";
}
/**
* 查看详情
* @param account
* @param password
* @param map
* @return
*/
@GetMapping(value ="/list")
public Stringlist(
@RequestParam("account") String account,
@RequestParam("password") String password,
ModelMap map){
map.addAttribute("person",sporterService.login(account,password));
map.addAttribute("list",sporterService.findAll());
return "list";
}
}
6、index
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<meta charset="UTF-8">
<title>Title
*{padding:0;margin:0;}/* 清除浮动 */
body {
font-family:"Microsoft YaHei";
background:#EBEBEB;
font-weight:300;
font-size:15px;
color:#333;
overflow:hidden;
}
a {text-decoration:none; color:#000;}
a:hover{color:#F87982;}/*home*/
#home {/*logint界面*/
padding-top:100px;
}
#login{
padding:20px 30px 30px;
width:300px;
background:#FFF;
margin:auto;
border-radius:3px;
box-shadow:0 3px 3px rgba(0, 0, 0, 0.3);
}
#login h3{
font-size:18px;
line-height:25px;
font-weight:300;
letter-spacing:3px;
margin-bottom:20px;
color:#C8C8C8;
text-align:center;}
#login label{
color:#C8C8C8;
display:block;
height:35px;
padding:0 10px;
font-size:12px;
line-height:35px;
background:#EBEBEB;
margin-bottom:10px;
position:relative;}
#login label input{
font:13px/20px "Microsoft YaHei";
background:none; height:20px;
border:none; margin:7px 0 0 10px;
width:245px;outline:none ;
letter-spacing:normal;
z-index:1; position:relative;
}
#login label span{
display:block;
height:35px;
color:#F30;
width:100px;
position:absolute;
top:0;
left:190px;
text-align:right;
padding:0 10px 0 0;
z-index:0;
display:none;
}
#login #button{
font-family:"Microsoft YaHei";
cursor:pointer;
width:300px;
height:35px;
background:#FE4E5B;
border:none;
font-size:14px;
line-height:30px;
letter-spacing:3px;
color:#FFF;
position:relative;
margin-top:10px;
-moz-transition:all 0.2s ease-in;
-webkit-transition:all 0.2s ease-in;
-o-transition:all 0.2s ease-in;
transition:all 0.2s ease-in;}
#login #button:hover{background:#F87982; color:#000;}
.avator{
display:block;
margin:0 auto 20px;
border-radius:50%;
}
<div id="home">
<form id="login" action="/sporter/list" method="get" class="current1">
<h3>用户登入
<img class="avator" src="img/1.png" width="96" height="96"/>
<label>账号<input type="text" name="account" style="width:215px;" />
<span>账号为空
<label>密码<input type="password" name="password" />
<span>密码为空
<input type="submit" id="button">
</form>
<div>
</body>
</html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<meta charset="UTF-8">
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css"/>
<title>SporterList
.top-nav {
height:60px;
}
.top-img {
height:36px;
}
.top-title {
font-size:20px;
color:black;
line-height:60px;
}
p {
margin:0;
padding:0;
}
.icon1 {
margin-right:20px;
line-height:60px;
}
.icon2 {
margin-right:20px;
line-height:60px;
}
.center-nav {
width:100%;
/*margin-top: 60px;*/
height:40px;
border-bottom:solid #E0E0E0 2px;
margin-top: -20px;
}
.center-nav .col-md-2 {
border:solid #E0E0E0 1px;
}
.center-container{
margin:0 180px;
height:40px;
}
.center-list{
text-align:center;
line-height:37px;
font-size:16px;
font-weight:bold;
/*border: solid #E0E0E0 1px;*/
}
.main{
margin-top:40px;
}
.main .center-img {
width:200px;
height:140px;
}
.article-div {
width:100%;
height:300px;
padding:20px;
margin-bottom:20px;
border:solid #E0E0E0 1px;
}
.article-div .bottom-info {
margin-top:20px;
}
.article-div .avatar-img{
width:40px;
height:40px;
border-radius:50%;
}
.bottom-info-right {
display:inline-block;
width:40px;
height:40px;
float:right;
padding-right:30px;
}
#person_avatar {
border-radius:50px;
width:40px;
height:40px;
}
<nav class="navbar navbar-default navbar-static-top">
<div class="container top-nav">
<div class="col-md-4">
<div class="col-md-4">
<p class="top-title text-center ">SporterList
<div class="col-md-4 text-right">
<a><span class="glyphicon glyphicon-pencil icon1">
<a><span class="glyphicon glyphicon-search icon2">
<a class="navbar-brand navbar-right" href="#">
<img id="person_avatar" th:src="@{${person.avatar}}">
<div class="container">
<div class="col-md-4">
<div class="col-md-4">
<div class="thumbnail" th:each="sport:${list}">
<img th:src="@{${sport.avatar}}" alt="...">
<div class="caption">
<h3 th:text="${sport.nickname}">
<div th:each="walk:${sport.walksList}">
<h4 th:text="${walk.walkCount}">
<div class="col-md-4">
</html>
7、运行截图