微信运动作业文档
2018-10-20 本文已影响0人
不知所措的STRANGER
1、在pom.xml中添加依赖,从SpringBoot2.0之后只需一个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2、在java目录下创建一个包,在里面写两个配置类
/**
* @author lq
* 自定义的可配置的JobFactory
*/
@Component
public class SpringJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
/**
* 创建一个Job实列
* @param bundle
* @return
* @throws Exception
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
/**
* @author lq
* quartz的配置类
*/
@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();
}
}
3、创建一个job任务类
/**
* Created by lq on 2018/10/10.
* 自定义的Job任务类
*/
@Component
public class SporterJob {
@Resource
private WalksService walksService;
@Scheduled(cron = "0 17 08 * * ? ")
public void updateTodayWalks() throws Exception {
walksService.updateWalks();
}
}
4、创建两个持久化实体类
/**
* Created by lq on 2018/10/8.
* 用户的持久化类
*/
@Data
@Entity
@GenericGenerator(name = "sporter-uuid", strategy = "uuid")
public class Sporter {
/**
* 自增的UUID
*/
@Id
@GeneratedValue(generator = "sporter-uuid")
@Column(length = 32)
private String sporterId;
/**
* 账号
*/
private String account;
/**
* 密码
*/
private String password;
/**
* 昵称
*/
private String nickname;
/**
* 头像
*/
private String avatar;
/**
* 一对多关系,另一张表的外键
*/
@OneToMany(fetch = FetchType.EAGER , cascade = CascadeType.REMOVE)
@JoinColumn(name = "sporter_id")
private List<Walks> walksList = 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<Walks> walksList) {
this.account = account;
this.password = password;
this.nickname = nickname;
this.avatar = avatar;
this.walksList = walksList;
}
}
/**
* Created by lq on 2018/10/8.
* 步数类
*/
@Data
@Entity
public class Walks {
@Id
@GeneratedValue
private Integer id;
/**
* 行走的步数
*/
private Integer walkCount;
/**
* 创建的时间
*/
private Date createTime;
public Walks() {
}
public Walks(Integer walkCount, Date createTime) {
this.walkCount = walkCount;
this.createTime = createTime;
}
}
5、创建两个实体类的对应的Repository接口
/**
* Created by lq on 2018/10/8.
* 用户的Repository接口
*/
public interface SporterRepository extends JpaRepository<Sporter,String> {
/**
* 根据账号密码来查找个人信息
* @param account
* @param password
* @return
*/
Sporter findByAccountAndPassword(String account,String password);
}
/**
* Created by lq on 2018/10/8.
* 步数的Repository接口
*
*/
public interface WalksRepository extends JpaRepository<Walks,Integer> {
}
6、两个持久化类的接口及实现类
/**
* Created by lq on 2018/10/8.
* 用户的service接口
*/
public interface SporterService {
/**
* 登陆
* @param account
* @param password
* @return
*/
Sporter login(String account,String password);
/**
* 查找所有用户的信息,以及该用户下的步数信息
* @return
*/
List<Sporter> findAll();
}
/**
* Created by lq on 2018/10/8.
* 用户service接口的实现类
*/
@Service
public class SporterServiceImpl implements SporterService {
@Resource
private SporterRepository sporterRepository;
/**
* 登陆
* @param account
* @param password
* @return
*/
@Override
public Sporter login(String account, String password) {
return sporterRepository.findByAccountAndPassword(account,password);
}
/**
* 查找所有的用户
* @return
*/
@Override
public List<Sporter> findAll() {
return sporterRepository.findAll();
}
}
/**
* Created by lq on 2018/10/10.
* 步数的service接口
*/
public interface WalksService {
/**
* 更新步数表中的数据
*/
void updateWalks();
}
/**
* Created by lq on 2018/10/10.
* 步数的service接口的实现类
*/
@Service
public class WalksServiceImpl implements WalksService {
@Resource
private WalksRepository walksRepository;
/**
* 更新数据
*/
@Override
public void updateWalks() {
List<Walks> list = walksRepository.findAll();
Random random = new Random();
for (int i=0;i<list.size();i++){
Walks walks = list.get(i);
walks.setWalkCount(random.nextInt(20000)+20000);
walks.setCreateTime(new Date());
walksRepository.saveAndFlush(walks);
}
}
}
7、controller层
/**
* Created by lq on 2018/10/9.
* 用户的controller层
*/
@Controller
@RequestMapping(value = "/sporter")
public class SporterController {
@Resource
private SporterService sporterService;
/**
* 登陆
* @return
*/
@GetMapping()
public String test(){
return "login";
}
/**
* 查看详情
* @param account
* @param password
* @param map
* @return
*/
@GetMapping(value = "/list")
public String list(
@RequestParam("account") String account,
@RequestParam("password") String password,
ModelMap map){
map.addAttribute("person",sporterService.login(account,password));
map.addAttribute("list",sporterService.findAll());
return "list";
}
}
8、login.html
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<!--<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css"/>-->
<!--<link type="text/css" rel="stylesheet" href="login.css"/>-->
<title>Title</title>
<style>
*{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%;
}
</style>
</head>
<body>
<div id="home">
<form id="login" action="/sporter/list" method="get" class="current1">
<h3>用户登入</h3>
<img class="avator" src="img/avatar.png" width="96" height="96"/>
<label>账号<input type="text" name="account" style="width:215px;" />
<span>账号为空</span></label>
<label>密码<input type="password" name="password" />
<span>密码为空</span></label>
<input type="submit" id="button"></input>
</form>
</div>
</body>
</html>
9、list.html
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css"/>
<title>SporterList</title>
<style>
.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;
}
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-static-top">
<div class="container top-nav">
<div class="col-md-4">
</div>
<div class="col-md-4">
<p class="top-title text-center ">SporterList</p>
</div>
<div class="col-md-4 text-right">
<a><span class="glyphicon glyphicon-pencil icon1"></span></a>
<a><span class="glyphicon glyphicon-search icon2"></span></a>
<a class="navbar-brand navbar-right" href="#">
<img id="person_avatar" th:src="@{${person.avatar}}">
</a>
</div>
</div>
</nav>
<div class="container">
<div class="col-md-4">
</div>
<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}"></h3>
<div th:each="walk:${sport.walksList}">
<h4 th:text="${walk.walkCount}"></h4>
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
</body>
</html>