一、动态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. 基于 CDH 构建推荐系统

    我理解的推荐系统本质是一种排序方式.排序的规则是按照我们预测的用户喜好程度的一个排序的列表,而如何定义用户的喜好程度是推荐系统要解决的核心问题.机器学习的算法只是推荐系统的一部分.构建一个完整的推荐系 ...

  2. canvas实现将文字变成颗粒

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. ASP.NET MVC上传图片的奇怪问题

    本文来源于博客园-钱智慧,转载请注明出处 表现:客户说就华为的手机有问题,而且是在QQ里打开有问题,如果在手机的浏览器上,则可以正常上传图片. 有问题的代码如下: private ResultMode ...

  4. python全栈开发-Day2 布尔、流程控制、循环

    python全栈开发-Day2 布尔 流程控制 循环   一.布尔 1.概述 #布尔值,一个True一个False #计算机俗称电脑,即我们编写程序让计算机运行时,应该是让计算机无限接近人脑,或者说人 ...

  5. 笔记:Maven 项目目录结构

    Maven提倡使用一个共同的标准目录结构,使开发人员能在熟悉了一个Maven工程后,对其他的Maven工程也能清晰了解.这样做也省去了很多设置的麻烦,以下的文档介绍是Maven希望的目录结构,并且也是 ...

  6. hi-nginx-1.3.4编译安装

    hi-nginx既是 web 服务器,也是 application 服务器.它开源在https://github.com/webcpp/hi-nginx 它是NGINX的超集,因此与编译NGINX稍有 ...

  7. Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版

    一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...

  8. Beta冲刺第二天

    一.昨天的困难 没困难 二.今天进度 局部测试并修复出现的bug 1.林洋洋:修复登录页面显示问题,修复日程查询问题 2.黄腾达:修复创建协作开始时间和结束时间没做检验的问题 3.张合胜:修复页面内容 ...

  9. 20162318 实验四 Android程序设计

    北京电子科技学院(BESTI) 实 验 报 告 课程:程序设计与数据结构 班级:1623班 姓名:张泰毓 指导老师:娄老师.王老师 实验日期:2017年5月26日 实验密级:非密级 实验器材:带Lin ...

  10. python 操作Memcached

    启动Memcached memcached -d -m 10 -u root -l 10.211.55.4 -p 12000 -c 256 -P /tmp/memcached.pid 参数说明: -d ...