/** 新增 */ @Test public void testInsert() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { UserLoginMapper mapper = sqlSession.getMapper(UserLoginMapper.class); UserLogin userLogin = new UserLogin(); userLogin.setUsername("abel533"); userLogin.setLogindate(new Date()); userLogin.setLoginip("192.168.123.1"); Assert.assertEquals(1, mapper.insert(userLogin)); Assert.assertNotNull(userLogin.getLogid()); Assert.assertTrue(userLogin.getLogid() > 10); // 这里测了实体类入参的删除 Assert.assertEquals(1, mapper.deleteByPrimaryKey(userLogin)); } finally { sqlSession.close(); } }
/** 根据主键全更新 */ @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(); } }