Mybatis xml节点-分

节点<mapper>

属性: namespace
\<!-- 此文件必须使用mapper作为根级节点 -->
\<!-- namespace属性:必须的,用于指定此XML文件对应的接口,取值为接口的全限定名 -->
<?xml version="1.0" encoding="UTF-8" ?>
\<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
\<mapper namespace="cn.tedu.mybatis.mapper.AdminMapper">

节点<insert>

属性: id & #{} & useGeneratedKeys & keyProperty
<!-- 根据要执行的数据操作的类型来选择insert/delete/update/select节点 ,节点的内部编写SQL语句-->
<!-- 节点的id是抽象方法的名称(仅名称) -->
<!-- SQL语句中的参数值使用#{}格式的占位符 -->
<!-- useGeneratedKeys使用系统生成的属性,keyProperty指定使用的属性名称 -->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    insert into ams_admin (username) values (#{username})
</insert>

节点<update>

属性: <set> & <if>
<!--  动态修改sql,能够根据OmsOrder对象的值来修改指定的字段\列  -->
<!--  在sql语句中,判断OmsOrder的属性是否为空,如果为空,不修改,如果不为空,生成修改语句  -->
<!--  sql语句中使用set标签
                        1.能生成set关键字
                        2.能够将动态生成的所有语句中,最后一个","(逗号)删除    -->
<update id="updateOrderById">
    update oms_order
    <set>
        <if test="contactName!=null">
            contact_name=#{contactName},
        </if>
        ...
        <if test="gmtPay!=null">
            gmt_pay=#{gmtPay},
        </if>
    </set>
    where id=#{id}
</update>

节点<select>

子节点<resultType>
<!-- 查询所使用的必须是select节点 -->
<!-- select节点必须配置resultType或resultMap中的其中1个
                                              对查询对象结果值的封装-->
<!-- resultType的值就是抽象方法的返回值类型的全限定名 适用于单表查询 -->
<!--resultType="int"-->
<select id="count" resultType="int">
    select count(*) from ams_admin
</select>

<!--resultMap="BaseResultMap"-->
<select id="getById" resultMap="BaseResultMap">
    select
        <include refid="xxx"/>
    from ams_admin where id=#{id}
</select>

<!--多表查询-->
<!-- AdminDetailsVO getDetailsById(Long id); -->
<select id="getDetailsById" resultMap="DetailsResultMap">
    select
        <include refid="DetailsQueryFields"/>
    from ams_admin
    left join ams_admin_role on ams_admin.id = ams_admin_role.admin_id
    left join ams_role on ams_role.id = ams_admin_role.role_id
    where ams_admin.id=#{id}
</select>

<sql id="DetailsQueryFields">
    <if test="true">
        ams_admin.id, ams_admin.username, ams_admin.gmt_modified,
      ams_role.id AS role_id,         ams_role.name AS role_name,
      ...
    </if>
</sql>

<resultMap id="BaseResultMap" type="cn.tedu.mybatis.entity.Admin">
    <id column="id" property="id" />
    <result column="username" property="username" />
    ...
    <result column="gmt_modified" property="gmtModified" />
</resultMap>

子节点<resultMap>
<!-- resultMap节点的作用:指导mybatis将查询到的结果集封装到对象中 -->
<!-- resultMap节点的id属性:自定义名称 -->
<!-- resultMap节点的type属性:封装查询结果的类型的全限定名 -->
子节点
<!-- 主键应该使用id节点进行配置,非主键、非集合的使用result节点进行配置 -->
<!-- column=结果集中的列名,property=属性名 -->
<!-- 在关联查询中,即便结果集中的列名与类的属性名完全相同,也必须配置 -->
子节点
<!-- collection子节点:用于配置1对多关系的数据部分,通常在类中是List类型的属性 -->
<!-- collection子节点的ofType:List集合中的元素的类型 -->
<resultMap id="DetailsResultMap" type="cn.tedu.mybatis.vo.AdminDetailsVO">
    <id column="id" property="id" />
    <result column="username" property="username" />
    ...
    <result column="gmt_modified" property="gmtModified" />
    <collection property="roles" ofType="cn.tedu.mybatis.entity.Role">
        <id column="role_id" property="id" />
        <result column="role_name" property="name" />
       ...
        <result column="role_gmt_modified" property="gmtModified" />
    </collection>
</resultMap>

子节点<foreach>

<!-- foreach节点是用于对参数值进行遍历的 -->
属性:collection
<!-- collection属性:被遍历对象 -->
<!-- 如果抽象方法的参数只有1个,当参数是数组时,collection="array",
                                当参数是id数组时,collection="ids",
                                当参数是List时,collection="list" -->
<!-- 如果抽象方法的参数只有多个,则collection="参数名",例如通过@Param注解配置的名称 -->
属性: item & separator & open & close
<!-- item属性:自定义名称,是被遍历的对象的名称 -->
<!-- separator属性:遍历过程中各值之间的分隔符号 -->
<!-- open="(",close=")" 等同于在<foreach/>的前后加上大括号: 例: 如下写法
1.   delete from ams_admin where id in (
        <foreach collection="array" item="id" separator=",">
            #{id}
       </foreach>
      )    
 2.   delete from ams_admin where id in 
        <foreach collection="array" item="id" separator="," open="(",close=")">
            #{id}
        </foreach>
      
 3.insert 
          into oms_order_item(id, ... , quantity)
   values
          <foreach collection="omsOrderItems" item="ooi" separator=",">
              (#{ooi.id}, ... , #{ooi.quantity})
          </foreach>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pigerr杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值