美文网首页
mapper.xml文件动态sql的标签

mapper.xml文件动态sql的标签

作者: 正_文 | 来源:发表于2023-11-11 11:43 被阅读0次

一、CRUD语句

1.1查询语句
<select id="selList"
        resultType="***.resultVo"
        parameterType="***.Request">
    SELECT * FROM table WHERE *** limit #{offset},#{limit}  oder by ***
</select>
1.2插入语句
<insert id="insertOrderRecord" parameterType="map">
    INSERT INTO 
    order_table (id, customer_id, create_time, updated_time)
    VALUES(#{ID}, #{employeeId}, #{date},now(), now());
</insert>
1.3修改语句
<update id="updateOrderTable" parameterType = "map">
    UPDATE order_table 
    SET 
        store_id = #{param.storeId},
        updated_time = NOW(),
        version = version+1
    WHERE
        memberKey = #{param.memberKey}
</update>
1.4删除语句
<delete id="deleteById" parameterType="java.lang.String" >
    delete from order_table  
    where order_id = #{orderId,jdbcType=VARCHAR}  
</delete>

属性配置

属性 描述
id 命名空间中的唯一标识符,表示此段SQL执行语句的唯一标识,也是接口的方法名称 必须一致才能找到
parameterType 表示SQL语句中需要传入的参数,类型要与对应的接口方法的类型一致,这个属性是可选的。
resultMap 定义出参,调用已定义的映射管理器的id的值。
resultType 定义出参,匹配普通Java类型,如int,string,long,Date;或自定义的pojo
出参类型若不指定,将为语句类型默认类型,如语句返回值为int

mapper.xml特殊符号

原符号:   <      <=      >      >=      &       '        "
替换符号: &lt;   &lt;=   &gt;   &gt;=   &amp;   &apos;   &quot; 

<![CDATA[]]>:表示XML解析器忽略解析。在使用MyBatis过程中,有时我们的SQL是写在XML 映射文件中,如果写的SQL中有一些特殊的字符的话,在解析XML文件的时候会被当做XML自身元素,但我们不希望如此操作,此时我们可以使用<![CDATA[ ]]>来解决。

二、常用标签

Mybatis的mappper.xml中用到的标签有很多,在mybatis-3-mapper.dtd文件(点击标签,可跳转到该文件)中可以查看,如:<mapper><select><insert><selectKey><update><delete><include><resultMap><association><case><typeAlias><bind><sql>等。

2.1 <where>、<if>标签

这两个是使用比较多的标签

<where>
    <if test="null != theYear and '' != theYear">
        the_year=#{theYear}
    </if>
</where>
2.2 <sql>片段标签

通过该标签可定义能复用的sql语句片段,在执行sql语句标签中直接引用即可。

<!--定义sql片段---1-->
<sql id="sel_customerListSql1">
    o.id,o.cid,o.address,o.create_date,i.count
</sql>
<!--定义sql片段---2-->
<sql id="sel_customerListSql2">
    <where>
        <if test="null != year and '' != year">
            and the_year=#{year}
        </if>
    </where>
</sql>

<!--引用sql片段-->
select
    <include refid="sel_customerListSql1" />
from customer_table o
    <include refid="sel_customerListSql2"></include>
2.3 <choose> <when> <otherwise> 标签

有条件符合,就查询,相当于 if.....else。该标签可以插入在sql标签里面。

<select id="listProduct" resultType="Product">
    SELECT * FROM product_table
    <where>
        <choose>
            <when test="type == 1">
                and `type` not in (4,6,7)
            </when>
            <otherwise>
                and `type` in (4,6)
            </otherwise>
        </choose>
    </where>
</select>
2.4 <set>标签

<where>标签类似的,在update语句里也会碰到多个字段相关的问题, 在这种情况下,就可以使用<set>标签

<update id="updateUser" parameterType="com.example.model.User">
    UPDATE user_table
    <set>
        <if test="name != null"> name = #{name},</if>
        <if test="age != null"> age = #{age},</if>
    </set>
    WHERE id = #{id}
</update>
2.4 < foreach >标签

<foreach>标签来实现循环操作,可以用于遍历集合数组,并将集合或数组中的元素作为参数传递给SQL语句。

<select id="getUserListByIdList" parameterType="list" resultMap="userResultMap">
    SELECT * FROM user_table WHERE id IN
    <foreach item="id" collection="list" open="(" separator="," close=")">
        #{id}
    </foreach>
</select>

<foreach>标签用于遍历传入的List对象,将List中的元素作为参数传递给SQL语句中的#{id}占位符。
item属性介绍item属性指定了集合中的元素名称,collection属性指定了要遍历的集合,open属性指定了循环开始时的字符串,separator属性指定了元素之间的分隔符,close属性指定了循环结束时的字符串。
如果传入的参数是数组parameterType="array",可以使用collection属性的值为数组的名称collection="array"

2.5 <bind>标签

<bind>标签就像是再做一次字符串拼接,网上也有说叫绑定,只是方便后续的使用。

<select id="orderList" resultType="Order">
    <bind name="likename" value="'%' + name + '%'" />
    select * from order_table where name like #{likename}
</select>
2.6 <resultMap>标签

<resultMap>标签是MyBatis Mapper.xml中用于定义结果集映射的标签,它可以将查询结果映射为Java对象,可以参考Mybatis中强大的resultMap
在MyBatis中,查询结果通常是一个ResultSet对象,ResultSet对象中包含了查询结果的所有行和列。为了将查询结果转换成Java对象,需要定义一个结果集映射,将ResultSet中的每一行映射成一个Java对象。<resultMap>标签就是用于定义这个映射关系的。

<!-- resultMap定义 -->
<resultMap id="userMap" type="com.example.model.User">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <result property="age" column="age" />
</resultMap>

<!-- resultMap的引用,引用名userMap -->
<select id="getUserById" resultMap="userMap">
  SELECT * FROM user_table WHERE id = #{id}
</select>

示例<resultMap>标签定义了一个结果集映射,包含了三个子标签:<id>和两个<result>

  1. <resultMap>属性id:理解为别名,用于引用; type:对应的是我们的实体类,全路径名。
  2. <id>子标签用于定义主键,此id值用于select元素属性的引用;
    <result>子标签用于定义普通列
    property属性指定了Java对象的属性名称,
    column属性指定了数据库表中的列名称。

<resultMap>相关标签

元素名称 描述
<association> 关联一个对象,即一个映射集合
<constructor> 用于实例化类时,注入结果到构造方法中
<collection> 关联对象的集合
<association>标签

<association>标签用于关联对象。
定义关联对象类:

@Data
public class User {
    //省略用户属性...
    
    //角色信息
    private Role role;
}

mapper.xml查询语句:

<!-- User类中只有一个Role对象,并没有role_id和role_name字段属性。 -->
<!-- 所以,我们不能用resultType=User来返回,要使用association来关联它们。 -->
<!-- <select id="getUserById" resultType="User"> -->
<select id="getUserById" resultType="UserMap">
    SELECT
        u.id,
        u.username,
        r.id as 'role_id',
        r.name as 'role_name'
    FROM
        USER u
            LEFT JOIN user_roles_table ur ON u.id = ur.user_id
            LEFT JOIN role_table r ON r.id = ur.role_id
    where u.id=#{id}
</select>

<resultMap id="userMap" type="User">
    <id property="id" column="id"></id>
    <result property="username" column="username"></result>
    
    <association property="role" javaType="Role">
        <id property="id" column="role_id"></id>
        <result property="name" column="role_name"></result>
    </association>
</resultMap>

相关文章

  • 救火文章,致敬实用

    1、My Batis mapper.xml中 动态SQL中使用trim标签 if end的场景网络地址:https...

  • 动态 SQL

    根据不同的条件需要执行不同的 SQL 命令.称为动态 SQL MyBatis中动态SQL在mapper.xml中添...

  • MyBatis-Mapper.xml(输入输出映射动态sql)讲

    Mapper.xml映射文件 Mapper.xml映射文件定义了操作数据库的sql,每个sql是一个stateme...

  • 动态sql

    根据不同条件,需要执行不同的sql命令 MyBatis中动态sql是在mapper.xml中添加逻辑判断

  • 使用Example及Example[Criteria]

    mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。...

  • MyBatis动态SQL

    MyBatis 动态SQL 内容 Mybatis动态SQL在XML中支持的几种标签: if chose trim、...

  • 动态SQL

    动态SQL: Mybatis中可以在写mapper.xml时添加逻辑判断常用关键字:if、where、「choos...

  • MyBatis源码阅读(六)----mapper接口和mappe

    概述 前面我们知道sql是通过Executor执行器来执行的,那我们的sql都是写到mapper.xml文件中的,...

  • mybatis分页

    一、sql语句分页: 在mapper.xml文件添加sql语句 在接口中添加方法: 在测试类中添加: 二、RowB...

  • MyBatis动态sql

    动态sql就是可以对sql语句进行灵活的封装,拼接。通过mybatis语法的判断可以实现动态sql。 1 if标签...

网友评论

      本文标题:mapper.xml文件动态sql的标签

      本文链接:https://www.haomeiwen.com/subject/feecwdtx.html