zoukankan      html  css  js  c++  java
  • Java-Mybatis动态SQL整理


    SQL映射文件的几个顶级元素:

      • cache - 该命名空间的缓存配置

      • cache-ref - 引用其他命名空间的缓存配置

      • resultMap - 描述如何从数据库结果集中加载对象

      • sql - 可被其他语句引用的可重用语句块

      • insert - 增

      • update - 改

      • delete - 删

    select属性

    属性描述
    id 标识符,与Mapper中对应方法的方法名一致
    parameterType 传入这条语句的参数的类全限定名或别名
    resultType 返回结果的类全限定名或别名,如果返回的是集合,应设置为集合包含的类型而不是集合本身的类型。resultType和resultMap之间只能使用一个
    resultMap 对外部resultMap的命名引用,结果映射是Mybatis的最强大的特性

     sql

    sql元素可以用来定义可重用的SQL代码片段,以便在其他语句中使用。参数可以静态地(加载的时候)确定下来并且可以在不同的include元素中定义不同的参数值

    <sql id="userColumns">
        ${alias}.id,${alias}.usernname,${alias}.password
    </sql>

    常用在include元素的refid属性或内部语句中使用属性值

    <sql id="sometable">
        ${preifx}Table
    </sql>
    
    <sql id="someinclude">
        FROM
        <include refid="${include_target}"/>
    </sql>
    
    <select id="select" resultType="Map">
        SELECT
            field1,field2,field3
        <include refid="someinclude">
            <property name="prefix" value="Some"/>
            <property name="include_target" value="sometable"/>
        </include>
    </select>

    ResultMap

    一对一查询:

    <!-- 属性名不一致或进行关联查询时,可用resultMap标签 
         以查询订单为例,从查询订单角度出发为一对一,因为一个订单只会是一个人下的 -->
    1)改造实体类:
        <!-- 在订单类中添加User属性,即一个引用类型 -->
    
    2)编写resultMap:
    <resultMap type="order" id="orderUserResultMap">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        ...
        <result property="note" column="note"/>
        <association property="user" javaType="user">
            <id property="id" column="id"/>
            <result property="username" column="username"/>
            ...
        </association>
    </resultMap>
    
    3)调用实例:
    <!-- 一对一关联,查询订单,订单内部包含用户属性 -->
    <select id="queryOrderUserResultMap" resultMap="orderUserResultMap">
        SELECT
        o.id,
        o.user_id,
        o.number,
        o.createtime,
        o.note,
        u.username,
        u.address
        FROM
        `order` o
        LEFT JOIN `user` u ON o.user_id = u.id
    </select>

    一对多查询:

    <!-- 查询所有用户信息及相关订单 -->
    <!-- 对于用户而言,一个用户可能对应多个不同的订单,即一对多 -->
    1)改造实体类:
        添加订单集合属性,如:List< Order > orders
    
    2)编写resultMap:
    <resultMap type="user" id="userOrderResultMap">
        <id property="id" column="id" />
        <result property="username" column="username" />
        <result property="birthday" column="birthday" />
        <result property="sex" column="sex" />
        <result property="address" column="address" />
    
        <!-- 配置一对多的关系
            property:填写pojo类中List集合类类属性的名称
            javaType:填写集合类型的名称 
        -->
        <collection property="orders" javaType="list" ofType="order">
            <!-- 配置主键,是关联Order的唯一标识 -->
            <id property="id" column="oid" />
            <result property="number" column="number" />
            <result property="createtime" column="createtime" />
            <result property="note" column="note" />
        </collection>
    </resultMap>
    
    3)调用实例:
    <!-- 一对多关联,查询订单同时查询该用户下的订单 -->
    <select id="queryUserOrder" resultMap="userOrderResultMap">
        SELECT
        u.id,
        u.username,
        u.birthday,
        u.sex,
        u.address,
        o.id oid,
        o.number,
        o.createtime,
        o.note
        FROM
        `user` u
        LEFT JOIN `order` o ON u.id = o.user_id
    </select>

    动态SQL

    简介

    主要介绍Mybatis提供的几种用于实现动态SQL的标签的基本使用方式:

    • if
    • choose、when、otherwise
    • trim、where、set
    • foreach

    if

    if 语句提供了可选的查找文本功能

    若 if 中条件成立,则执行 if 标签中的语句,拼接至SQL语句中

    若 if 中条件不成立,则忽略,举个栗子:

    <select id="findActiveBlogWithTitleLike" resultType="Blog">
        SELECT * FROM BLOG
        WHERE state = 'ACTIVE'
        <if test="title != null">
            AND title like #{title}
        </if>
        <if test="author != null and author.name != null">
            AND author_name like #{author.name}
        </if>
    </select>

    choose、when、otherwise

    Mybatis提供的choose元素,类似Java的switch语句,可从多个条件中选择一个使用

    举个栗子:传入了"title"就按"title"查找,传入了"author"就按"author"查找的情形。

    若两者都没有传入,就返回标记为featured=1的BLOG(若多个条件满足则遵循就近原则)

    <select id="findActiveBlogLike" resultType="Blog">
        SELECT * FROM BLOG
        WHERE state = 'ACTIVE'
        <choose>
            <when test="title !=null">
                AND title like #{title}
            </when>
            <when test="author != null and author.name != null">
                AND author_name like #{author.name}
            </when>
            <otherwise>
                AND featured = 1
            </otherwise>
        </choose>
    </select>

    https://www.cnblogs.com/torima/p/15151210.html

    故乡明
  • 相关阅读:
    异常
    抽象类
    java基础—接口3
    java基础—接口2
    java基础—接口1
    Android拍照适配方案
    继承—Car
    继承—矩形
    使用CSS和jQuery实现tab页
    纯CSS实现圆形进度条
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/15205270.html
Copyright © 2011-2022 走看看