コード例 #1
0
 @Test
 public void testGetById() {
   for (int i = 1; i < 10; i++) {
     long id = i;
     User user = userDao.getUserById(id);
     System.out.println(user.getName());
   }
 }
コード例 #2
0
 @Test
 public void testInsert() {
   for (int i = 1; i < 10; i++) {
     User user = new User();
     long id = i;
     user.setId(id);
     user.setName("User" + i);
     // 插入
     userDao.insert(user);
     System.out.println(user.getName());
   }
 }
コード例 #3
0
  @Test
  public void testTransaction() {
    for (int i = 10000; i < 10080; i++) {
      long id = i;
      User user = new User();
      user.setId(id);
      user.setName("User" + i);
      try {
        // 插入master, 事务生效,插入失败
        userDao.insertWithTransaction(user);
      } catch (Exception e) {

      }
    }
  }
コード例 #4
0
 /** 测试事务, 事务注解要放到子类上,才能生效。不可把事务注解放到接口上 */
 @Test
 public void testTransaction() {
   for (int i = 10000; i < 10008; i++) {
     long id = i;
     User user = new User();
     user.setId(id);
     user.setName("User" + i);
     try {
       // 插入master, 事务生效,插入失败
       userDao.insertWithTransaction(user);
     } catch (Exception e) {
     }
     // 插入master, 事务生效,插入失败 count 为0
     RoutingHolder.setCurrentDataSourceKey(null); // Master
     int count = jdbcTemplate.queryForInt("select count(*) from user where id = " + id);
     Assert.assertEquals(0, count);
   }
 }
コード例 #5
0
  /** 测试读写分离 */
  @Test
  public void testWriteMasterReadSlaves() {
    for (int i = 1; i < 10; i++) {
      User user = new User();
      long id = i;
      user.setId(id);
      user.setName("User" + i);
      // 插入master
      userDao.insert(user);

      // 插入成功
      RoutingHolder.setCurrentDataSourceKey(null); // Master
      int count = jdbcTemplate.queryForInt("select count(*) from user where id = " + id);
      Assert.assertEquals(1, count);
      System.out.println(user.getName());

      // 从slave读
      user = userDao.getUserById(id);
      Assert.assertNotNull(user);
      System.out.println(user.getName());
    }
  }