博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis学习笔记三(关联关系)
阅读量:6588 次
发布时间:2019-06-24

本文共 4653 字,大约阅读时间需要 15 分钟。

  学习mybatis的关联关系,主要注解在代码上,这里不做解释。配置文件一样的就不贴了

1.关联关系表创建(学生对应老师 多对一)

  学生老师表

 

 2.表对应的实体类

package com.home.entity;/** * 此类是:学生 * @author hpc * @2017年1月14日下午7:06:33 */public class Student {    private Integer student_id;//学生id    private String student_name;//学生名字    private Integer student_number;//学生编号    private Teacher student_teacher;//是哪个教师的学生    public Integer getStudent_id() {        return student_id;    }    public void setStudent_id(Integer student_id) {        this.student_id = student_id;    }    public String getStudent_name() {        return student_name;    }    public void setStudent_name(String student_name) {        this.student_name = student_name;    }    public Integer getStudent_number() {        return student_number;    }    public void setStudent_number(Integer student_number) {        this.student_number = student_number;    }    public Teacher getStudent_teacher() {        return student_teacher;    }    public void setStudent_teacher(Teacher student_teacher) {        this.student_teacher = student_teacher;    }    @Override    public String toString() {        return "Student [student_id=" + student_id + ", student_name=" + student_name + ", student_number="                + student_number + ", student_teacher=" + student_teacher + "]";    }}
package com.home.entity;import java.util.HashSet;import java.util.Set;/** * 此类是:老师 * @author hpc * @2017年1月14日下午7:06:47 */public class Teacher {    private Integer teacher_id;// 老师id    private String teacher_name;// 老师名字    private Integer teacher_number;// 老师编号    private Set
teacher_student = new HashSet
();// 老师教过哪些学生 public Integer getTeacher_id() { return teacher_id; } public void setTeacher_id(Integer teacher_id) { this.teacher_id = teacher_id; } public String getTeacher_name() { return teacher_name; } public void setTeacher_name(String teacher_name) { this.teacher_name = teacher_name; } public Integer getTeacher_number() { return teacher_number; } public void setTeacher_number(Integer teacher_number) { this.teacher_number = teacher_number; } public Set
getTeacher_student() { return teacher_student; } public void setTeacher_student(Set
teacher_student) { this.teacher_student = teacher_student; } @Override public String toString() { return "Teacher [teacher_id=" + teacher_id + ", teacher_name=" + teacher_name + ", teacher_number=" + teacher_number + ", teacher_student=" + teacher_student + "]"; } }

3.核心主要是配置文件

 4. 测试

package com.home.mybatis;import java.io.IOException;import org.apache.ibatis.session.SqlSession;import com.home.entity.Student;import com.home.entity.Teacher;public class TestApp {    public static void main(String[] args) throws IOException {        // 我将获取session的代码封装成了一个工具类,直接用工具类来获取session        SqlSession session = SessionUtils.getSession("mybatis.xml");        String loadTeacher="com.home.entity.teacherMapper.loadTeacher";        String loadStudent="com.home.entity.studentMapper.loadStudent";        Teacher teacher = session.selectOne(loadTeacher,1);        Student student = session.selectOne(loadStudent,1);        System.out.println(student);        System.out.println(teacher);        // 将数据属性到数据中去        session.commit();        // 关闭session        session.close();    } }

5.结果

Student [student_id=4, student_name=hpc, student_number=1, student_teacher=Teacher [teacher_id=1, teacher_name=王老师, teacher_number=1, teacher_student=[]]]Teacher [teacher_id=1, teacher_name=王老师, teacher_number=1, teacher_student=[Student [student_id=5, student_name=zll, student_number=2, student_teacher=null], Student [student_id=4, student_name=hpc, student_number=1, student_teacher=null]]]

 

转载于:https://www.cnblogs.com/mohehpc/p/6286099.html

你可能感兴趣的文章
struts2的json插件配置详解
查看>>
bootstrap多模态框
查看>>
【翻译】RAID, Auto Volume, HBA, Simple Volume模式之间的区别
查看>>
用函数指针控制排序的顺序
查看>>
IE的@cc_on条件编译
查看>>
tab补全
查看>>
使用JUnit-4.11报java.lang.NoClassDefFoundError:
查看>>
cordova构建项目命令小结
查看>>
IntelliJ IDEA 12 创建Web项目 教程 超详细版
查看>>
linux 远程脚本批量 scopy文件
查看>>
复制虚拟机/vmware中linux系统
查看>>
ASP.NET中的HttpWorkerRequest对像及其应用
查看>>
Nginx+Keepalived实现站点高可用
查看>>
Rails + Bootstrap个人博客搭建的完整过程(4)
查看>>
开源 java CMS - FreeCMS2.7 移动端栏目页静态化
查看>>
DNS使用的是TCP协议还是UDP协议
查看>>
牛津词典 2018 年度词汇 ——「有毒」!
查看>>
五、数据类型转换
查看>>
十二、流程控制之条件运算符
查看>>
python学习笔记之——装饰器及对象
查看>>