SpringMVC JPA 事务,数据库保存操作没有异常但数据保存不成功

  SpringMVC JPA 事务,数据库保存操作没有异常但数据保存不成功

 

   在主容器中(applicationContext.xml),将Controller的注解排除掉

 

<context:component-scan base-package="net.cloudun">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> 

 

   而在springMVC配置文件(xx-servlet-context.xml)中将Service注解给去掉

 

<context:component-scan base-package="net.cloudsun">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan> 

 

因 为spring的context是父子容器,所以会产生冲突,Controller会先进行扫描装配,而此时的Service还没有进行事务的 增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描配置进行事务处理。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些实现步骤,具体如下: 1.在Spring MVC的配置文件中添加文件上传的配置,包括文件上传的大小、文件保存的路径等配置,如下所示: ``` <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5242880"/> <property name="defaultEncoding" value="utf-8"/> <property name="resolveLazily" value="true"/> </bean> ``` 2.在前端页面中使用Layui的上传组件,代码如下所示: ``` layui.use('upload', function(){ var upload = layui.upload; //执行实例 var uploadInst = upload.render({ elem: '#test1', //绑定元素 url: '/upload/', //上传接口 accept: 'images', size: 5120, done: function(res){ //上传完毕回调 }, error: function(){ //请求异常回调 } }); }); ``` 3.在Spring MVC的Controller中编写文件上传的代码,包括文件上传、文件保存以及将文件信息保存数据库操作,代码如下所示: ``` @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public Map<String, Object> upload(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> map = new HashMap<String, Object>(); try { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("file"); String fileName = file.getOriginalFilename(); String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); String newFileName = UUID.randomUUID().toString() + "." + suffix; String savePath = request.getSession().getServletContext().getRealPath("/") + "upload/"; File savePathFile = new File(savePath); if (!savePathFile.exists()) { savePathFile.mkdirs(); } String filePath = savePath + newFileName; File dest = new File(filePath); file.transferTo(dest); String imgUrl = "/upload/" + newFileName; //图片访问路径 //将文件信息保存数据库 Image image = new Image(); image.setFilename(fileName); image.setUrl(imgUrl); imageService.saveImage(image); map.put("code", 0); map.put("msg", "上传成功"); map.put("data", imgUrl); } catch (Exception e) { e.printStackTrace(); map.put("code", -1); map.put("msg", "上传失败"); map.put("data", ""); } return map; } ``` 以上就是使用Spring MVC和Layui实现图片上传并保存数据库的步骤。希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值