写在前面

很久以前在有道云笔记上写的笔记,打算放弃它了,将笔记迁移到这里来。文章可能还有很多不足,请大家谅解,欢迎大佬提意见。

本文使用到的东西

  1. MyBastis
  2. java
  3. SQL server 2012

1.正文

resultMap标签中的collection-属性映射到其他映射器的查询语句

示例项目文件:

链接:https://pan.baidu.com/s/188tF3W33Y3QAFWKmtYrYjA
提取码:gkou

collection聚集元素用来处理“一对多”的关系,可以将表中的一个属性映射到其他映射器的语句上,并将输出的内容返回到该属性上,本身的sql语句不用为该属性的值操作

语法:

        <collection property="courses" column="sid" select="com.etc.dao.CourseDao.findBySid">    
        </collection>
  • properrty属性:表示要映射到java实体类中的对应属性
  • select属性:为类的全路径.< select>、< update>等标签的id,表示用该标签的属性来处理数据。
  • sid属性:表示将表中sid字段的数据输入到select属性指定的标签查询

实例:

StudentDaoMapper.xml

<mapper namespace="com.etc.dao.StudentDao">
    <resultMap type="Student" id="StudentType">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <collection property="courses" column="sid" select="com.etc.dao.CourseDao.findBySid">    
        </collection>
    </resultMap>
   
    <select id="findById" parameterType="int" resultMap="StudentType">
        select * from student where sid=#{sid}
    </select>
</mapper>

CourseDaoMapper.xml

