ProjectService { public boolean updateByPrimaryKeySelective(Project project) throws Exception { LOG.info("updateByPrimaryKeySelective({})", project.toString()); try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) { ProjectMapper projectMapper = sqlSession.getMapper(ProjectMapper.class); projectMapper.updateByPrimaryKeySelective(project); ProjectFilesMapper projectFilesMapper = sqlSession.getMapper(ProjectFilesMapper.class); Map<String, Object> where = new HashMap<>(); where.put("projectId", project.getId()); List<ProjectFiles> oldProjectFiles = projectFilesMapper.selectAll(where); List<String> oldProjectFilesId = new ArrayList<>(); for (ProjectFiles oldProjectFile : oldProjectFiles) { oldProjectFilesId.add(oldProjectFile.getId()); } List<ProjectFiles> currProjectFiles = project.getProjectFilesList(); List<String> currProjectFilesId = new ArrayList<>(); for (ProjectFiles currProjectFile : currProjectFiles) { currProjectFilesId.add(currProjectFile.getId()); } for (ProjectFiles old : oldProjectFiles) { if (!currProjectFilesId.contains(old.getId())) { projectFilesMapper.deleteByPrimaryKey(old.getId()); } else { for (ProjectFiles currProjectFile : currProjectFiles) { if (currProjectFile.getId() != null && currProjectFile.getId().equals(old.getId())) { projectFilesMapper.updateByPrimaryKeySelective(currProjectFile); } } } } for (ProjectFiles curr : currProjectFiles) { if (curr.getId() == null) { curr.setProjectId(project.getId()); projectFilesMapper.insert(curr); } } sqlSession.commit(); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new Exception(e); } return true; }  List<Project> queryPageList(String userName,
                                     String column,
                                     String order,
                                     int pageNo,
                                     int pageSize); boolean add(Project project); boolean updateByPrimaryKeySelective(Project project); boolean delete(String id);  }
@Test public void updateByPrimaryKeySelective() throws Exception { ProjectFiles projectFiles = new ProjectFiles(); projectFiles.setFileContent("ProjectServiceTest-FileContent"); projectFiles.setFileName("ProjectServiceTest-FileName"); projectFiles.setCreateBy("ProjectServiceTest-UserName"); Project project = new Project(); project.setDescription("ProjectServiceTest-Description"); project.setName("ProjectServiceTest-ProjectName"); project.setType("PROJECT_TYPE_NOTEBOOK"); project.setUserName("ProjectServiceTest-UserName"); project.setVisibility("PROJECT_VISIBILITY_PRIVATE"); project.setCreateBy("ProjectServiceTest-UserName"); List list = new ArrayList<ProjectFiles>(); list.add(projectFiles); project.setProjectFilesList(list); Boolean ret = projectService.add(project); assertTrue(ret); project.setName("update_projectName"); project.setDescription("update_description"); project.setVisibility("PROJECT_VISIBILITY_PUBLIC"); project.setUpdateBy("project_updateBy"); ProjectFiles projectFilesUpdate = new ProjectFiles(); projectFilesUpdate.setFileContent("ProjectServiceTest-FileContent2"); projectFilesUpdate.setFileName("ProjectServiceTest-FileName2"); projectFilesUpdate.setCreateBy("ProjectServiceTest-UserName2"); list.add(projectFilesUpdate); projectFiles.setFileName("update_fileName"); projectFiles.setFileContent("update_fileContent"); projectFiles.setUpdateBy("projectFiles_updateby"); boolean editRet = projectService.updateByPrimaryKeySelective(project); assertTrue(editRet); List<Project> projectList = projectService.queryPageList("ProjectServiceTest-UserName", "create_time", "desc", 0, 100); assertEquals(projectList.size(), 1); Project projectDb = projectList.get(0); assertEquals(project.getName(), projectDb.getName()); assertEquals(project.getDescription(), projectDb.getDescription()); assertEquals(project.getVisibility(), projectDb.getVisibility()); assertEquals(project.getUpdateBy(), projectDb.getUpdateBy()); LOG.info("update_time:{}", projectDb.getUpdateTime()); List<ProjectFiles> projectFilesList = projectDb.getProjectFilesList(); for (ProjectFiles files : projectFilesList) { if (!files.getFileContent().equals("ProjectServiceTest-FileContent2")) { assertEquals(files.getFileName(), projectFiles.getFileName()); assertEquals(files.getFileContent(), projectFiles.getFileContent()); assertEquals(files.getUpdateBy(), projectFiles.getUpdateBy()); } } assertEquals(projectFilesList.size(), 2); }