一、动态sql

  通过mybatis提供的标签,实现sql语句的拼接。

  1.where

<select id="findUserList" parameterType="user" resultType="user">
        select * from user
               <!--使用where可以自动处理第一个and-->
        <where>
        <if test="id!=null and id!=''">
        and id=#{id}
        </if>
        <if test="username!=null and username!=''">
        and username like '%${username}%'
        </if>
        </where>
</select>

  2.foreach

  场景:当传入参数为一个数组或者集合时,mybatis提供了foreach标签解析

        <if test="ids!=null">
                <!-- 使用foreach遍历传入的ids
                    collection:输入对象中的集合属性
                    item:每次遍历的生成的对象名
                    open:开始遍历时拼接的串
                    close:结束遍历时拼接的串
                    separator:遍历的两个对象中需要拼接的串
                -->
                <!-- 实现以下拼接
                AND(id=1 or id=2 or id=3)
                 -->
                <foreach collection="ids" item="id" open="AND(" close=")" separator="or">
                    id=#{id}
                </foreach>
        </if>

  3.sql片段

  当sql语句重复使用时,我们可以对sql进行抽取,使用时直接引用,提升代码的复用性。

<!-- 传递pojo综合查询用户信息 -->
    <select id="findUserList" parameterType="user" resultType="user">
        select * from user
        <where>
        <if test="id!=null and id!=''">
        and id=#{id}
        </if>
        <if test="username!=null and username!=''">
        and username like '%${username}%'
        </if>
        </where>
    </select>

  对where条件中的sql进行抽取

<sql id="query_user_where">
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>
    <if test="username!=null and username!=''">
        and username like '%${username}%'
    </if>
</sql>

  引用sql

<select id="findUserList" parameterType="user" resultType="user">
        select * from user
        <where>
        <include refid="query_user_where"/>
        </where>
    </select>

二、关联查询(重点)

resultType和resultMap

  resultType指定输出结果类型,可以是基本类型,也可以 是pojo对象。

  mapper.xml文件

    <!-- 根据id查询用户信息 -->
    <select id="findUserById" parameterType="int" resultType="user">
        select * from user where id = #{id}
    </select>

  resultMap 

  resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功.

  如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。

  

    <!-- 配置resultmap type:为映射结果类型 -->
    <resultMap type="orders" id="user_orders_resultmap">
        <!-- 配置要映射的订单信息 -->
        <!-- id为要查询对象的唯一标识(orders的主键 ) 如果有多个唯一表示都都配置上即可 column:唯一标识的咧 property:为结果类型的属性 -->
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="number" property="number" />
        <result column="createtime" property="createtime" />
        <result column="note" property="note" />
        <!-- 配置要映射的关联用户的信息 -->
        <!-- assocation用来映射关联查询单个对象的信息 property:要将关联的信息映射到Orders中的那个属性 -->
        <association property="user" javaType="cn.mycookies.mybatis.demo.po.User">
            <result column="user_id" property="id" />
            <result column="username" property="username" />
            <result column="sex" property="sex" />
            <result column="address" property="address" />
        </association>
    </resultMap>

1.一对一查询

  场景:查询所有订单信息,关联查询下单用户信息。

注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单

way1:使用resultType定义返回值结果类型 返回结果为Orders的po类 类中有User属性

mapper.xml配置

<select id="findOrdersListResultMap" resultType="Orders">
    SELECT
    orders.*,
    user.username,
    user.address
    FROM
    orders,    user
    WHERE orders.user_id = user.id
</select>

way2:使用resultMap定义返回 结果类型

<!--定义resultMap-->
<resultMap type="orders" id="user_orders_resultmap">
    <!--配置映射的订单信息-->
  <!-- id为要查询对象的唯一标识(orders的主键 ) 如果有多个唯一表示都都配置上即可
column:唯一标识的咧 property:为结果类型的属性 --> <id column="id" property="id" /> <result column="user_id" property="userId" /> <result column="number" property="number" /> <result column="createtime" property="createtime" /> <result column="note" property="note" /> <!-- 配置要映射的关联用户的信息 --> <!-- assocation用来映射关联查询单个对象的信息 property:要将关联的信息映射到Orders中的那个属性 --> <association property="user" javaType="cn.mycookies.mybatis.demo.po.User"> <result column="user_id" property="id" /> <result column="username" property="username" /> <result column="sex" property="sex" /> <result column="address" property="address" /> </association> </resultMap>
<!-- resultMap映射结果查询 -->
    <select id="findOrdersUserResultMap" resultMap="user_orders_resultmap">
        SELECT orders.*
        ,User.username,User.sex,User.address
        from orders ,user
        where
        orders.user_id = user.id
    </select>

