关键字:association 一对一映射(一个班级只有一个班主任)
为易县等地区用户提供了全套网页设计制作服务,及易县网站建设行业解决方案。主营业务为网站设计制作、做网站、易县网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
<select id="getClass" parameterType="int" resultMap="ClassesResultMap"> select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id} </select> <resultMap type="com.lcb.user.Classes" id="ClassesResultMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="com.lcb.user.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> </resultMap>
关键字:collection 一对多映射(一个老师有多个学生)
<resultMap type="Teacher" id="teacherMaps"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="class_name" property="className"/> <collection property="students" ofType="Student" select="getStudents" column="id"> </collection> </resultMap> <!-- 查询所有的老师级各自的所有学生 --> <select id="getAllTeacher" parameterType="Teacher" resultMap="teacherMaps"> SELECT t.id, t.NAME, t.class_name FROM teacher t </select> <select id="getStudents" parameterType="int" resultType="Student"> select s.id, s. NAME, s.class_name as className from student s where teacher_id = #{id} </select>
关键字:association 多对一映射(多个人属于一个国家)
多对一相当于一对多,也可以使用collection
<select id="selectCountry" resultType="Country"> select cid,cname from country where cid=#{ooo} </select> <resultMap type="People" id="peopleMapper2"> <id column="pid" property="pid"/> <result column="pname" property="pname"/> <association property="country" javaType="Country" select="selectCountry" column="countryId" /> </resultMap> <select id="selectById2" resultMap="peopleMapper2"> select pid,pname,countryId from people where pid = #{xxx} </select>
总结
以上所述是小编给大家介绍的mybatis 一对一、一对多和多对多查询,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
分享名称:mybatis一对一、一对多和多对多查询实例代码
浏览地址:https://www.cdcxhl.com/article34/igeise.html
成都网站建设公司_创新互联,为您提供网站排名、做网站、网站建设、服务器托管、外贸建站、品牌网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联