zoukankan      html  css  js  c++  java
  • Mybatis 多表间查询之 一对多

    前期准备见 Mybatis 多表间查询之 多对一

    0.准备

    1. domain Category

    2. 主配置文件

    ...
     </environments>
        <mappers>
            <mapper resource="cn/ccut/mapper/CategoryDao.xml"  ></mapper>
        </mappers>
    </configuration>
    

    3. 接口ICategoryDao

    public interface ICategoryDao {
        Category getCate(String cid);
    }
    
    

    1. 连接查询

    1.1 映射配置文件 CategoryDao.xml

    <?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="cn.ccut.dao.ICategoryDao">
    
        <resultMap id="res1" type="cn.ccut.domain.Category">
            <id property="cid" column="tcid"/>
            <result property="cname" column="tcname"/>
            <result property="desc" column="desc"/>
    
            <collection property="bList" ofType="cn.ccut.domain.Book">
                <id property="bid" column="bid"/>
                <result property="bname" column="bname"/>
                <result property="author" column="author"/>
                <result property="price" column="price"/>
                <result property="press" column="press"/>
                <result property="cid" column="cid"/>
            </collection>
            
        </resultMap>
        
        <select id="getCate" resultMap="res1">
    select t1.cid tcid,t1.cname tcname,t1.desc,t.* from t_book  t join t_category t1 on t.cid=t1.cid and t.cid=#{cid}
        </select>
    </mapper>
    
    

    1.2 测试

    public class tests {
        @Test
        public void fun(){
            ICategoryDao mapper = MyBatisUtil.openSession().getMapper(ICategoryDao.class);
            System.out.println( mapper.getCate("5F79D0D246AD4216AC04E9C5FAB3199E"));
        }
    }
    

    成功

    2. 分步查询

    2.1 CategoryDao.xml

    <?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="cn.ccut.dao.ICategoryDao">
    
        <resultMap id="res1" type="cn.ccut.domain.Category">
            <id property="cid" column="cid"/>
            <collection property="bList" column="cid" ofType="cn.ccut.domain.Book"  select="selectBook">
            </collection>
        </resultMap>
    
        <select id="getCate" resultMap="res1">
    select * from t_Category where cid=#{cid}
        </select>
        
        <select id="selectBook" resultType="cn.ccut.domain.Book">
            select * from t_book where cid=#{c1id}
        </select>
    </mapper>
    
    
    不停的思考,就会不停的进步
  • 相关阅读:
    LoadRunner时间戳函数web_save_timestamp_param
    场景报错Error -27492: "HttpSendRequest" failed, Windows error code=12029 (cannot connect) and retry limit (0) exceeded for URL=""
    nginx配置和测试
    基准测试-jmeter压力测试activeMQ之一环境安装配置
    Windows负载机JVM 远程监控Linux服务器下tomcat
    查看linux机器cpu、内存环境信息
    C语言-重写strupr函数
    Promise(一)
    Zookeeper在Linux平台Java开发环境配置(命令行)
    Zookeeper会话
  • 原文地址:https://www.cnblogs.com/zhenqk/p/13544042.html
Copyright © 2011-2022 走看看