2.一对多查询

案例:查询所有订单信息一级订单下的订单明细

  订单与订单明细是一对多的关系

只能用resultMap实现

<!-- 一对多查询 订单与订单明细 -->
    <resultMap type="orders" id="OrdersAndOrderDetailResultMap"
        extends="user_orders_resultmap">
        <!-- 因为出现了重复 我们可以使用restulemap的继承 -->
        <!-- 订单信息 <id column="id" property="id"/> <result column="user_id" property="userid"/>
            <result column="number" property="number"/> <result column="createtime" property="createtime"/>
            <result column="note" property="note"/> 用户信息 <association property="user"
            javaType="user"> <result column="user_id" property="id"/> <result column="username"
            property="username"/> <result column="sex" property="sex"/> <result column="address"
            property="address"/> </association> -->
        <!-- 订单明细信息 property:po类中对应的属性名 ofType映射到list集合属性中的po类型 -->
        <collection property="orderdetailsList" ofType="Orderdetail">
            <id column="ordertaile_id" property="id" />
            <result column="items_id" property="itemsId" />
            <result column="items_num" property="itemsNum" />
            <result column="orders_id" property="ordersId" />
        </collection>
    </resultMap>
<select id="findOrdersAndOrderDetailsresultMap" resultMap="OrdersAndOrderDetailResultMap">
        SELECT orders.* ,
        User.username,
        User.sex,
        User.address,
        orderdetail.id
        ordertaile_id ,
        orderdetail.items_id,
        orderdetail.items_num,
        orderdetail.orders_id
        from orders ,user,orderdetail
        where orders.user_id
        = user.id and orderdetail.orders_id=orders.id
    </select>

3.多对多的查询

 案例:查询用户购买的商品信息。

需要查询用户信息,关联查询订单和订单明细,通过订单明细关联查询商品信息

    <!--用户与items的 多对多映射关系 -->
    <resultMap type="User" id="userAndItemsResultMap">
        <!-- 用户信息 -->
        <id column="user_id" property="id" />
        <result column="username" property="username" />
        <result column="sex" property="sex" />
        <result column="address" property="address" />

        <!-- 用户所购买的订单信息 -->
        <collection property="ordersList" ofType="orders">
            <id column="id" property="id" />
            <result column="user_id" property="userId" />
            <result column="number" property="number" />
            <result column="createtime" property="createtime" />
            <result column="note" property="note" />
            <!-- 订单明细 -->
            <collection property="orderdetailsList" ofType="orderdetail">
                <id column="orderdetail_id" property="id" />
                <result column="items_id" property="itemsId" />
                <result column="items_num" property="itemsNum" />
                <result column="orders_id" property="ordersId" />
                <!-- 商品明细 一个订单包括一个item -->
                <association property="items" javaType="Items">
                    <id column="items_id" property="id" />
                    <result column="items_name" property="name" />
                    <result column="items_detail" property="detail" />
                    <result column="items_price" property="price" />
                </association>
            </collection>
        </collection>
    </resultMap>
    <select id="findUserandItemsResultMap" resultMap="userAndItemsResultMap">
        SELECT
        orders.*,
        User.username,
        User.sex,
        User.address,
        orderdetail.id
        ordertaile_id ,
        orderdetail.items_id,
        orderdetail.items_num,
        orderdetail.orders_id,
        items.price items_price,
        items.detail
        items_detail,
        items.name items_name
        from orders ,user,orderdetail,items
        where orders.user_id = user.id and
        orderdetail.orders_id=orders.id and
        orderdetail.items_id=items.id
    </select>

总结:

resulttype:将查询的结果按照sql列名pojo属性名一致性映射到pojo中。

  使用场景:常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面上时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list即可。

