mybatis 注解查询 ,one to many 从员工查部门

 

库表中,在部门表中 departId,这个名字需要一致(2个表),用它来建立外键关系。

 

1. 建立基本的springboot项目

2. 其中user   bean,有字段 depart,(类),不需要有departId(理论模型虽不需要,但实际业务时往往需要,若需要前台mvc页面注入属性,如在添加业务时,这里最好能写上 Integer departId)。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    private Integer id;

    private String name;

    private String address;

    private Date birth;

    private  Depart depart;      // 引用部门

    private static final long serialVersionUID = 1L;
}

 

3. 在userdao上建立注解,这里one引用的是departDao里接口里的方法的全名。,这里的写法是sql99标准,using(departid),这个字段需要2各表名称一致。

    @Select("select * from tbl_user inner join tbl_depart using(departid) where id=#{value}")
    @Results({
            @Result(property = "depart",column = "departid",javaType = Depart.class,one = @One(select = "cn.taotao.dao.DepartDao.selectByPrimaryKey")),
           
    })
    User selectByPrimaryKey(Integer id);

4. 在departdao里 建立注解

  @Select("select * from tbl_depart where departid = #{value}")
  Depart selectByPrimaryKey(Integer departid);

5. 测试方法

 

posted @ 2022-04-02 15:04  琴声清幽  阅读(109)  评论(0编辑  收藏  举报