详解mybatis多对一关联查询的方式

根据ID查询学生信息,要求该学生的教师和班级信息一并查出

第一种关联方式

1.修改实体类Student,追加关联属性,用于封装关联的数据

修改完以后重新生成get set方法还有toString方法

private Teacher teacher;private Classes classes;

2.修改TeacherMapper相关配置

1.接口类 增加

Teacher selectTeacherById(Integer tid);

2.xml映射文件 增加

<sql id="params">tid,tname</sql><select id="selectTeacherById" resultType="Teacher">    select    <include refid="params"></include>    from teacher where tid=#{tid}</select>

3.修改ClassesMapper相关配置

1.接口类 增加

Classes selectClassesById(Integer cid);

2.xml映射文件 增加

        <resultMap type="Classes" id="clsMap">  <id property="cid" column="cid"></id>  <result property="cname" column="cname"/> </resultMap> <select id="selectClassesById" resultMap="clsMap">  select * from classes where cid = #{cid} </select>

4.修改StudentMapper相关配置

1.接口类 增加

Student selectStudentById(Integer sid);

2.xml映射文件 增加

ps:

多对一关联属性配置:

property:关联对象名

javaType:关联对象类型

select:引用的关联查询sql对应id名

column:引用的关联查询sql语句需要的参数

        <resultMap type="Student" id="stuMap">  <id property="sid" column="sid"/>  <result property="sname" column="sname"/>  <result property="age" column="age"/>  <result property="email" column="email"/>  <association property="teacher" select="com.yc.dao.TeacherMapper.selectTeacherById" column="tid" javaType="Teacher"></association>  <association property="classes" select="com.yc.dao.ClassesMapper.selectClassesById" column="cid" javaType="Classes"></association> </resultMap>        <select id="selectStudentById" resultMap="stuMap">  select * from student where sid = #{sid} </select>

5.测试代码

        @Test public void test1() {  Student stu = studentMapper.selectStudentById(100001);  System.out.println(stu); }

第二种配置方式

1.修改实体类Student,追加关联属性,用于封装关联的数据

跟前面一样的

2.修改studentMapper.xml映射文件

ps:接口里面的方法还是跟原来一样的

        <resultMap type="Student" id="stuMap">  <id property="sid" column="sid" />  <result property="sname" column="sname" />  <result property="age" column="age" />  <result property="email" column="email" />  <association property="teacher" javaType="Teacher">   <id property="tid" column="tid"></id>   <result property="tname" column="tname" />  </association>  <association property="classes" javaType="Classes">   <id property="cid" column="cid"></id>   <result property="cname" column="cname" />  </association> </resultMap> <select id="selectStudentById" resultMap="stuMap">  select * from student s,teacher t,classes c  where s.sid=#{sid} and s.tid = t.tid and s.cid=c.cid </select>

小结一下:个人感觉第二种方法是会简单许多,只是说,因为查询语句中,连接条件的限制,当cid或者tid为空时,查询到的数据就会是null,而第一种的话,如果只是cid为空,至少还是会显示student和teacher的相关信息

第三种配置方式 全局映射

ps:student类和studentmapper接口类不变

1.修改全局配置文件的setting信息

<setting name="autoMappingBehavior" value="FULL" /><setting name="mapUnderscoreToCamelCase" value="true" />  

2.修改studentMapper.xml映射文件

ps:

1.实体类的属性名要和表字段名保存一致,或按照驼峰命名规则(属性名:aColumn,字段名:A_COLUMN),settion属性mapUnderscoreToCamelCase设置成true

2.关联表的字段名不能有相同的名字

        <resultMap type="Student" id="stuMap">  <association property="teacher" javaType="Teacher" />  <association property="classes" javaType="Classes" /> </resultMap> <select id="selectStudentById" resultMap="stuMap">  select * from student s,teacher t,classes c where s.sid = #{sid} and  s.tid=t.tid  and s.cid=c.cid </select>

还是连接查询为空的问题~

总结

到此这篇关于mybatis多对一关联查询的文章就介绍到这了,更多相关mybatis多对一关联查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

请让我们从容面对这离别之后的离别。

详解mybatis多对一关联查询的方式

相关文章:

你感兴趣的文章:

标签云: