zoukankan      html  css  js  c++  java
  • 多表关联查询

    无字段映射

      

            <!--多表查询-->
            <!--返回的是数据库的字段属性无法和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);
            }
        }
  • 相关阅读:
    科目2考试最好的网址
    怎么解决tomcat占用8080端口问题
    JDBC全部分析
    JSP分页技术的实现(利用当前页进行前后加减,并利用href进行当前页面传值,传值当然是那个当前值变量)
    mysql 如何创建一个简单的存储过程
    MySQL、SqlServer、Oracle三大主流数据库分页查询 (MySQL分页不能用top,因为不支持)
    JAVA线程操作常见面试题 包括不使用内部类对多个线程加减1
    JAVA常用设计模式(静态化调用和实例化调用的区别,编辑可见 )
    面试王牌 JAVA并发
    不通过ecplise,只通过文件目录 创建最简单的JSP文件
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/13831515.html
Copyright © 2011-2022 走看看