springboot 联合查询

使用@mapper         不使用xml配置(自动注入)

联合查询

检索能力太差查了一晚上没能解决

具体问题:

Employee类 属性:

@Data
@AllArgsConstructor

@NoArgsConstructor
@Component

public class Employee {

    private Integer id;
    private String lastName;
    private String email;
    private  Integer gender;//0 女  1 男
    private  Department department;
    private Integer age;
    
}

Department类

//部门表
@Data    //使用lombok (为了下面的有参无参)
@AllArgsConstructor   //有参
@NoArgsConstructor     //无参
@Component
public class Department {
    private Integer id;
    private String departmentName;
}

EmpMapper:

@Select("select * from emp where id = #{id}")
@Results({
        @Result(property = "department", column="department",
one = @One(select = "com.example.dao.DepMapper.getDepartmentById"))
})
Employee getEmpAndDeptByid(@Param("id") int id);
     * @Result 的使用:
     * property:查询结果赋值给此实体属性(pojo实体类的名称 column是实际数据库字段 对应实例化property字段)
     * column:对应数据库的表字段,做为下面@One(select方法的查询参数
     * one:一对一的查询
     * @One(select = 方法全路径) :调用的方法
@Mapper
@Repository  //标记为dao层
public interface EmpMapper {

  
    @Select("select * from emp where id = #{id}")
    @Results({
            @Result(property = "department", column="department",one = @One(select = "com.example.dao.DepMapper.getDepartmentById"))
    })

    Employee getEmpAndDeptByid(@Param("id") int id);



    }

DepMapper:

@Mapper
@Repository  //标记为dao层
public interface DepMapper {
    @Select("select * from department")
    List<Department> getDepartments();

    @Select("select * from department where id=#{id}")
    Department getDepartmentById(@Param("id") int id);


}

具体参考

spring boot 2使用Mybatis多表关联查询 - gdjlc - 博客园

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值