TeamService { public boolean updateByPrimaryKeySelective(Team team) throws Exception { LOG.info("updateByPrimaryKeySelective({})", team.toString()); try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) { TeamMapper teamMapper = sqlSession.getMapper(TeamMapper.class); teamMapper.updateByPrimaryKeySelective(team); TeamMemberMapper teamMemberMapper = sqlSession.getMapper(TeamMemberMapper.class); Map<String, Object> where = new HashMap<>(); where.put("teamId", team.getId()); List<TeamMember> oldTeamMembers = teamMemberMapper.selectAll(where); List<String> oldMembers = new ArrayList<>(); for (TeamMember oldTeamMember : oldTeamMembers) { oldMembers.add(oldTeamMember.getMember()); } List<TeamMember> newTeamMembers = team.getCollaborators(); List<String> newMembers = new ArrayList<>(); for (TeamMember newTeamMember : newTeamMembers) { newMembers.add(newTeamMember.getMember()); } for (TeamMember oldTeamMember : oldTeamMembers) { if (!newMembers.contains(oldTeamMember.getMember())) { teamMemberMapper.deleteByPrimaryKey(oldTeamMember.getId()); } } for (TeamMember newTeamMember : newTeamMembers) { if (!oldMembers.contains(newTeamMember.getMember())) { newTeamMember.setTeamId(team.getId()); newTeamMember.setTeamName(team.getTeamName()); teamMemberMapper.insert(newTeamMember); } } TeamMember teamMember = new TeamMember(); teamMember.setTeamName(team.getTeamName()); teamMember.setTeamId(team.getId()); teamMemberMapper.updateSelective(teamMember); sqlSession.commit(); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new Exception(e); } return true; }  List<Team> queryPageList(String owner,
                                  String column,
                                  String order,
                                  int pageNo,
                                  int pageSize); boolean add(Team team); boolean updateByPrimaryKeySelective(Team team); boolean delete(String id);  }
@Test public void updateByPrimaryKeySelective() throws Exception { TeamMember teamMember = new TeamMember(); teamMember.setTeamName("submarine"); teamMember.setInviter(0); teamMember.setMember("admin"); teamMember.setCreateBy("createByteamMember"); Team team = new Team(); team.setTeamName("submarine"); team.setOwner("test_sub"); team.setCreateBy("createByteam"); List list = new ArrayList<TeamMember>(); list.add(teamMember); team.setCollaborators(list); Boolean ret = teamService.add(team); assertTrue(ret); team.setTeamName("submarine_update"); team.setUpdateBy("submarine_user_update"); TeamMember teamMemberUpdate = new TeamMember(); teamMemberUpdate.setTeamName("submarine"); teamMemberUpdate.setInviter(0); teamMemberUpdate.setMember("test_member"); teamMemberUpdate.setCreateBy("createByteamMember2"); list.add(teamMemberUpdate); boolean editRet = teamService.updateByPrimaryKeySelective(team); assertTrue(editRet); List<Team> teamList = teamService.queryPageList("test_sub", "create_time", "desc", 0, 100); assertEquals(teamList.size(), 1); Team teamDb = teamList.get(0); assertEquals(team.getTeamName(), teamDb.getTeamName()); List<TeamMember> teamMemberList = teamDb.getCollaborators(); assertEquals(teamMemberList.size(), 2); for (TeamMember member : teamMemberList) { assertEquals(member.getTeamName(), team.getTeamName()); assertEquals(team.getUpdateBy(), teamDb.getUpdateBy()); } LOG.info("team.UpdateTime:{}", teamDb.getUpdateTime()); }