此文已由作者夏昀授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
Model类如下:package com.xy.model
1 package com.xy.model; 2 3 /** 4 * Created by helloworld on 2015/1/30. 5 */ 6 public class NameQa { 7 private long id; 8 private String nameQa; 9 10 public long getId() { 11 return id; 12 } 13 14 public void setId(long id) { 15 this.id = id; 16 } 17 18 public String getNameQa() { 19 return nameQa; 20 } 21 22 public void setNameQa(String nameQa) { 23 this.nameQa = nameQa; 24 } 25 }
1 package com.xy.model; 2 3 /** 4 * Created by helloworld on 2015/1/30. 5 */ 6 public class NameDev { 7 private long id; 8 private String nameDev; 9 10 public long getId() { 11 return id; 12 } 13 14 public void setId(long id) { 15 this.id = id; 16 } 17 18 public String getNameDev() { 19 return nameDev; 20 } 21 22 public void setNameDev(String nameDev) { 23 this.nameDev = nameDev; 24 } 25 }
qa数据源的mybatis mapper接口 package com.xy.dao
1 package com.xy.dao; 2 3 import com.xy.model.NameQa; 4 5 /** 6 * Created by helloworld on 2015/1/30. 7 */ 8 public interface NameQaMapper { 9 int insert(NameQa nameQa); 10 }
dev数据源的mybatis mapper接口 package com.xy.devdao
1 package com.xy.daodev; 2 3 import com.xy.model.NameDev; 4 5 /** 6 * Created by helloworld on 2015/1/30. 7 */ 8 public interface NameDevMapper { 9 int insert(NameDev nameDev); 10 }
处理事务的service
1 package com.xy.service; 2 3 import com.xy.dao.NameQaMapper; 4 import com.xy.daodev.NameDevMapper; 5 import com.xy.model.NameDev; 6 import com.xy.model.NameQa; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Transactional;10 11 /** 12 * Created by helloworld on 2015/1/30. 13 */ 14 @Service 15 public class NameService { 16 @Autowired 17 NameQaMapper nameQaMapper; 18 @Autowired 19 NameDevMapper nameDevMapper; 20 21 @Transactional(rollbackFor = Exception.class) 22 public void addQaAndDev(boolean hasException) throws Exception { 23 NameQa nameQa = new NameQa(); 24 nameQa.setNameQa("qa"); 25 nameQaMapper.insert(nameQa); 26 27 NameDev nameDev = new NameDev(); 28 nameDev.setNameDev("dev"); 29 nameDevMapper.insert(nameDev); 30 31 if(hasException) { 32 throw new Exception(); 33 } 34 } 35 36 37 }
controller代码
1 package com.xy.controller; 2 3 import com.xy.service.NameService; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.ModelMap; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.RequestParam; 10 11 /** 12 * Created by helloworld on 2014/11/22. 13 */ 14 @Controller 15 public class mybatisController { 16 17 @Autowired 18 NameService nameService; 19 20 @RequestMapping(value = "/addName", method = RequestMethod.POST) 21 ModelMap addName(@RequestParam("hasException") boolean hasException) { 22 try { 23 nameService.addQaAndDev(hasException); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 return new ModelMap("false"); 27 } 28 return new ModelMap("true"); 29 } 30 31 32 }
将项目打成war包,命名为mybatis.war部署在tomcat上。
测试:
request parameters: hasException=false
返回:true 数据添加成功
request parameters: hasException=true
返回:false 两个数据库数据都未添加
网易云免费体验馆,0成本体验20+款云产品!
更多网易技术、产品、运营经验分享请点击。
相关文章:
【推荐】 从海报视觉到网页设计
【推荐】 【你离硅谷只差一步】网易中国创业家大赛项目火热征集中
【推荐】 关于Runtime.getRuntime().exec()产生阻塞的2个陷阱