springboot项目的整体结构
package com.lz.palmscore.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Activity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
private String name;//活动名
private String startTime;//开始日期
·
·
·
}
为了获取表单数据转换为对象的形式,同时注解的进行限制。比如不能为空···
package com.lz.palmscore.form;
import org.springframework.beans.factory.annotation.Value;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
public class ActivityForm {
@NotEmpty(message = "活动名必填")
private String name;//活动名
@NotNull(message = "参赛组数必填")
private Integer groupNum;
···
}
枚举类型 定义错误信息, 因为枚举允许用类型化的值替换常量并在任何地方使用这些类型。
使用时通过 ActivityEnum.ACTIVITY_ERROR.getCode()/.getMessage() 可获取错误码或者错误信息
(具体可参考下面controller里面的代码)
package com.lz.palmscore.enums;
public enum ActivityEnum {
PASSWORD_NULL(205,"评委打分密码必填"),
````
;
private Integer code;
private String message;
ActivityEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
定义activity的异常类,继承RuntimeException'
通常使用方法为:throw new AcitvityException(ActivityEnum.ACTIVITY_ERROR);
(具体可参考下面controller里面的代码)
package com.lz.palmscore.exception;
import com.lz.palmscore.enums.ActivityEnum;
public class AcitvityException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
//调用定义的ActivityEnum 中的错误信息
public AcitvityException(ActivityEnum activityEnum) {
super(activityEnum.getMessage());
this.code =activityEnum.getCode();
}
//自定义code,以及错误信息
public AcitvityException(Integer code,String msg){
super(msg);
this.code = code;
}
}
return ResultVOUtil.success(); return ResultVOUtil.success(activityList); return ResultVOUtil.error(222,"删除失败");
(具体可参考下面controller里面的代码)
package com.lz.palmscore.util;
import com.lz.palmscore.vo.ResultVO;
public class ResultVOUtil {
//成功,有返回值
public static ResultVO success(Object object){
ResultVO resultVO = new ResultVO();
resultVO.setCode(0);
resultVO.setMsg("成功");
resultVO.setData(object);
return resultVO;
}
//成功 没有返回值
public static ResultVO success(){
return success(null);
}
//错误
public static ResultVO error(Integer code,String msg){
ResultVO resultVO = new ResultVO();
resultVO.setCode(code);
resultVO.setMsg(msg);
return resultVO;
}
}
Repository居于业务层和数据层之间,将两者隔离开来,在它的内部封装了数据查询和存储的逻辑。
这样设计的好处有两个:
降低层级之间的耦合:更换、升级ORM引擎(Hibernate)并不会影响业务逻辑
提高测试效率:如果在测试时能用Mock数据对象代替实际的数据库操作,运行速度会快很多
package com.lz.palmscore.repository;
import com.lz.palmscore.entity.Activity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ActivityRepository extends JpaRepository {
}
具体的业务处理
package com.lz.palmscore.service;
import com.lz.palmscore.entity.Activity;
import com.lz.palmscore.entity.Player;
import com.lz.palmscore.entity.Rater;
import com.lz.palmscore.entity.ScoreItem;
public interface ActivityService{
Activity add(Activity ac);
List findAll();
boolean alldelete(Integer id);
}
package com.lz.palmscore.service.impl;
import com.lz.palmscore.entity.*;
import com.lz.palmscore.repository.*;
import com.lz.palmscore.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class ActivityServiceImpl implements ActivityService {
@Autowired
private ActivityRepository activityRepository;
/**
* 添加activity
* @return
*/
@Override
public Activity add(Activity ac) {
Activity activity=activityRepository.save(ac);
return activity;
}
/**
* 查询所有activity
* @return
*/
@Override
public List findAll() {
return activityRepository.findAll();
}
}
是控制器,所以它是负责业务调度的,所以在这一层应写一些业务的调度代码,
方法定义成ResultVO类型···· 慢慢看代码··不想说了··
有用到之前定义的form表单,
通过@vaild注入,通知需要注入BindingResult结合验证·········
package com.lz.palmscore.controller;
import com.lz.palmscore.Conventer.ActivityForm2ActivityConventer;
···
@RestController
@RequestMapping("/admin")
@Slf4j
public class ActivityController {
@Autowired
ActivityService activityService;
@Autowired
PeopleService peopleService;
/**
* 活动添加 仅添加
*/
@PostMapping("activity_add")
public ResultVO ActivityAdd(@Valid ActivityForm activityForm,
BindingResult bindingResult,
HttpSession session){
if (bindingResult.hasErrors()) {
log.error("[添加活动]格式错误");
return ResultVOUtil.error(ActivityEnum.ACTIVITY_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
}
Activity activity = ActivityForm2ActivityConventer.conventer(activityForm);
if (activity == null) {
log.error("[活动]创建失败");
throw new AcitvityException(ActivityEnum.ACTIVITY_ERROR);
}
Integer id= (Integer) session.getAttribute("activityId");
activity.setId(id);
session.setAttribute("activity",activity);
return ResultVOUtil.success();
}
/**
* 显示所有活动
* @return
*/
@GetMapping("activity_show")
public ResultVO show() {
List activityList = activityService.findAll();
return ResultVOUtil.success(activityList);
}
}
}
然后页面信息放到: