OrgEmployeeProDto 包含字段empCode和empName List<OrgEmployeeProDto> employeeProDtoList 转换成Map<String, String> map map的key值为empCode,value值为empName
时间: 2023-12-14 22:38:39 浏览: 28
可以使用Java 8的流式操作来实现这个转换过程,具体代码如下:
```
Map<String, String> map = employeeProDtoList.stream()
.collect(Collectors.toMap(OrgEmployeeProDto::getEmpCode, OrgEmployeeProDto::getEmpName));
```
这段代码将List<OrgEmployeeProDto> employeeProDtoList转换成了Map<String, String> map,其中map的key值为每个OrgEmployeeProDto对象的empCode属性,value值为每个OrgEmployeeProDto对象的empName属性。
相关问题
<resultMap id="empAndDeptResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <result column="dept_id" property="dept.deptId"></result> <result column="dept_name" property="dept.deptName"></result> </resultMap>中为什么Emp类中没有无参构造MyBatis却能正确创建Emp对象
在MyBatis中,如果一个类中没有显式定义无参构造函数,Java会自动提供一个无参构造函数。当MyBatis创建Emp对象时,它会首先调用Emp的无参构造函数来创建一个对象,然后通过调用setter方法来设置对象的属性。因此,尽管Emp类中没有显式定义无参构造函数,MyBatis仍然可以正确创建Emp对象。
但是,如果Emp类中定义了有参构造函数,而没有显式定义无参构造函数,那么在创建Emp对象时就会出现问题。因为MyBatis无法通过调用无参构造函数来创建Emp对象,因此会抛出异常。所以,最好为每个类都显式地定义一个无参构造函数,以确保MyBatis能够正确地创建对象。
update tbl_emp <set> <if test="empName != null"> emp_name = #{empName,jdbcType=VARCHAR}, </if> <if test="gender != null"> gender = #{gender,jdbcType=CHAR}, </if> <if test="email != null"> email = #{email,jdbcType=VARCHAR}, </if> <if test="dId != null"> d_id = #{dId,jdbcType=INTEGER}, </if> </set> where emp_id = #{empId,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.zhiyou.bean.Employee">
这是一个 MyBatis 的 XML 映射文件中定义的 SQL 语句,用于更新员工表(tbl_emp)中的数据。这个 SQL 语句使用了 MyBatis 提供的动态 SQL 功能,根据传入的参数来决定更新哪些列的数据。其中,<if> 标签用于判断参数是否为 null,如果不为 null,则更新对应的列。这个 SQL 语句还定义了两个 update 操作,一个是根据 emp_id 更新数据,另一个是根据主键更新数据。