mybatis中返回一个List字段

目的:在一个查询接口里面,返回一个人的信息,以及这个人所携带的东西的信息,返回效果如下:

{
  "msg": {
    "listMain": [
      {
        "id": "dd4a2f49b9c94196b065d425e1338ec4",
        "userName": "张三",
        "age": 24,
        "sex": "男",
        "list": [
          {
            "listid": "39f5d745751c97e4efadc7b92559",
            "mainId": "dd4a2f49b9c94196b065d425e1338ec4",
            "username": "张三的手机"
          },
          {
            "listid": "hvbhu515e751c97e4efadc7b92663",
            "mainId": "dd4a2f49b9c94196b065d425e1338ec4",
            "username": "张三的书包"
          }
        ]
      }
    ]
  }
}

实现方法:通过mybatis即可实现

<resultMap id="BaseResultMap" type="com.mihutao.pojo.userInfo">
<id column="id" jdbcType="VARCHAR" property="id"/>
<id column="userName" jdbcType="VARCHAR" property="user_name"/>
<id column="age" jdbcType="VARCHAR" property="age"/>
<id column="sex" jdbcType="VARCHAR" property="sex"/>
<collection property="flows" column="id"
ofType="com.mihutao.pojo.userThing"
select="com.mihutao.mapper.flowMapper.selectFlowById">
</collection>
//conllection这个标签就是在返回值里面引入list的方法
//其中flows是对应userInfo这个实体类的一个List字段
//oftype指的是返回的内部list接收的实体类
//select指的是另一个xml里面的查询方法,将collection中column的id传入到selectFlowById中
//总结就是将collection中column="id"传入到selectFlowById中,返回值装入ofType="com.mihutao.pojo.userThing"最后这一个list放入property="flows"这个字段中
</resultMap>
<sql id="selectUserVo">
select id, user_name,age from user_info
</sql>

<select id="selectUserInfo" resultMap="BaseResultMap">
<include refid="selectUserVo"></include>
</select>

其中userInfo实体类中flows的写法如下

private List<Flow> Flows = new ArrayList<>();

 

posted @ 2022-06-06 16:04  迷糊桃  阅读(1507)  评论(0编辑  收藏  举报