mybatis返回List<Integer> resultMap怎么写
时间: 2023-06-18 17:05:45 浏览: 132
可以使用MyBatis的ResultMap来映射查询结果到Java对象或基本类型的集合中。对于返回类型为List<Integer>,可以按照以下方式进行配置:
1. 在mapper.xml中定义一个ResultMap,例如:
```
<resultMap id="integerResultMap" type="java.lang.Integer" />
```
2. 在查询语句中引用ResultMap,例如:
```
<select id="selectIds" resultMap="integerResultMap">
SELECT id FROM my_table
</select>
```
3. 在Java代码中调用mapper方法,例如:
```
List<Integer> ids = sqlSession.selectList("selectIds");
```
这样,查询结果中的每个整数值都会被映射为Java中的Integer对象,并被添加到返回的List中。
相关问题
mapper层怎么用List<Integer>返回多个Integer值
对于这个问题,可以使用MyBatis提供的ResultMap来实现。首先在Mapper.xml中定义resultMap,指定返回值类型为List<Integer>,并设置对应的列名。然后在Mapper接口中定义对应的方法,通过@ResultMap注解指定使用的resultMap即可。具体实现可以参考以下代码示例:
Mapper.xml:
```
<resultMap id="intListMap" type="java.util.List">
<result column="col1" property="0"/>
<result column="col2" property="1"/>
<result column="col3" property="2"/>
</resultMap>
<select id="getIntList" resultMap="intListMap">
select col1, col2, col3 from my_table
</select>
```
Mapper.java:
```
@ResultMap("intListMap")
List<Integer> getIntList();
```
mybatis如何映射下面的实体类 public class EquipmentVo extends RegionalEquipment { private List<EquipmentDataVo> equipmentDataVoList; private List<Map> maps; public List<Map> getMaps() { return maps; } public void setMaps(List<Map> maps) { this.maps = maps; } public List<EquipmentDataVo> getEquipmentDataVoList() { return equipmentDataVoList; } public void setEquipmentDataVoList(List<EquipmentDataVo> equipmentDataVoList) { this.equipmentDataVoList = equipmentDataVoList; } }
在 MyBatis 中映射这个实体类,可以新建一个对应的映射文件,并在该文件中编写 SQL 语句和相应的映射规则。以下是一个映射文件的示例:
```
<resultMap id="equipmentVoMap" type="com.example.EquipmentVo">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
<result property="equipmentDataVoList" column="id" javaType="java.lang.Integer" select="com.example.EquipmentDataMapper.selectByEquipmentId"/>
<result property="maps" column="id" javaType="java.lang.Integer" select="com.example.MapMapper.selectByEquipmentId"/>
</resultMap>
<select id="selectById" resultMap="equipmentVoMap">
SELECT id, name, create_time, update_time
FROM equipment
WHERE id = #{id}
</select>
```
在上面的示例中,`resultMap` 标签定义了一个结果映射,其中使用了 `<result>` 标签将查询结果的列映射到实体类的属性上。其中 `equipmentDataVoList` 属性和 `maps` 属性的值需要通过另外的 SQL 语句进行查询,所以在 `<result>` 标签中使用了 `select` 属性来指定查询的 SQL 语句和对应的 Mapper 接口方法。在实现这些 Mapper 接口方法时,需要通过 MyBatis 的动态 SQL 语句来拼接查询条件,以保证查询的准确性。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)