Mybatis一对多和多对一处理

一对多和多对一处理

多对一的处理

  1. 按查询嵌套处理

编写对应的Mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.StudentMapper">
    <!--id:指定重写方法的名字
        resultMap:定义查询结果集的名字
    -->
    <select id="getStudents" resultMap="stu_tea">
        select * from student;
    </select>
    <!--id:指定对应的resultMap
        type:配置文件里是扫包配置,所以可以小写Student类
    -->
    <resultMap id="stu_tea" type="student">
    <!--association:关联对应的类
        property:上文中Student类里的属性
        column:数据库对应的列名
        javaType:通过属性对应的类
        select:定义嵌套子查询的名字-->
        <association property="teacher" column="tid" javaType="Teacher" select="getT"/>
    </resultMap>
    <!--执行指定子查询-->
    <select id="getT" resultType="Teacher">
      <!--{tid} 大括号中的tid可以写任意值,
      上文中通过列名,属性及对应的类已经判断出该id关联表的外键 -->
        select * from teacher where id=#{tid}
    </select>
</mapper>
  1. 按结果嵌套处理
    <!--重写接口方法,定义结果集名字,书写完整sql语句-->
    <select id="getStudents2" resultMap="stu_tea2">
        select s.id sid,s.name sname,t.name tname
        from student s,teacher t
        where s.tid = t.id
    </select>
    <!--指定结果集,关联指定类的属性prperty对应的查询列名column-->
    <resultMap id="stu_tea2" type="student">
        <id property="id" column="sid"/>
        <result property="name" column="sname"/>
        <!--关联对象property 关联对象在Student实体类中的属性-->
        <association property="teacher" javaType="teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

一对多处理

  1. 按结果嵌套处理
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.TeacherMapper">
    <!--按结果嵌套处理-->
    <select id="getTeachers" resultMap="tea_s">
        select s.id sid,s.name sname,t.name tname,t.id tid,s.tid stid
        from student s,teacher t
        where s.tid = t.id and t.id=#{tid}<!--运行代码中的参数传递-->
    </select>
    <!--
  思路:
      1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
      2. 对查询出来的操作做结果集映射
          1. 集合的话,使用collection!
              JavaType和ofType都是用来指定对象类型的
              JavaType是用来指定pojo中属性的类型
              ofType指定的是映射到list集合属性中pojo的类型。
  -->
    <resultMap id="tea_s" type="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
          	<result property="tid" column="stid"/>
        </collection>
    </resultMap>

</mapper>
  1. 按查询嵌套处理
 <!--按查询嵌套处理-->
    <select id="getTeachers2" resultMap="tea_s2">
        select * from teacher where id =#{teaid}<!--运行代码中传入的参数-->
    </select>
    <resultMap id="tea_s2" type="teacher">
        <!--column是一对多的外键 , 写的是一的主键的列名-->
        <collection property="students" javaType="ArrayList"
                    ofType="student" select="get12" column="id"/>
    </resultMap>
        <select id="get12" resultType="stuedent">
            select * from student where tid=#{id}
        </select>

小结

  1. 关联-association

  2. 集合-collection

  3. 所以association是用于一对一和多对一,而collection是用于一对多的关系

  4. JavaType和ofType都是用来指定对象类型的

  • JavaType是用来指定pojo中属性的类型
  • ofType指定的是映射到list集合属性中pojo的类型。

注意说明:

  1. 保证SQL的可读性,尽量通俗易懂

  2. 根据实际要求,尽量编写性能更高的SQL语句

  3. 注意属性名和字段不一致的问题

  4. 注意一对多和多对一 中:字段和属性对应的问题

  5. 尽量使用Log4j,通过日志来查看自己的错误

猜你喜欢

转载自www.cnblogs.com/tianxintang/p/13391554.html