Beispiel #1
0
  /** 根据主键全更新 */
  @Test
  public void testUpdateByPrimaryKey() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
      UserLoginMapper mapper = sqlSession.getMapper(UserLoginMapper.class);
      Map<String, Object> key = new HashMap<String, Object>();
      key.put("logid", 2);
      key.put("username", "test1");
      UserLogin userLogin = mapper.selectByPrimaryKey(key);
      Assert.assertNotNull(userLogin);
      userLogin.setLoginip("1.1.1.1");
      userLogin.setLogindate(null);
      // 不会更新username
      Assert.assertEquals(1, mapper.updateByPrimaryKey(userLogin));

      userLogin = mapper.selectByPrimaryKey(userLogin);
      Assert.assertNull(userLogin.getLogindate());
      Assert.assertEquals("1.1.1.1", userLogin.getLoginip());
    } finally {
      sqlSession.close();
    }
  }
Beispiel #2
0
  /** 主要测试删除 */
  @Test
  public void testDelete() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
      UserLoginMapper mapper = sqlSession.getMapper(UserLoginMapper.class);
      // 查询总数
      Assert.assertEquals(10, mapper.selectCount(new UserLogin()));
      // 根据主键查询
      Map<String, Object> key = new HashMap<String, Object>();
      key.put("logid", 1);
      key.put("username", "test1");
      UserLogin userLogin = mapper.selectByPrimaryKey(key);
      // 根据主键删除
      Assert.assertEquals(1, mapper.deleteByPrimaryKey(key));

      // 查询总数
      Assert.assertEquals(9, mapper.selectCount(new UserLogin()));
      // 插入
      Assert.assertEquals(1, mapper.insert(userLogin));
    } finally {
      sqlSession.close();
    }
  }