spring注解SQL注意事项

目前有两个类:机构、职员

package com.common.vo;

public class Org{
public long id; public String name; public String desc;
}

 

package com.common.vo;

public class Employee{

    public  long id;
    public String name;
    public String age; 
    public long orgId;
    public Org org;

}

 

dao接口类:

package com.common.dao;
import com.common.vo.*;

import java.util.List;

public interface commonDao{

    /**查询所有职员及其上司*/
    @Results(id="EmployeeAndOrg",value={
        @Result(column="ID",property="id"),
        @Result(column="NAME",property="name"),
        @Result(column="AGE",property="age"),
        @Result(column="ORGID",property="orgId")
        @Result(property="org",cloumn="{id=orgId}",one=@one(com.common.dao.commonDao.queryOrgById ))
    })
    @Select("<script>select id ,name,arg,orgId from T_EMPLOYEE</script>")
    List <Employee> queryAllEmployee();

    @Select("<script>select id ,name,desc from T_ORG where id=#{id}</script>")
    Org queryOrgById(long id);

    @ResultMap(value="EmployeeAndOrg")
    @Select("<script>select id ,name,arg,orgId from T_EMPLOYEE where id =#{id}</script>")
    Employee queryEmployeeById(long id);
}        

 

注意:关联查询方法queryOrgById中使用了orgId属性,则@results中必须有@Result(cloumn="ORGID",property="orgId"),否则查询的Employee中orgId为0

 

posted @ 2020-10-27 16:21  bud  阅读(248)  评论(0编辑  收藏  举报