zoukankan      html  css  js  c++  java
  • MyBatis4——一对一、一对多关联查询

    关联查询:
    一对一:
    1、业务扩展类
        核心:用resultType指定的类的属性包含多表查询的所有字段。
    2、resultMap
        通过添加属性成员建立两个类之间的连接
    <!--利用resultMap实现一对一  -->
        <select id="queryPersonsByReOnetoOne" parameterType="int" resultMap="person-card-map">
            select p.*,c.* from person p inner join personcard c
            on p.cardid=c.cardid
            where p.id=#{id}
        </select>
        
        <!-- resultMap实现映射 -->
        <resultMap type="person" id="person-card-map">
            <!-- person信息 -->
            <id property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="age" column="age"/>
            <!--一对一时,对象成员使用assocation实现映射,javaType指定该属性的类型  -->
            <association property="card" javaType="PersonCard">
                <id property="cardid" column="cardid"/>
                <result property="cardinfo" column="cardinfo"/>
            </association>
        </resultMap>
     
    一对多:
    <!--一对多关联查询  -->
        <select id="queryClassAndPersons" parameterType="int" resultMap="class-person-map">
            select c.*,p.* from person p
            inner join class c
            on c.classid=p.classid
            where c.classid=#{classid}
        </select>
        
        <!-- 类和表的对应关系 -->
        <resultMap type="class" id="class-person-map">
            <!-- 先配class -->
            <id property="classid" column="classid" />
            <result  property="classname" column="classname" />
            <!-- 配置成员属性。属性类型:jdbcType;属性的元素类型:ofType -->
            <collection property="persons" ofType="Person">
                <id property="id" column="id"/>
                <result property="name" column="name"/>
                <result property="age" column="age"/>
            </collection>
        </resultMap>
       
  • 相关阅读:
    WCF客户端获取服务端异常[自定义异常]
    关于VS2013 Browser Link 新功能
    MVC 单元测试xUnit初探
    ASP.NET MVC4中加入Log4Net日志记录功能
    简洁的MVC思想框架——Nancy(Session的使用)
    iOS开发之快速排序算法
    iOS开发之内购的完整流程
    ios面试数据结构与算法
    iOS开发之读取info.plist配置信息
    iOS开发之ARC与非ARC的设置
  • 原文地址:https://www.cnblogs.com/ghlz/p/12234351.html
Copyright © 2011-2022 走看看