此文已由做者夏昀受權網易雲社區發佈。
html
歡迎訪問網易雲社區,瞭解更多網易技術產品運營經驗。web
Model類以下:package com.xy.modelspring
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.devdaotomcat
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代碼mybatis
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上。併發
測試:app
request parameters: hasException=false測試
返回:true 數據添加成功
request parameters: hasException=true
返回:false 兩個數據庫數據都未添加
網易雲免費體驗館,0成本體驗20+款雲產品!
更多網易技術、產品、運營經驗分享請點擊。
相關文章:
【推薦】 在一臺服務器上搭建相對高可用HiveServer實踐
【推薦】 接口測試之Kotlin篇(下)
【推薦】 測試角度的併發和冪等問題總結