SpringBoot+MyBatis(2)
业务分层
1、dao层
dao层主要做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,dao层的设计首先是设计dao层的接口,然后就可以在模块中调用此接口来进行数据业务的处理。与数据库打交道,放在所有的逻辑之后,处理与数据库的CRUD相关的操作。
2、service层
service层主要负责业务模块的应用逻辑应用设计。service层的业务实现,具体要调用已经定义的dao层接口,封装service层业务逻辑有利于通用的业务逻辑的独立性和重复利用性。程序显得非常简洁。
3、controller层
controller层,controller层会调用前面两层,controller层一般会和前台的页面进行数据的交互, controller层是前台数据的接收器,后台处理好的数据也是通过controller层传递到前台显示的。,在此层要调用service层和Dao的接口来控制业务流程。
注解说明
@RestController:是@Controller和@ResponseBody的结合体,表示可以可以接收HTTP请求,并且@ResponseBody这个注解会让所有返回的对象转成json格式,不会返回视图
@RequestMapping:是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
@Autowired:将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去
@GetMapping:是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get 映射到 特定的处理方法上。
@RequestBody :Spring 会自动将 http request body 中的json字符串反序列化为java对象,接受的是json,所以在用postman的时候添加此注解报错
@PathVariable: URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
@RequestParam:可以对传入参数指定参数名,可以通过required=false或者true来要求@RequestParam配置的前端参数是否一定要传
@GetMapping("/search")
public XunzhenVoWrapper getXunzhenByCondition(
@RequestParam(required = false) Long chargeId,//定义参数,并规定是否可为空
@RequestParam(required = false) Long conditions,
@RequestParam(required = false) Long startTime,
@RequestParam(required = false) Long endTime,
@RequestParam(required = false) String ppm1,
@RequestParam(required = false) String ppm2,
@RequestParam(required = false) String queryStr,
@RequestParam Integer limit,
@RequestParam Integer offset
){
String startStr = getDateStr(startTime, false);
String endStr = getDateStr(endTime, true);
List<XunzhenVo> xunzhenVos=xunzhenVoDao.queryXunzhenListStep(//查询熏蒸事件,并将返回结果封装为一个List
startStr,
endStr,
chargeId,
conditions,
queryStr,
ppm1,
ppm2,
limit,
offset
);
Long total = xunzhenVoDao.queryCountStep(//查询熏蒸事件的总计结果
startStr,
endStr,
chargeId,
conditions,
ppm1,
ppm2,
queryStr,
limit,
offset
);
List<XunzhenVo> xunzhenVoList=
xunzhenVos.stream().map( s->{//java8新语法特性,对字符串进行操作
Long id=s.getSegmentId();
Tuple3<Float, Float,Float> tuple = aggregateService.getTempAndHumAndPpmDataBySegmentId(id);//元组来封装温度湿度和ppm浓度,方便的实现函数的多返回值
XunzhenVo xunzhenVo=new XunzhenVo();
BeanUtils.copyProperties(s, xunzhenVo);
xunzhenVo.setHumidity(tuple.first);
xunzhenVo.setTemperature(tuple.second);
xunzhenVo.setHzsppm(tuple.third);
// xunzhenVo.setCreate(s.getStarttime().getTime());
// xunzhenVo.setUpdate(s.getUpdatetime().getTime());
return xunzhenVo;
}).collect(Collectors.toList());
XunzhenVoWrapper res =new XunzhenVoWrapper();
res.setTableData(xunzhenVoList);
res.setTotal(total);
return res;
}
定时任务
@Scheduled:可以作为一个触发源添加到一个方法中,其使用cron表达式(从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份)
@EnableScheduling:开启计划任务支持,只有在主函数添加这个注解,定时器才可以使用
每分钟执行一次,先获取所有熏蒸事件id,再根据当前时间和id对熏蒸时间的结束时间进行比对,更新任务状态
@Scheduled(cron ="0 * * * * *" )
public void RealtimeUpdateConditon(){
Long time= System.currentTimeMillis();
List<Xunzhen> RealIdList=getRealtimeId();
for(int i = 0 ; i < RealIdList.size() ; i++) {
//RealIdList.get(i).getId()
if (time>=RealIdList.get(i).getEnd_time().getTime()){
if (RealIdList.get(i).getConditions()!=2) {
xunzhenVoDao.updatecondition(RealIdList.get(i).getId(),0);
}
}
}
}
public List<Xunzhen> getRealtimeId(){
List<Xunzhen> IdList= xunzhenVoDao.RealtimeXunzhenId();
return IdList;
}