mybatis-plus多表关联查询
mybatis-plus多表关联查询
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("wb_member")
public class WbMember implements Serializable {
private static final long serialVersionUID=1L;
/**
* 用户ID
*/
@ApiModelProperty(value = "用户ID", position = 0)
@TableId(value = "id", type = IdType.ASSIGN_ID)
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long id;
/**
* 昵称
*/
@ApiModelProperty(value = "昵称", position = 1)
private String nickName;
/**
* 手机号码
*/
@ApiModelProperty(value = "手机号码", position = 2)
private String phone;
/**
* 密码
*/
@ApiModelProperty(value = "密码", position = 3)
private String password;
/**
* 测试接口
* @return
*/
@ApiOperation("测试")
@PostMapping("/list/testMember")
public AjaxResult testMember(){
long id = 1339284736172032l;
return AjaxResult.success(iWbMemberService.queryTest(id));
}
public interface WbMemberMapper extends BaseMapper<WbMember> {
@Select({
"select",
"* ",
"from wb_member",
"where id = #{id,jdbcType=INTEGER}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="nick_name", property="nickName", jdbcType=JdbcType.VARCHAR),
@Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
@Result(column="sex", property="sex", jdbcType=JdbcType.INTEGER),
@Result(column="birthday", property="birthday", jdbcType=JdbcType.VARCHAR)
})
List<WbMember> queryTest(long id);
}
单表查询结果正常,参数传递正常,我们可以尝试通过多表关联传递参数去查询。
@Select({
"select",
"u.user_id,u.user_name,u.nick_name ",
"from sys_user u,sys_user_role r,sys_role o",
"where u.user_id = r.user_id AND r.role_id = o.role_id",
"AND u.user_id = #{id,jdbcType=INTEGER}"
})
@Results({
@Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
@Result(column="nick_name", property="nickName", jdbcType=JdbcType.VARCHAR),
})
List<Map> queryTest(long id);
测试结果和mybatis写的xml运行结果一致,所以这种方法也是可以实现mybatis-plus多表关联查询
通过mapper中书写sql得出多表关联查询。