sinat_38423095
小鱼的学习之路
2021-01-17 17:00

mybatis使用注解多表查询疑问

  • java
  • mysql

先贴实体类
```
public class VideoOrder {
    private int id;

    private String outTradeNo;

    private String state;

    private Date createTime;

    private int totalFee;

    private int videoId;

    private String videoTitle;

    private String videoImg;

    private int userId;

    private User user;

    get  set 方法
}

```
```
public class User {

    private int id;

    private String name;

    private String pwd;

    private String headImg;

    private String phone;

    private Date createTime;

    private List<VideoOrder> videoOrderList;

   get set 方法
}
```
```
在使用mybatis 的注解方式对数据库内信息进行多表查询时,只返回了VideoOrder查询信息,而user返回信息为null,求答疑!!
贴mapper 代码

```
public interface VideoOrderMapper {

    
    @Select("SELECT O.id,O.video_title,O.total_fee,U.name,U.phone FROM" +
            " video_order O " +
            "LEFT JOIN " +
            "user U ON O.user_id = U.id")
    @Results(id = "queryVideoOrderList",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "videoTitle",column = "video_title"),
            @Result(property = "totalFee",column = "total_fee"),
            @Result(property = "user",column = "user_id",one = @One(select = "cn.my.mapper.UserMapper.findUserById"),javaType = User.class)
    })
    public List<VideoOrder> queryVideoOrderList();
}

```
```

再贴一下输出结果

[VideoOrder{id=1, outTradeNo='null', state='null', createTime=null, totalFee=32, videoId=0, videoTitle='xx教程', videoImg='null', userId=0, user=null}, VideoOrder{id=2, outTradeNo='null', state='null', createTime=null, totalFee=42, videoId=0, videoTitle='Harbor', videoImg='null', userId=0, user=null}]

初学Mybatis还请各位大神多多指教!谢谢各位大神!!!!跪求答案

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