1. 程式人生 > 其它 >mybatis 註解查詢 ,one to many 從員工查部門

mybatis 註解查詢 ,one to many 從員工查部門

 

庫表中,在部門表中 departId,這個名字需要一致(2個表),用它來建立外來鍵關係。

 

1. 建立基本的springboot專案

2. 其中user   bean,有欄位 depart,(類),不需要有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. 測試方法