MyBatis多对多关联映射创建示例

网友投稿 239 2022-07-26


目录示例【通过班级查询老师信息】

示例

【通过班级查询老师信息】

创建t_classes

创建t_classessTeacher

创建t_teacher

创建Classes

package com.po;

import java.util.List;

public class Classes {

private Integer cid;

private String cname;

private List teachers;

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 List getTeachers() {

return teachers;

}

public void setTeachers(List teachers) {

this.teachers = teachers;

}

@Override

public String toString() {

return "Classes [cid=" + cid + ", cname=" + cname + ", teachers=" + teachers + "]";

}

}

创建Teacher

package com.po;

import java.util.List;

public class Teacher {

private Integer tid;

private String tname;

private List classes;

public Integer getTid() {

return tid;

}

public void setTid(Integer tid) {

this.tid = tid;

}

public String getTname() {

return tname;

}

public void setTname(String tname) {

this.tname = tname;

}

public List getClasses() {

return classes;

}

public void setClasses(List classes) {

this.classes = classes;

}

@Override

public String toString() {

return "Teacher [tid=" + tid + ", tname=" + tname + "]";

}

}

创建ClassesMapper.xml

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from t_classes c,t_teacher t,t_classessTeacher ct

where ct.cid=c.cid

and ct.tid=t.tid

and c.cid=#{cid}

配置mybatis-config.xml

创建ClassesDao

package com.dao;

import org.apache.ibatis.session.SqlSession;

import org.junit.Test;

import com.factory.SessionFactory;

import com.po.Classes;

pubhttp://lic class ClassesDao {

private SqlSession session=SessionFactory.getSqlSession();

@Test

public void findClassesWithTeacher() {

Classes classess=session.selectOne("com.mapper.ClassesMapper.findClassesWithTeacher", 1);

System.out.println(classess);

}

}

执行 findClassesWithTeacher()方法

以上就是MyBatis多对多关联映射创建示例的详细内容,更多关于MyBatis多对多关联映射的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis条件if test如何使用枚举值
下一篇:Java实现飞机小游戏
相关文章

 发表评论

暂时没有评论,来抢沙发吧~