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;
    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 getStudent() {
        return student;
    }
 
    public void setStudent(List 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 selectByCid(int cid);
}

配置文件 ClassInfo




    
 
        
        
        
 
        
        
 
    
    



 
    
 
    
 

测试

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();
    }
}

你可能感兴趣的:(mybatis,java,mysql)