resultMap:使用association和collection完成一对一和一对多高级映射(对结果有特殊要求时使用)

    association:将关联查询信息映射到一个pojo对象中

    使用场景:为了方便查询关联信息可以使用association将关联订单信息映射到用户对象的pojo属性中,比如,查询订单的用户信息。

    collection:将关联查询信息映射到一个list集合中

    使用场景:为了方便查询遍历关联信息可以使用collection将关联信息映射到一个集合中,比如,查询用户权限范围模块以及模块下的菜单,可以使用collection将模块映射到模块list中,将菜单列表映射到模块对象的list属性中,这样做的目的是方便对查询结果集进行遍历查询。

框架学习笔记之Mybatis(二)的更多相关文章

  1. 框架学习笔记之Mybatis(一)

    一.简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...

  2. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

  3. Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建

    之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...

  4. SSM框架学习笔记_第1章_SpringIOC概述

    第1章 SpringIOC概述 Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. 1.1 控制反转IOC IOC(inversion of controller)是一种概念 ...

  5. phalcon(费尔康)框架学习笔记

    phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构   pha ...

  6. JavaSE中Map框架学习笔记

    前言:最近几天都在生病,退烧之后身体虚弱.头疼.在床上躺了几天,什么事情都干不了.接下来这段时间,要好好加快进度才好. 前面用了三篇文章的篇幅学习了Collection框架的相关内容,而Map框架相对 ...

  7. JavaSE中线程与并行API框架学习笔记1——线程是什么?

    前言:虽然工作了三年,但是几乎没有使用到多线程之类的内容.这其实是工作与学习的矛盾.我们在公司上班,很多时候都只是在处理业务代码,很少接触底层技术. 可是你不可能一辈子都写业务代码,而且跳槽之后新单位 ...

  8. spring mvc 及NUI前端框架学习笔记

    spring mvc 及NUI前端框架学习笔记 页面传值 一.同一页面 直接通过$J.getbyName("id").setValue(id); Set值即可 二.跳转页面(bus ...

  9. Spring框架学习笔记(5)——Spring Boot创建与使用

    Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...

随机推荐

  1. 分享基于Qt5开发的一款故障波形模拟软件

    背景介绍 这是一款采用Qt5编写的用于生成故障模拟波形的软件.生成的波形数据用于下发到终端机器生成对应的故障类型,用于培训相关设备维护人员的故障排查技能.因此,在这款软件中实现了故障方案管理.故障波形 ...

  2. Java DualPivotQuickSort 双轴快速排序 源码 笔记

    DualPivotQuicksort source code 这个算法是Arrays.java中给基本类型的数据排序使用的具体实现.它针对每种基本类型都做了实现,实现的方式有稍微的差异,但是思路都是相 ...

  3. V-bind详细使用

    v-bind  主要用于属性绑定,Vue官方提供了一个简写方式 :bind,例如: <!-- 完整语法 --> <a v-bind:href="url">& ...

  4. virtualbox主机与虚拟机之间互相通信教程

    前言 在使用虚拟机搭建集群时,需要实现虚拟机与虚拟机之间互相ping通,并且主机与虚拟机也可以互相ping通. 一.环境准备: 1.主机为win7 2.virtualbox下创建两台ubuntu虚拟机 ...

  5. DataTables ajax + bootstrap 分页/搜索/排序/常见问题

    最近学校的网站建设需要,尝试使用了下Jquery dataTables控件,接触过C#的人都知道,C#中也含有一个DataTable,但它和我们今天讨论的东西无关 我使用的是官网最新的DataTabl ...

  6. CSS 剩余宽度和高度完全填充

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  7. 说一说Java的Unsafe类

    最近在看Java并发包的源码,发现了神奇的Unsafe类,仔细研究了一下,在这里跟大家分享一下. Unsafe类是在sun.misc包下,不属于Java标准.但是很多Java的基础类库,包括一些被广泛 ...

  8. python全栈开发-Day11 迭代器、生成器、面向过程编程

    一. 迭代器 一 .迭代的概念 迭代器即迭代的工具,那什么是迭代呢? 迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复,因而 ...

  9. Oracle之SQL优化专题01-查看SQL执行计划的方法

    在我2014年总结的"SQL Tuning 基础概述"中,其实已经介绍了一些查看SQL执行计划的方法,但是不够系统和全面,所以本次SQL优化专题,就首先要系统的介绍一下查看SQL执 ...

  10. QTableView

    QTableView常用于实现数据的表格显示.下面我们如何按步骤实现学生信息表格: 一 添加表头 //准备数据模型     QStandardItemModel *student_model = ne ...