mybatis一对多

一、colleciton 标签

Mybatis的 collection 是一对多的使用的, 在 resultMap 标签内使用当一个Bean中有 一个list属性需要关联查询出来的使用就用collection 标签

package com.q.bena;

public class ClassInfo {
    private Integer cid;
    private String cname;
    private String cinfo;
    List<Student> student;
    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCinfo() {
        return cinfo;
    }

    public void setCinfo(String cinfo) {
        this.cinfo = cinfo;
    }
    public List<Student> getStudent() {
        return student;
    }
 
    public void setStudent(List<Student> student) {
        this.student = student;
    }


    @Override
    public String toString() {
        return "ClassInfo{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                ", cinfo='" + cinfo + '\'' +
                ",student=" + student +
                '}';
    }
}
package com.q.bena;

public class Student {
    private Integer sid;
    private String sname;
    private String sex;
    private String phone;
    private String address;
    private ClassInfo classInfo;

    public Integer getSid() {
        return sid;
    }

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

    public String getSname() {
        return sname;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public ClassInfo getClassInfo() {
        return classInfo;
    }

    public void setClassInfo(ClassInfo classInfo) {
        this.classInfo = classInfo;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", classInfo=" + classInfo +
                '}';
    }
}

接口

package com.q.dao;
 
import com.q.bean.ClassInfo;
 
public interface ClassInfoDao {
 
    ClassInfo selectByCid(int cid);
 
}
package com.q.dao;
 
import com.q.bean.Student;
 
import java.util.List;
 
public interface StudentDao {
 
    Student selectBySid(int sid);
 
    List<Student> selectByCid(int cid);
}

配置文件 ClassInfo

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.q.dao.ClassInfoDao">
    <resultMap type="com.q.dao.ClassInfoDao" id="classInfoMap">
 
        <id column="cid" property="cid"/>
        <result column="cname" property="cname"/>
        <result column="cinfo" property="cinfo"/>
 
        <!--配置一对多关系:自动根据cid去关联查询Student表中该cid的信息-->
        <collection column="cid" property="student" select="com.gao.dao.StudentDao.selectByCid"></collection>
 
    </resultMap>
    <select id="selectByCid" resultMap="classInfoMap" parameterType="int">
        select * from classInfo where cid=#{cid};
     </select>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.q.dao.EmpDao">
 
    <select resultType="student" parameterType="int" id="selectBySid">
        select * from student where sid=#{sid}
    </select>
 
    <select resultType="student" parameterType="int" id="selectByCid">
        select * from student where cid=#{cid}
    </select>
 
</mapper>

测试

package com.q.test;
 
import com.q.bean.ClassInfo;
import com.q.dao.ClassInfoDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import java.io.IOException;
import java.io.InputStream;
 
public class ClassInfoTest {
 
    InputStream stream=null;
    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    ClassInfoDao classInfoDao=null;
 
    @Before
    public void init() throws IOException {
        stream= Resources.getResourceAsStream("mybatis.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
        sqlSession=factory.openSession();
        classInfoDao=sqlSession.getMapper(ClassInfoDao.class);
    }
 
    @Test
    public void testSelectByCid(){
        ClassInfo classInfo = classInfoDao.selectByCid(1);
        System.out.println(classInfo);
    }
 
    @After
    public void distory() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();
    }
}
package com.gao.test;
 
import com.q.bean.Student;
import com.q.dao.ClassInfoDao;
import com.q.dao.StudentDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import java.io.IOException;
import java.io.InputStream;
 
public class StudentTest {
 
    InputStream stream=null;
    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    StudentDao studentDao=null;
 
    @Before
    public void init() throws IOException {
        stream= Resources.getResourceAsStream("mybatis.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
        sqlSession=factory.openSession();
        studentDao=sqlSession.getMapper(StudentDao.class);
    }
 
    @Test
    public void testSelectBySid(){
        Student student = studentDao.selectBySid(1);
        System.out.println(student);
    }
 
    @After
    public void distory() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值