• 多表关联查询


    无字段映射

      

            <!--多表查询-->
            <!--返回的是数据库的字段属性无法和javaBean的属性一一映射-->
            <select id="goodsAndcategory" resultType="java.util.LinkedHashMap">
                SELECT g.*,c.category_name FROM t_goods AS g , t_category AS c
                    WHERE g.category_id = c.category_id limit 5
            </select>
        public void selectGoodsAndCategory() throws Exception {
            SqlSession sqlSession = null;
            try {
                //获取sql对象
                sqlSession = MybatisUtils.openSession();
                //执行sql
                List<Map> list = sqlSession.selectList("goods.goodsAndcategory");
                for (Map map:list){
                    System.out.println(map);
                }
                //查看连接状态
                Connection conn = MybatisUtils.getConnection(sqlSession);
    
            }catch (Exception e){
                throw e;
            }finally {
                MybatisUtils.release(sqlSession);
            }
        }

    字段一一映射javabean 多表查询

    <!--多表查询-->
            <!--resultMap根据javaBean一一映射-->
            <!--select中resultMap的名称和resultMap的id名称一一映射,type是数据传输对象-->
            <resultMap id="goodsResultMap" type="com.imooc.mybatis.dto.GoodsDto">
                <!--id主键property是javaBean对象属性映射,column对数据库字段映射-->
                <!--column数据库字段映射到property上-->
                <id property="good.goodsId" column="goods_id"/>
                <result property="good.title" column="title"/>
                <result property="good.categoryId" column="category_id"/>
                <result property="good.subTitle" column="sub_title"/>
                <result property="good.originalCost" column="original_cost"/>
                <result property="good.currentPrice" column="current_price"/>
                <result property="good.discount" column="discount"/>
                <result property="good.isFreeDelivery" column="is_free_delivery"/>
    
                <result property="category.categoryId" column="category_id"/>
                <result property="category.categoryName" column="category_name"/>
                <result property="category.categoryLevel" column="category_level"/>
            </resultMap>
            <select id="jointQuery" resultMap="goodsResultMap">
                select g.goods_id,g.title,g.sub_title,g.original_cost,g.current_price,g.discount,g.is_free_delivery,
                c.category_name,c.category_level from t_goods as g, t_category as c where g.category_id = c.category_id
                limit 5
            </select>
        public void selectGoodsDto() throws Exception {
            SqlSession sqlSession = null;
            try {
                //获取sql对象
                sqlSession = MybatisUtils.openSession();
                //执行sql
                List<GoodsDto> list = sqlSession.selectList("goods.jointQuery");
                for (GoodsDto goodsDto:list){
                    System.out.println(goodsDto.getGood().getTitle());
                    System.out.println(goodsDto.getCategory().getCategoryName());
                    System.out.println(goodsDto.getCategory().getCategoryLevel());
                }
                //查看连接状态
                Connection conn = MybatisUtils.getConnection(sqlSession);
    
            }catch (Exception e){
                throw e;
            }finally {
                MybatisUtils.release(sqlSession);
            }
        }
  • 相关阅读:
    EasyUI的DataGrid 打印导出
    js 验证各种格式类型的正则表达式
    双机热备方案
    使用 IDEA 创建 Maven Web 项目 (异常) Disconnected from the target VM, address: '127.0.0.1:59770', transport: 'socket'
    MySQL 常用函数
    使用 IDEA 创建 Maven Web 项目 (四) 让 WEB 应用跑起来
    使用 IDEA 创建 Maven Web 项目 (二) 搭建 WEB 项目框架
    使用 IDEA 创建 Maven Web 项目 (三) 编写一个简单的 WEB 应用
    Android 开发环境 —— Eclipse 启动时报错:Error when loading the SDK
    使用 EasyUI 创建左侧导航菜单
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/13831515.html
Copyright © 2020-2023  润新知