Spring Boot 实践:MyBatis

generatorConfig.xml 

  • 自动生成之外,也可同时手动创建,通过mapper2.xml中的namespace来指定mapper2对象即可。
  • VirtualPrimaryKeyPlugin
  • LombokPlugin
  • generatedKey


 

  
  
  
    
    
    
    
         
         
    
    
              
              
     
    
    
 
    
    
      
    
     
    
      
      
    
     
    
      
    
     
    
      
    
    
    
        
    

Controller:

  • ResponseEntity>
  • @Valid及内部对象
  • HttpStatus
  • Api-Version
    @PostMapping("/createPayment")
    public ResponseEntity> createPayment(@Valid @RequestBody PaymentInDTO dto, BindingResult bindingResult){
    	log.info("data:{}", dto);
    	if(bindingResult.hasErrors()) { 
    		Map err = MiscUtil.getValidateError(bindingResult);
            return new ResponseEntity>(err, HttpStatus.UNPROCESSABLE_ENTITY);
        }
    	
        PaymentOutDTO dtoOut = paymentService.createPayment(dto);
        Map result = new HashMap();        
        result.put("data", dtoOut);        
        return new ResponseEntity>(result, HttpStatus.OK);
    }
    
    @PostMapping(value = "/createPayment", headers = "Api-Version=2")
    public ResponseEntity> createPaymentV2(@Valid @RequestBody PaymentInDTO dto, BindingResult bindingResult){
    	log.info("data:{}", dto);
    	
        Map result = new HashMap();        
        result.put("data", "just test");        
        return new ResponseEntity>(result, HttpStatus.OK);
    }

ServiceImpl:

  • InDTO
  • OutDTO
  • Transactional
@Override
@Transactional(rollbackFor={RuntimeException.class, Exception.class})
public PaymentOutDTO createPayment(PaymentInDTO dto) {
    	Date createAt = new Date(System.currentTimeMillis());

    	Payment payment = new Payment();
    	BeanUtils.copyProperties(dto, payment);    	
    	payment.setCode(MiscUtil.getUUID());
    	payment.setStatus(StatusEnum.STATUS_NEW.getCode());
    	payment.setCreatedAt(createAt);
    	payment.setLogoUrl("https://aaa.com/payments/aaa.png");//TODO
    	payment.setPaymentUrl("https://aaa.com/payments/"+payment.getCode());//TODO
    	paymentMapper.insertSelective(payment);

    	PaymentOutDTO dtoOut = new PaymentOutDTO();
    	BeanUtils.copyProperties(payment, dtoOut);
    	dtoOut.setStatus(StatusEnum.STATUS_NEW.getName());
    	
    	dto.getPrice().forEach(price -> {
    		PaymentPricing paymentPricing = new PaymentPricing();
    		BeanUtils.copyProperties(price, paymentPricing);
    		paymentPricing.setPaymentId(payment.getId()); 
    		paymentPricingMapper.insertSelective(paymentPricing);
    	});
    	dtoOut.setPrice(dto.getPrice());
    	
    	PaymentTimeline paymentTimeline = new PaymentTimeline();
    	paymentTimeline.setPaymentId(payment.getId());
    	paymentTimeline.setStatus(StatusEnum.STATUS_NEW.getCode());
    	paymentTimeline.setTime(createAt);
    	paymentTimelineMapper.insertSelective(paymentTimeline);
    	
    	PaymentTimelineOutDTO paymentTimelineOutDTO = new PaymentTimelineOutDTO();
    	BeanUtils.copyProperties(paymentTimeline, paymentTimelineOutDTO);
    	paymentTimelineOutDTO.setStatus(StatusEnum.STATUS_NEW.getName());
    	
    	List timelineList = new ArrayList();
    	timelineList.add(paymentTimelineOutDTO);
    	dtoOut.setTimeline(timelineList);
    	
    	dtoOut.setConfirmations(0);
    	
	return dtoOut;
}

全局异常处理:

@ControllerAdvice
public class CommonExceptionHandler {

    /**
     *  拦截Exception类的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity> exceptionHandler(Exception e){
        Map result = new HashMap();
        result.put("message", "内部错误");
        result.put("description", e.getMessage());
        return new ResponseEntity>(result, HttpStatus.INTERNAL_SERVER_ERROR); 
    }
}

 

你可能感兴趣的:(Spring,Boot)