zoukankan      html  css  js  c++  java
  • mybatis基础(三)

    一.一对多

      理解:一个父亲有多个儿子

      解决:在父亲的实体类中植入儿子的list集合

      1.单条sql

    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE mapper    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
    
    <mapper namespace="com.happy.test.classMapper">
            <!--单条sql的一对多-->
        <select id="getClass" parameterType="int" resultMap="getClassMap">
            select * from class c, teacher t  where c.teacher_id = t.t_id and c.teacher_id=#{id}
        </select>
        
        <!-- resultMap:映射实体类和字段之间的一一对应的关系 -->
        <resultMap type="Classes" id="getClassMap">
            <id property="id" column="c_id"/>   
            <result property="name" column="c_name"/>
            <association property="teacher" javaType="Teacher">   
                <id property="id" column="t_id"/>
                <result property="name" column="t_name"/>
            </association>
        </resultMap>
    
    
    </mapper>
    View Code

      2.多条sql

    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE mapper    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
    
    <mapper namespace="com.happy.test.classMapper">
        
                    <select id="getClass1" resultMap="getClassMap1">
            select * from class where c_id=#{id}
        </select>
        
        <resultMap type="Classes" id="getClassMap1">
            <id property="id" column="c_id"/>   
            <result property="name" column="c_name"/>
            <association property="teacher" column="teacher_id" select="getTeacher"/>   
        </resultMap>
        <select id="getTeacher" parameterType="int" resultType="Teacher">
            select t_id id,t_name name from teacher where t_id =#{id}
        </select>
    </mapper>
    View Code

    二.一对一

      理解:一个人对应一个身份证号

      解决:在人的实体类中植入身份证的实体

    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE mapper    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
    <mapper namespace="com.yc.mapper.CustomerMapper"> 
     
      <resultMap type="com.yc.m.Customer" id="resultCustomerMap"> 
        <id column="id" jdbcType="INTEGER" property="id" />
        <result property="address" column="address"/> 
        <result property="postcode" column="postcode"/> 
        <result property="sex" column="sex"/> 
        <result property="cname" column="cname"/> 
        <collection property="orders" ofType="com.yc.m.Orders">
              <id property="id" column="id"/>
              <result property="code" column="code"/>
        </collection>
        
      </resultMap> 
        
      <select id="getCustomer" resultMap="resultCustomerMap" > 
        SELECT * FROM t_customer  WHERE id=#{id} 
      </select>      
    </mapper>
    View Code

    三.多对一

      理解:是一对多的变形

      解决:利用一对一的思路来解决问题,即在多的一方实体类中植入少的一方的实体,让其变形为一对一

    四.多对多

      理解:一个学生可以有多个老师,且一个老师可以有多个学生

      解决:无论站在哪一方多可以变形为一对多来看,在按照一对多的方案解决即可

    二.自关联

      即某种事务与其父类属于同种事务,例如人:一个人与他的父亲都属于人,

                    商品分类:一个分类属于另一个分类,且也归属于分类中

    如图:数码下有手机和移动电源,他们都属于分类.

    故通常我们在设计数据库的时候就会将这些分类归属与同一张分类表中

    父类目录id即该分类的父分类为谁,    例如手机的父分类就是数码

     <resultMap id="CategoryMapper" type="Category">
            <id column="cid" property="cid"></id>
            <result column="cname" property="cname"></result>
            <collection property="cates" ofType="Category" select="getChildrenListByPid" column="cid">
            </collection>
            </resultMap>
             <!--查询-->
            <select id="getChildrenListByPid" resultMap="CategoryMapper">
            select * from category where pid=#{pid}
            </select>
    View Code

    使用这种方式需要在实体类中植入一个自己的实体

    这种使用一条sql语句将分类表拆分,在调用sql时调用自身的方式称之为递归(不懂什么时递归的自己再查)

  • 相关阅读:
    【leetcode】Valid Palindrome
    【leetcode】Longest Common Prefix
    【leetcode】Intersection of Two Linked Lists
    【leetcode】Compare Version Numbers
    【leetcode】Pascal's Triangle
    ubuntu 14.04下spark简易安装
    【leetcode】Number of 1 Bits
    【leetcode】LRU Cache
    【leetcode】Container With Most Water
    【leetcode】Roman to Integer
  • 原文地址:https://www.cnblogs.com/wy0119/p/7690032.html
Copyright © 2011-2022 走看看