Mybatis注解实现一对多关联查询

bean

package com.bean;

public class Depment {
    public  int did;
    public String dname;

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }
}
package com.bean;

public class Student {
    public int sid;
    public String sname;
    public Depment depment;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Depment getDepment() {
        return depment;
    }

    public void setDepment(Depment depment) {
        this.depment = depment;
    }
}

dao

package com.dao;

import com.bean.Student;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
@Mapper
@Component (value ="studentdao")
public interface Studentdao{
    //查找所有学生
    @Select("select * " +
            "from student a,depment b " +
            "where a.did =b.did")
    @Results ({
            @Result(id=true,column="sid",property="sid"),
            @Result(column="sname",property="sname"),
            @Result(column="did",property="depment",one=@One(select="com.dao.Depmentdao.querydepmentbydid",fetchType= FetchType.EAGER))
    })
    public List<Student> queryallstudnet();
    //根据学号查找学生
    @Select("select * " +
            "from student a,depment b " +
            "where sid =#{sid} and a.did =b.did")
    public Student querystudnetbysid(Student student);
    //删除学生根据学生学号
    @Delete("delect * from student where sid =#{sid}")
    public void delectstudnetbysid(Student student);
    //增加学生
    @Insert("insert INTO student(sname,did) VALUES(#{sname},#{did})")
    public void addstudent(Student student);
}
package com.dao;

import com.bean.Depment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
@Component(value = "depmentdao")
public interface Depmentdao {
    //根据编号查找学院
    @Select("select * from depment")
    public Depment querydepmentbydid(int did);

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值