本文主要是介绍Transaction在Controller层的探索,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Transaction在Controller层的探索
一般开发中事务要求我们放在Service层,可是有些情况,我们可能会要求放在Controller层,你有没有碰到过这样的需求呢?那么放到Controller层事务会生效吗?会产生什么问题呢?下面一起来看看
I、透过现象看本质
第一种情况
Controller层代码如下
@RestController @RequestMapping("/city") public class CityControllerImpl implements CityController {@Autowiredprivate CityService cityService;@Override@RequestMapping(value = "getCity",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)@Transcationalpublic BaseResult<City> getCity(@RequestParam("id") Integer id) {City one = cityService.getOne(id);BaseResult<City> baseResult=new BaseResult<>();baseResult.setData(one);return baseResult;} }
运行结果
对的,你没有看错,当Transactional加载Controller层时出现404异常
第二种情况
Controller层代码如下
@RestController @RequestMapping("/city") public class CityControllerImpl {@Autowiredprivate CityService cityService;@RequestMapping(value = "getCity",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)@Transactionalpublic BaseResult<City> getCity(@RequestParam("id") Integer id) {City one = cityService.getOne(id);BaseResult<City> baseResult=new BaseResult<>();baseResult.setData(one);return baseResult;} }
这篇关于Transaction在Controller层的探索的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!