学习目标:
mybatis-plus多表关联查询
学习内容:
mybatis-plus多表关联查询
实体类部分代码
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("wb_member")
public class WbMember implements Serializable {
private static final long serialVersionUID=1L;
@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;
测试接口
@ApiOperation("测试")
@PostMapping("/list/testMember")
public AjaxResult testMember(){
long id = 1339284736172032l;
return AjaxResult.success(iWbMemberService.queryTest(id));
}
mapper继承BaseMapper接口
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);
}
不需要写xml文件,sql写在mapper接口注解中,可以使用mybatis-plus生成器数据库
查询结果为:
重点部分
单表查询结果正常,参数传递正常,我们可以尝试通过多表关联传递参数去查询。
修改为多表查询此处应封装一个对象可能维护方便一些,测试写了map
@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得出多表关联查询。
到此这篇关于mybatis-plus多表关联查询的文章就介绍到这了,更多相关mybatis-plus多表关联查询内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!