mybatis關聯查詢

<resultMap type="com.whty.mytest.entity.People" id="pbaseResultMap">
<result column="people_id" property="people_id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<association property="clothes" column="{prop1=people_id,prop2=name}" javaType="java.util.ArrayList"
resultMap="cbaseResultMap"></association>
</resultMap>

<resultMap type="com.whty.mytest.entity.Clothes" id="cbaseResultMap">
<result column="clothes_id" property="clothes_id"/>
<result column="people_id" property="people_id"/>
<result column="brand" property="brand"/>
<result column="clour" property="clour"/>
</resultMap>

<select id="getPeopleList" resultMap="pbaseResultMap" parameterType="Integer">
select p.*,c.* from test_people p
left join test_clothes c on p.people_id = c.people_id and p.name = c.brand
<if test="_parameter != null and _parameter != ''">
where p.people_id = #{_parameter}
</if>
public class Clothes {

    private Integer clothes_id;
    private String clour;
    private Integer people_id;
    private String brand;

    public String getClour() {
        return clour;
    }
    public void setClour(String clour) {
        this.clour = clour;
    }
    public Integer getPeople_id() {
        return people_id;
    }
    public void setPeople_id(Integer people_id) {
        this.people_id = people_id;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public Integer getClothes_id() {
        return clothes_id;
    }
    public void setClothes_id(Integer clothes_id) {
        this.clothes_id = clothes_id;
    }
}
public class People {

    private Integer people_id;
    
    private String name;
    
    private Integer age;
    
    private String sex;
    
    private String aaaaa;
    
    private List<Clothes> clothes;

    public List<Clothes> getClothes() {
        return clothes;
    }
    public void setClothes(List<Clothes> clothes) {
        this.clothes = clothes;
    }
    public String getAaaaa() {
        return aaaaa;
    }
    public void setAaaaa(String aaaaa) {
        this.aaaaa = aaaaa;
    }
    public People(String name,Integer age){
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getPeople_id() {
        return people_id;
    }
    public void setPeople_id(Integer people_id) {
        this.people_id = people_id;
    }
}
//D層調用方法
public List<People> getPeopleList(String people_id);
create table TEST_PEOPLE
(
  people_id NUMBER not null,
  name      NVARCHAR2(20),
  age       NUMBER,
  sex       NVARCHAR2(20),
  aaaaa     NVARCHAR2(30)
)
create table TEST_CLOTHES
(
  "clothes_ID" NUMBER not null,
  people_id    NUMBER not null,
  brand        NVARCHAR2(20),
  clour        NVARCHAR2(20)
)
相關文章
相關標籤/搜索