zoukankan      html  css  js  c++  java
  • 一对多处理

    一对多处理

    按照查询嵌套处理

    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性需要单独处理   对象:association   集合:collection-->
        <collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId"/>
    </resultMap>
    
    <select id="getStudentByTeacherId" resultType="Student">
        select * from student where tid = #{tid}
    </select>
    <select id="getTeacher" resultMap="TeacherStudent">
        select * from teacher where id = #{tid}
    </select>
    

    按照结果嵌套处理

    <resultMap id="TeacherStudent2" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--复杂的属性需要单独处理   对象:association   集合:collection-->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
    
    <select id="getTeacher2" resultMap="TeacherStudent2">
        select s.id sid, s.name sname, t.name tname, t.id tid
        from student s, teacher t
        where s.tid = t.id and t.id = #{tid}
    </select>
    

    小结

    1. 关联-association [多对一]
    2. 集合-collection [一对多]
    3. javaType : 用来指定实体类中属性的类型
    4. ofType : 用来指定映射到List或者集合中的pojo类型, 泛型中的约束类型

    注意

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

    • 注意一对多和多对一中, 属性名和字段的问题

    • 善用日志排查问题

  • 相关阅读:
    Hibernate与数据库的触发器协同工作
    Hibernate的调用数据库的存储过程
    hibernate中持久化对象的状态
    Hibernate-sessio缓存的操作
    Hibernate中的一些关键字理解
    配置Hibernate的流程
    Struts2自定义拦截器
    Struts2中解决表单重复提交
    Struts文件下载(静态)
    Struts2的简单的文件上传
  • 原文地址:https://www.cnblogs.com/pinked/p/12178026.html
Copyright © 2011-2022 走看看