Mybatis入门——实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis入门——实例相关的知识,希望对你有一定的参考价值。

Mybatis入门(三)

数据库

技术分享

public class Student {

    private String sno;
     public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getsName() {
        return sName;
    }
    public void setsName(String sName) {
        this.sName = sName;
    }
    public String getsSex() {
        return sSex;
    }
    public void setsSex(String sSex) {
        this.sSex = sSex;
    }
    public String getsAge() {
        return sAge;
    }
    public void setsAge(String sAge) {
        this.sAge = sAge;
    }
    public String getsDept() {
        return sDept;
    }
    public void setsDept(String sDept) {
        this.sDept = sDept;
    }
    public String sName;
     public String sSex;
     public String sAge;
     public String sDept;
     public String getcId() {
        return cId;
    }
    public void setcId(String cId) {
        this.cId = cId;
    }
    private String cId;
    public Student(String sno, String sName, String sSex, String sAge,
            String sDept,String cId) {
        super();
        this.sno = sno;
        this.sName = sName;
        this.sSex = sSex;
        this.sAge = sAge;
        this.sDept = sDept;
        this.cId=cId;
    }
    public Student() {
        super();
    }
    @Override
    public String toString() {
        return "Student [sno=" + sno + ", sName=" + sName + ", sSex=" + sSex
                + ", sAge=" + sAge + ", sDept=" + sDept + ", cId=" + cId + "]";
    }

}
public class Classes {

    private String cId;
    public String getcId() {
        return cId;
    }
    public void setcId(String cId) {
        this.cId = cId;
    }
    public String getcName() {
        return cName;
    }
    public void setcName(String cName) {
        this.cName = cName;
    }
    private String cName;
    public Classes(String cId, String cName) {
        super();
        this.cId = cId;
        this.cName = cName;
    }
    public Classes() {
        super();
    }
    @Override
    public String toString() {
        return "Classes [cId=" + cId + ", cName=" + cName + "]";
    }
    

}
public class Teacher {

private String tId;
public String gettId() {
    return tId;
}
public void settId(String tId) {
    this.tId = tId;
}
public String gettName() {
    return tName;
}
public void settName(String tName) {
    this.tName = tName;
}
private String tName;
public Teacher(String tId, String tName) {
    super();
    this.tId = tId;
    this.tName = tName;
}
public Teacher() {
    super();
}
@Override
public String toString() {
    return "Teacher [tId=" + tId + ", tName=" + tName + "]";
}

}
 

实例(一)

查询每个班级及其老师的详细信息。

1.创建ClassTeacher.java

public class ClassTeacher {
private Classes classes;
private List<Teacher> teaList;

public Classes getClasses() {
    return classes;
}
public void setClasses(Classes classes) {
    this.classes = classes;
}
public List<Teacher> getTeaList() {
    return teaList;
}
public void setTeaList(List<Teacher> teaList) {
    this.teaList = teaList;
}
public ClassTeacher( Classes classes, List<Teacher> teaList) {
    super();

    this.classes = classes;
    this.teaList = teaList;
}
public ClassTeacher() {
    super();
}
@Override
public String toString() {
    return "ClassTeacher [ classes=" + classes + ", teaList="
            + teaList + "]";
}
}

2.创建classTeacherMapping.xml

<?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.bean.classTeacherMapping">
    <select id="getClassTeachInfor"  resultMap="getClassTeaches">
        select * from teacher s,classes c,teaches t where s.t_id =t.t_id and c.c_id=t.c_id;
    </select>

    <resultMap type="com.bean.ClassTeacher" id="getClassTeaches">
        <id column="c_id"/>
        <association property="classes" javaType="com.bean.Classes">
            <id property="cId" column="c_id"/>
            <result property="cName" column="c_name"/>

        </association>
        <collection property="teaList" ofType="com.bean.Teacher">
            <id property="tId" column="t_id"/>
            <result property="tName" column="t_name"/>
        </collection>
    </resultMap>
</mapper>

3.查询结果。

String classTeacherMap="com.bean.classTeacherMapping";
        String classTeacherSelect=classTeacherMap+".getClassTeachInfor";
        List<ClassTeacher> ctList=sqlSession.selectList(classTeacherSelect);
        System.out.println(ctList);

 

以上是关于Mybatis入门——实例的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 详解------入门实例(基于XML)

Mybatis入门实例——使用MyBatis Generator生成DAO(转载http://qiuqiu0034.iteye.com/blog/1163026)

mybatis 详解------入门实例(基于注解)

mybatis快速入门

mybatis快速入门,mybatis简单实例, 如何使用mybatis

Mybatis入门——实例