欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

MyBatis - 关于一对多(<集合>)和一对多(<关联>)案例的详细信息

最编程 2024-03-05 10:27:46
...
<?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.szh.mybatis.mapper.TeacherMapper"> <!-- 使用insert、update、delete、select标签编写sql语句 --> <resultMap id="teacherAndStudent" type="com.szh.mybatis.model.Teacher"> <id property="id" column="tid"/> <result property="name" column="tname"/> <!-- 复杂的属性,我们要单独处理,对象:association,集合:collection 当使用association时,需要在其中加入javaType指定对象属性的类型 当使用collection时,集合中的泛型类型需要使用ofType来获取 --> <collection property="studentList" ofType="com.szh.mybatis.model.Student"> <id property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <select id="selectTeacherByStudent" resultMap="teacherAndStudent"> select s.id sid,s.name sname,t.name tname,t.id tid from student s join teacher t on s.tid=t.id where t.id=#{tid} </select> </mapper>