zoukankan      html  css  js  c++  java
  • Mybatis中mapper.xml的使用

    详解多对多,mybatis多对多查询(xml方式和注解方式)

    链接:https://blog.csdn.net/qq_42524262/article/details/98383977

    链接:https://blog.csdn.net/Mr_wangr/article/details/97920802

    类里面attribute和property区别
        attribute为数据成员
        property是get/set方法后面那个词,比如getMyName(),就是MyName。

    typeAliases元素:

    1,这个标签里面的typeAlias标签可以定义别名,会在映射文件中使用。如果没有定义别名,那必须使用【全限定名】。

    2,其【子标签package】可以为包下所有类定义别名。
            别名默认就是类名。

    【自动生成主键】:

    a.对于主键值可以自动增长的数据库,例如mysql  可以如下:

    <insert id="insertStudent" parameterType="Student"  useGeneratedKeys="true"  keyProperty="studId">表示主键为studId,且自动增长

    b.对于主键值不能够自动增长的数据库,例如oracle,使用【序列】(SEQUENCE)来生成主键值。

    create sequence my_seq;

    <insert id="insertStudent" parameterType="Student">
                <selectKey keyProperty="studId" resultType="int" order="BEFORE">
                      SELECT my_seq.nextval FROM DUAL
                </selectKey>
                INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL, PHONE)
                    VALUES(#{studId},#{name},#{email},#{phone})
            </insert>

    使用了<selectKey>子元素来生成主键值,并将值保存到Student对象的studId 属性上。

    order属性:MySQLorder需要设置为after,Oracle需要设置为before

    ResultMap属性:结果集映射,用来将SELECT语句的结果集映射到java对象的属性中。ResultMaps特性非常强大,你可以使用它将简单的SELECT语句映射到复杂的一对一、一对多关系的SELECT语句上。

    拓展 ResultMap:

    <resultMap type="Student" id="StudentResult">

    <.......>

    <resultMap type="Student" id="StudentWithAddressResult" extends="StudentResult">

    <........>

    其中id为StudentWithAddressResult的resultMap拓展了id为StudentResult的resultMap

    使用【嵌套结果ResultMap】一对一

    Association属性:

    <resultMap type="Address" id="AddressResult">

    <....>

    </resultMap>

    <resultMap type="Student" id="StudentWithAddressResult">
            <....>
                <!-- 【关联】的意思 【嵌套结果映射】-->
                <association property="address" resultMap="AddressResult" />
    </resultMap>

    元素<association>被用来导入“有一个”(has-one)类型的关联。在上述的例子中,我们使用了<association>元素引用了另外的在同一个XML文件中定义的<resultMap>。       

    Collection属性:将多行结果映射成一个对象的一个集合

    private List<Course> courses;

    <!-- collection表一对多关系,遇到courses集合成员 -->
    <collection property="courses" resultMap="CourseResult" />

    使用【嵌套查询select】一对一

    嵌套查询本质:
            一个select语句 转化成 多条select语句去实现功能。

    【先根据addr_id去查找 地址对象】

    <resultMap type="Address" id="AddressResult"></....>

    <select id="findAddressById" parameterType="int" resultMap="AddressResult">
                select * from addresses where addr_id=#{id}
      </select>

    <resultMap type="Student" id="StudentWithAddress">

      <........>

      <association property="address" column="addr_id" select="findAddressById" />
     </resultMap>

    在此方式中,<association>元素的select属性被设置成了id为findAddressById的语句。这里,两个分开的SQL语句将会在数据库中分别执行,第一个调用findStudentById加载student信息,而第二个调用findAddressById来加载address信息。
        
        addr_id列的值将会被作为输入参数传递给findAddressById语句。

    总结:
        嵌套结果查询 本质上是一条sql语句查询多张表;
        嵌套查询 本质上是每条sql语句查询一张表,组合在一起查询多张表。
        效率上,嵌套结果更快。

    结论:
        如果是 嵌套结果,通过 association标签中的 resultMap属性 实现;
        如果是 嵌套查询,通过 association标签中的 select属性 实现

    嵌套结果映射和嵌套查询  一对多  和  一对一  类似,只是运用collection属性而已

    多对多的关系可以参照一对多,只是中间多了一个桥表。

    不论是一对一还是一对多还是多对多,都不能在mybatis中进行级联保存、更新、删除,我们需要使用sql语句控制每一步操作(可以级联查询)

  • 相关阅读:
    Leetcode Unique Binary Search Trees
    Leetcode Decode Ways
    Leetcode Range Sum Query 2D
    Leetcode Range Sum Query
    Leetcode Swap Nodes in Pairs
    Leetcode Rotate Image
    Leetcode Game of Life
    Leetcode Set Matrix Zeroes
    Leetcode Linked List Cycle II
    CF1321A
  • 原文地址:https://www.cnblogs.com/wskb/p/10939486.html
Copyright © 2011-2022 走看看