Controller专用的实体分ResultModel和ResponseEntity
1)Controller实体使用ResultModel(自定义)
1.1)Controller代码
@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")
public class SmsController {
Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private SmsService smsService;
@LogAnnotation(type = Constants.LOG_TYPE_SELECT, value = "查询短信发送历史")
@PostMapping("/messages")
@ApiOperation(value = "查询短信发送历史", notes = "查询短信发送历史")
public ResultModel smsList(@RequestBody SmsEntity smsEntity) {
return new ResultModel(ResultStatus.SUCCESS, smsService.selSmsByWhere(smsEntity));
}
}
1.1.1)ResultModel在异常时的使用
//枚举ResultStatus在这里使用
return new ResultModel(-1, "数据异常");
return new ResultModel(ResultStatus.USER_NOT_FOUND);
1.1.2)ResultModel在正常时的使用
return ResultModel(ResultStatus.SUCCESS, 实体);
1.1.3)枚举ResultStatus
//枚举ResultStatus在这里
public enum ResultStatus {
SUCCESS(100, "成功"),
USERNAME_OR_PASSWORD_ERROR(-1001, "用户名或密码错误"),
USER_NOT_FOUND(-1002, "用户不存在"),
USER_NOT_LOGIN(-1003, "用户未登录"),
DUPLICATED_NAME(-1010,"名称重复");
/**
* 返回码
*/
private int code;
/**
* 返回结果描述
*/
private String message;
ResultStatus(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
1.2) Service专用的实体也叫VO(将PO中的01翻译成男女,并分页),多个实体可以PageInfo分页
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
@Service
public class SmsService {
//@Value调取yml中的常数
@Value("${sms.url}")
private String url;
@Autowired
private SmsMapper smsMapper;
public PageInfo<SmsEntity> selSmsByWhere(SmsEntity entity) {
//if (StringUtils.isNotEmpty(entity.getPage()) && StringUtils.isNotEmpty(entity.getPageSize()) {
if (entity.getPage() != null && entity.getPageSize() != null) {
PageHelper.startPage(entity.getPage(), entity.getPageSize());
}
//借助实体里的一个电话,查出多个短信
PageInfo<SmsEntity> pageInfo = new PageInfo<>(smsMapper.selSmsByWhere(entity));
return pageInfo;
}
}
2.1)Service查询,返回VO分页实体给Controller;
2.2)Service做逻辑判断时,也会调用DAO中的PO和Controller中的DTO;
1.3) DAO专用的实体SmsEntity,也叫PO:(Entity实体,完全复制数据库中的字段)
public interface SmsMapper {
int insert(SmsEntity smsEntity);
/**
* 条件查询短信列表
* @param entity
* @return
*/
List<SmsEntity> selSmsByWhere(SmsEntity entity);
}
1)查询:DAO层不涉及分页,不涉及给返回的PO/Entity实体增加行数字段;
return com.github.pagehelper.PageInfo;
2)Controller实体使用ResponseEntity(spring下的包)
2.1)Controller代码
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
@RequestMapping("api/app_apply")
@RestController
@Api(tags = "应用申请")
public class AppApplyController {
@Autowired
private AppApplyService appApplyService;
//更新
@LogAnnotation(type = Constants.LOG_TYPE_UPDATE,value = "应用申请审批")
@ApiOperation(value = "应用申请审批")
@PutMapping("/approve")
public ResponseEntity approve (@RequestBody ApproveEntity approveEntity) {
try {
appApplyService.approve(approveEntity);
return new ResponseEntity(HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity(e.getCause(),HttpStatus.INTERNAL_SERVER_ERROR);
}
}
//查询
@LogAnnotation(type = Constants.LOG_TYPE_SELECT,value = "获取应用申请详情")
@ApiImplicitParams({@ApiImplicitParam(name = "id",value = "申请Id",paramType = "path",required = true)})
@ApiOperation(value = "获取申请详情")
@GetMapping("selectById/{id}")
public ResponseEntity selectById (@PathVariable("id") Long id) {
try {
return new ResponseEntity(appApplyService.selectById(id),HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity("查询异常",-1);
}
}
2.1.1)ResponseEntity在正常时的使用
//查询
return new ResponseEntity(实体,HttpStatus.OK);
//增删改
return new ResponseEntity(HttpStatus.OK);
2.1.2)ResponseEntity在异常时的使用
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
2.1.3)枚举HttpStatus
import org.springframework.http.HttpStatus;
2.2)Service多个实体可以PageInfo分页
@Service
public class AppApplyService {
Logger logger = LoggerFactory.getLogger(AppApplyService.class);
@Autowired
private AppApplyMapper appApplyMapper;
public AppApplyEntity selectById (Long id) {
Map map = new HashMap();
map.put("id",id);
AppApplyEntity appEntity= appApplyMapper.select(map);
return appEntity;
}
}
2.3)DAO专用的实体AppApplyEntity
@Component
public interface AppApplyMapper {
void insert (AppApplyEntity appApplyEntity);
void update (AppApplyEntity appApplyEntity);
void delete (long id);
AppApplyEntity select (Long id);
}
附1:Controller专用的实体,作用是格式的统一,Service作用是对数据的过滤