<mapper namespace="com.etc.dao.CourseDao">
    <resultMap type="Course" id="CourseType">
        <id property="cid" column="cid"/>
        <result property="cname" column="cname"/>
    </resultMap>
 
    <!-- 根据学号,查询该学生对应所有的课程信息 -->
    <select id="findBySid" parameterType="int" resultType="Course" >
           select * from course where cid in
            ( select cid from student_course_relation where sid = #{sid})
    </select>
</mapper>
  1. 示例查询调用StudentDaoMapper中id为findById的语句,实现映射到Student实体类
  2. 由id为findById的语句可知,我们输入int类型数据,按定义的StudentType输出映射结构输出映射的Student表
  3. 当到映射结构中collection 集合映射时,将向com.etc.dao.CourseDao映射器中id为findBySid的语句输入当前sid字段的值,并返回List(Course集合)
  4. 当StudentType输出映射器使用collection 集合映射映射到CourseType输出映射器时,CourseType输出映射器就不能再使用collection 集合映射StudentType输出映射器,否则将出现死循环。

resultMap标签中的association–联合元素映射复杂对象中的属性

示例项目文件:
链接:https://pan.baidu.com/s/1wSgJH2QiamuSgXQ5pl33dA
提取码:r5wl

  • 该项目中使用了sex和person两个数据库表
    sex表
    person表
  • 其中sex的dao映射器使用collection标签,但并未直接映射其他映射器的查询结果,只是映射person的dao映射器中的输出映射格式,还是由自身的sql查询提供字段
  • 其中person的dao映射器使用的是association标签,在标签中定义了association对应java类对象中属性和字段的映射,由自身sql查询提供字段

association复杂类型联合;许多查询结果合成这个类型的属性,与collection映射到其他映射器的查询语句上查询不同,association是自身查询,由自身的查询语句的字段值映射到属性,所以查询语句一般比较复杂,包含多表联合查询。

语法:

        <association property="sex" javaType="Sex">
            <id property="sexid" column="sexid"/>
            <result property="sexname" column="sexname"/>        
        </association>
  • property属性:表示java实体类中映射的属性名
  • javaType表示该标签对应的属性类型,这里是别名,如果没有在MyBatis-config.xml中配置别名则需要填写该类的全路径
  • 其他中的配置和映射器.xml文件详解中的相同property表java类中的属性,column表示

PersonDaoMapper.xml

<mapper namespace="com.etc.dao.PersonDao">
   
    <resultMap type="Person" id="PersonType">
        <id property="pid" column="pid"/>
        <result property="pname" column="pname"/>
        <result property="age" column="age"/>
        <!--n端通过association配置1端的关联
        <association property="sex" resultMap="com.etc.dao.SexDao.SexType">     
        </association>
        -->
        <!-- 在关联对象,直接书写全部的映射map -->
        <association property="sex" javaType="Sex">
            <id property="sexid" column="sexid"/>
            <result property="sexname" column="sexname"/>        
        </association>
    </resultMap>  
   
    <select id="findById" parameterType="int"  resultMap="PersonType">
        select person.*,sex.sexname from person,sex where pid=#{pid} and person.sexid = sex.sexid
    </select>
</mapper>
  1. 示例查询调用PersonDaoMapper中id为findById的查询语句的,该查询语句联合查询了person和sex两个表
  2. 该查询语句返回的字段包含了上面所有用到的字段,其中sexid和sexname两个字段集合映射到一个sex对象

resultMap标签中的collection-属性映射到其他映射器的输出映射

SexDaoMapper.xml

<mapper namespace="com.etc.dao.SexDao">
    <resultMap type="Sex" id="SexType">
        <id property="sexid" column="sexid"/>
        <result property="sexname" column="sexname"/>
        <!-- 1端通过collection配置n端的关联 -->
        <collection property="persons" resultMap="com.etc.dao.PersonDao.PersonType">        
        </collection>
    </resultMap>
   
    <select id="findById" parameterType="int" resultMap="SexType">
        select person.*,sex.sexname from person,sex where person.sexid=#{sexid} and person.sexid = sex.sexid
    </select>
</mapper>

该映射器使用collection聚集元素来处理,但是只使用resultMap属性将值指向com.etc.dao.PersonDao文件id为PersonType的输出映射,并未直接映射输出查询,所以自身的查询还要负责给collection中的属性配置字段

resultMap标签中的association–联合元素映射其他映射器的查询语句

示例项目文件:
链接:https://pan.baidu.com/s/1oWQznjhPRQ-_yiaUE-rBKQ
提取码:uxaq

该项目中使用了sex和person两个数据库表
sex表
person表

其中sex的dao映射器使用collection标签,但并未直接映射其他映射器的查询结果,只是映射person的dao映射器中的输出映射格式,还是由自身的sql查询提供字段

PersonDaoMapper.xml

<mapper namespace="com.etc.dao.PersonDao">
    <resultMap type="Person" id="PersonType">
        <id property="pid" column="pid"/>
        <result property="pname" column="pname"/>
        <result property="age" column="age"/>
        <association property="sex" javaType="Sex" column="sexid" select="com.etc.dao.SexDao.findById">    
        </association>
    </resultMap>  
    <select id="findBySexid" parameterType="int" resultType="Person">
        select * from person where sexid=#{sexid}
    </select>  
    <select id="findById" parameterType="int"  resultMap="PersonType">
        select * from person where pid=#{pid}
    </select> 
</mapper>

SexDaoMapper.xml

<mapper namespace="com.etc.dao.SexDao">
    <resultMap type="Sex" id="SexType">
        <id property="sexid" column="sexid"/>
        <result property="sexname" column="sexname"/>
        <collection property="persons" column="sexid" select="com.etc.dao.PersonDao.findBySexid">      
        </collection>
    </resultMap>
   
    <select id="findById" parameterType="int" resultMap="SexType">
        select * from sex where sexid=#{sexid}
    </select>
</mapper>
  1. 示例查询调用SexDaoMapper中id为findById的语句,实现映射到Sex实体类
  2. 由id为findById的语句可知,我们输入int类型数据,按定义的StudentType输出映射结构输出映射的Sex表
  3. 当到映射结构中collectionl联合映射时,将向com.etc.dao.SexDao映射器中id为findBySid的语句输入当前sid字段的值,并返回List(Sex集合)
  4. 当PersonType输出映射器使用collection 集合映射映射到SexType输出映射器时,SexType输出映射器就不能再使用collection 集合映射PersonType输出映射器,否则将出现死循环。
  5. 和第一个标题一的resultMap标签中的collection-属性映射到其他映射器的查询语句非常类似

2.总结

很久以前在有道云笔记上写的笔记,打算放弃它了,将笔记迁移到这里来。有不清楚的地方欢迎评论留言,看到的我都会回复的。本文到此结束,有什么不足的地方请大家不吝指正。