springboot

springboot&MyBatis-Plus 关联查询

2019-01-09  本文已影响0人  大韭哥

版本:springboot2.1.1&MyBatis-Plus3.0.7.1

table1 与 table2 一对一
table1 与 table3 多对多
1.分别生成table1,table2,table3
2.table1中添加table2,table3注意添加注解,如果是多对多需要为List

  @TableField(exist=false)
    private Table2 table2;
  @TableField(exist=false)
    private List<Table3> listTable3;

3.Table1Mapper中添加对应方法

//select * from table1 a left join table2 on a.id=b.id
@Results({
            @Result(id=true,column="id",property="id"),//主键,不是主键可以不写
            @Result(column="id",property="id"),
            @Result(column="id",property="table2",//一对一
                    one=@One(
                            select="com.xx.xx.mapper.Table2Mapper.selectById",
                            fetchType= FetchType.EAGER
                    )
            ) ,

//            @Result(column="id",property="listTable3",//多对多
//                    many=@Many(
//                            select="com.xx.xx.mapper.Table3Mapper.selectById",
//                            fetchType= FetchType.EAGER
//                    )
//            )
    })
    @Select("SELECT * FROM `table1` WHERE id = #{id}")
    Table1 selectByIdWithTable2(Integer id);
上一篇 下一篇

猜你喜欢

热点阅读