List<HospitalExperience> hospitalExperiences = hospitalExperienceDao.selectCurrentPeople(example); return hospitalExperiences.stream().map(hospital -> { HospitalExperienceResp resp = new HospitalExperienceResp(); BeanUtils.copyProperties(hospital, resp); resp.setAjzddsc(hospital.getJzDdSc()); resp.setBjzddSc(hospital.getJzddSc()); return resp; }).collect(Collectors.toList()); }解释下代码
时间: 2024-03-28 07:36:27 浏览: 85
这段代码是一个方法,传入一个参数example,返回一个List<HospitalExperience>类型的对象hospitalExperiences。然后使用Java 8的流式编程方式对hospitalExperiences进行处理,将其转换为List<HospitalExperienceResp>类型的对象并返回。
具体地,通过stream()方法将List<HospitalExperience>的对象hospitalExperiences转换为一个流Stream<HospitalExperience>,然后使用map()方法对每一个HospitalExperience对象进行处理,将其转换为HospitalExperienceResp对象并返回。在这个过程中,使用了BeanUtils.copyProperties()方法将HospitalExperience对象的属性值复制到HospitalExperienceResp对象中,并对其中的两个属性ajzddsc和bjzddSc进行了设置。最后,使用collect()方法将处理后的结果收集到一个List<HospitalExperienceResp>对象中并返回。
相关问题
优化这段代码 private GroupMemberResp setmenberResp(CustomerGroupMember member){ GroupMemberResp resp=new GroupMemberResp(); BeanUtil.copyProperties(member,resp); List<CustomerGroupMember> memberSize=iCustomerGroupMemberService.queryGroupMembersinfo(member.getUserId()); resp.setgroupcount(memberSize.size()); List<String> chatIdList=memberSize.stream().map(t->t.getChatId()).collect(Collectors.toList()); List<CustomerGroup> groupList=getGroupListByChatId(chatIdList); List<String> groupNameList=groupList.stream().map(t->t.getChatIName()).collect(Collectors.toList()); resp.setgroupName(groupNameList.toString().replace(“,”,”|”)); if(member.getMemberType()==1){ EmployeeOrgConfig orgConfig=employeeOrgConfigService.getEmployeeOrgConfig(member.getUserId()); if(orgConfig!=null){ resp.setMobile(orgConfig.getMobile()); resp.setIdCard(orgConfig.getIdCard()); } }else{ Customer customer=customerService.getCustomer(member.getUserId()); if(customer!=null){ resp.setMobile(customer.getMobile()); resp.setIdCard(customer.getIdCard());. resp.setStatus(customer.getStatus()); } } Return resp; }
优化后的代码如下:
private GroupMemberResp setMemberResp(CustomerGroupMember member) {
GroupMemberResp resp = new GroupMemberResp();
BeanUtil.copyProperties(member, resp);
List<CustomerGroupMember> memberSize = iCustomerGroupMemberService.queryGroupMembersinfo(member.getUserId());
resp.setGroupCount(memberSize.size());
List<String> chatIdList = memberSize.stream().map(CustomerGroupMember::getChatId).collect(Collectors.toList());
List<CustomerGroup> groupList = getGroupListByChatId(chatIdList);
List<String> groupNameList = groupList.stream().map(CustomerGroup::getChatIName).collect(Collectors.toList());
resp.setGroupName(String.join("|", groupNameList));
if (member.getMemberType() == 1) {
EmployeeOrgConfig orgConfig = employeeOrgConfigService.getEmployeeOrgConfig(member.getUserId());
if (orgConfig != null) {
resp.setMobile(orgConfig.getMobile());
resp.setIdCard(orgConfig.getIdCard());
}
} else {
Customer customer = customerService.getCustomer(member.getUserId());
if (customer != null) {
resp.setMobile(customer.getMobile());
resp.setIdCard(customer.getIdCard());
resp.setStatus(customer.getStatus());
}
}
return resp;
}
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { SqlSession sqlSession = com.nxu.MyBatisUtils.openSession(); try { TeamMapper teamMapper = MyBatisUtils.getMapper(TeamMapper.class); List<Team> teamList = teamMapper.selectTeam(""); req.setAttribute("teamList", teamList); sqlSession.commit(); } catch (Exception e) { sqlSession.rollback(); e.printStackTrace(); } finally { com.nxu.MyBatisUtils.closeSession(sqlSession); } req.getRequestDispatcher("/teamList.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String parameter = req.getParameter("name"); SqlSession sqlSession = com.nxu.MyBatisUtils.openSession(); try { TeamMapper teamMapper = sqlSession.getMapper(TeamMapper.class); List<Team> teamList = teamMapper.selectTeam(parameter); req.setAttribute("teamList", teamList); sqlSession.commit(); } catch (Exception e) { sqlSession.rollback(); e.printStackTrace(); } finally { com.nxu.MyBatisUtils.closeSession(sqlSession); } req.getRequestDispatcher("/teamList.jsp").forward(req, resp); }
这段代码是一个简单的Java Servlet,它处理GET和POST请求,并使用MyBatis访问数据库。
在doGet方法中,它打开一个数据库会话(SqlSession),然后通过MyBatis获取一个TeamMapper实例。然后,它调用selectTeam方法来查询团队信息,并将结果存储在名为"teamList"的请求属性中。最后,它提交会话并将请求转发到"/teamList.jsp"页面。
在doPost方法中,它首先获取请求参数"name"的值。然后,它打开一个数据库会话(SqlSession),通过MyBatis获取一个TeamMapper实例,并使用selectTeam方法查询团队信息。查询结果也存储在名为"teamList"的请求属性中。最后,它提交会话并将请求转发到"/teamList.jsp"页面。
这段代码使用了MyBatis框架来简化数据库访问操作。它使用了注解方式配置数据库映射,并使用了自定义的工具类MyBatisUtils来获取数据库会话和Mapper实例。同时,它还处理了数据库事务,并在异常发生时进行回滚操作。
请注意,这只是一小部分代码,可能还有其他代码文件和配置文件与之相关。如果您有其他问题或需要更多详细信息,请随时提问。
阅读全文