You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

7 months ago
  1. package com.gaotao.service;
  2. import com.gaotao.datasource.annotation.DataSource;
  3. import com.gaotao.modules.sys.dao.SysUserDao;
  4. import com.gaotao.modules.sys.entity.SysUserEntity;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. /**
  9. * 测试多数据源
  10. *
  11. *
  12. */
  13. @Service
  14. //@DataSource("slave1")
  15. public class DynamicDataSourceTestService {
  16. @Autowired
  17. private SysUserDao sysUserDao;
  18. @Transactional
  19. public void updateUser(Long id){
  20. SysUserEntity user = new SysUserEntity();
  21. user.setUserId(id);
  22. user.setMobile("13500000000");
  23. sysUserDao.updateById(user);
  24. }
  25. @Transactional
  26. @DataSource("slave1")
  27. public void updateUserBySlave1(Long id){
  28. SysUserEntity user = new SysUserEntity();
  29. user.setUserId(id);
  30. user.setMobile("13500000001");
  31. sysUserDao.updateById(user);
  32. }
  33. @DataSource("slave2")
  34. @Transactional
  35. public void updateUserBySlave2(Long id){
  36. SysUserEntity user = new SysUserEntity();
  37. user.setUserId(id);
  38. user.setMobile("13500000002");
  39. sysUserDao.updateById(user);
  40. //测试事物
  41. int i = 1/0;
  42. }
  43. }