@Test publicvoidtestUpdateByUpdateQuery(){ User user = new User(); user.setId(19); user.setUsername("frank19"); user.setPassword("frank"); int result = userDao.insert(user); assertEquals(1, result); UpdateQuery<User> userUpdateQuery = UpdateQuery.createQuery(User.class) // 设置有值 .set(User::getUsername, "Marry") // 设置null .set(User::getPassword, null) .and(User::getId, isEqual(19)); result = userDao.updateByUpdateQuery(userUpdateQuery); assertEquals(1, result); final User user1 = userDao.selectByPrimaryKey(19); // 有值的设置成功更新 assertEquals("Marry", user1.getUsername()); // 设置为null 也成功 assertNull(user1.getPassword()); userDao.deleteByPrimaryKey(19); }
我们同样可以看一下 output 验证
1 2 3 4 5 6 7 8 9 10 11
JDBC Connection [HikariProxyConnection@1220747354 wrapping conn0: url=jdbc:h2:mem:default user=SA] will not be managed by Spring ==> Preparing: INSERT INTO users ( id,username,password ) VALUES( ?,?,? ) ==> Parameters: 19(Integer), frank19(String), frank(String) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@771db12c] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ba05e38] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@179060558 wrapping conn0: url=jdbc:h2:mem:default user=SA] will not be managed by Spring ==> Preparing: UPDATE users SET password=?,username=? WHERE (id = ?) ==> Parameters: null, Marry(String), 19(Integer) <== Updates: 1