用mybatis-plus使用注解表示一对多关系

service

    @Override
    public List<Admin> getAllAdmins(String keywords) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("a.id",AdminUtils.getCurrentAdmin().getId());
//        queryWrapper.like("a.name",keywords);
//        queryWrapper.orderByAsc("a.id");
        return adminMapper.getAllAdmins(queryWrapper);
    }

mapper

  /**,ar.admin_id aradminId,ar.rid arrid
     * 获取所有操作员
     * @return
     */
    @Select("select a.* from t_admin a ")
    @Results({
            @Result(column="id",property="id"),
            @Result(column="address",property="address"),
            @Result(column="create_time",property="createTime"),
            @Result(column="name",property="name"),
            @Result(column="phone",property="phone"),
            @Result(column="remark",property="remark"),
            @Result(column="telephone",property="telephone"),
            @Result(column="update_time",property="updateTime"),
            @Result(column="user_face",property="userFace"),
            @Result(column="username",property="username"),
            @Result(column="admin_id",property="aradminId"),
            @Result(column="rid",property="arrid"),

            @Result(column="name",property="name"),
            @Result(column="name_zh",property="nameZh"),
            //这里的id是不能写错,对应的是property,我以为是写数据库的字段名,原来是写别名字段  如果修改使用了别名字段,需要用到别名
            @Result(column="id",property="roles",many=@Many(select="getAllRoles",**fetchType= FetchType.EAGER**)),
    })
    List<Admin> getAllAdmins(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select * from t_admin_role ar  left join t_role r on ar.rid = r.id where ar.admin_id = #{id}")
    List<Role> getAllRoles(@Param("id") Long id);

FetchType.LAZY =除非您通过getter方法调用它,否则不会加载关系。
FetchType.EAGER =这将加载所有关系。
这两种类型的利弊。
Lazy initialization 通过避免不必要的计算来提高性能并减少内存需求。
Eager initialization 需要更多的内存消耗,并且处理速度很慢。

数据展示

原文地址:https://www.cnblogs.com/rzkwz/p/14883369.html