Mybatis配置抽象方法映射的SQL语句

本文详细介绍了如何在Mybatis中使用XML文件配置增删改查操作的SQL语句,包括节点的配置方法,动态SQL的使用,以及查询结果的封装。通过具体的例子展示了如何配置抽象方法映射,以及在不同场景下如增、删、改、查操作的SQL配置细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

提示:这里可以添加本文要记录的大概内容:

使用Mybatis的前期准备参照:https://blog.csdn.net/weixin_43816557/article/details/126072063
以下SQL语句都是使用XML文件来配置的SQL语句,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="接口的全限定名">
 ...
</mapper>

在首次使用时,需要在application.properties中配置以上XML文件的位置:

#Mybatis的配置SQL的XML文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml


提示:以下是本篇文章正文内容,下面案例可供参考

一、增 <insert> 节点

  • 使用<insert>节点配置SQL语句,每个节点必须配置id属性,用于指定对应的抽象方法。

  • 在配置<insert>时,如果表的id是自动编号的,则应该配置useGeneratedKeyskeyProperty属性,以获取自动编号的id。

  • 使用动态SQL的<foreach>可以实现对数组或List集合类型的参数的遍历,collection类型根据参数类型填写,集合用list,数组用array。

  • 例:

    单例增
    <!-- int insert(Album album); -->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO pms_album 
        (
            name, description, sort
        ) 
        VALUES 
        (
            #{name}, #{description}, #{sort}
        )
    </insert>

	批量增
    <!-- int insertBatch(List<Album> albums); -->
    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO pms_album 
        (
            name, description, sort
        ) values
        <foreach collection="list" item="album" separator=",">
            (#{album.name}, #{album.description}, #{album.sort})
        </foreach>
    </insert>

二、删<delete>节点

  • 使用<delete>节点配置删除SQL语句,每个节点必须配置id属性,用于指定对应的抽象方法。
  • 例:
	单例删
    <!-- int deleteById(Long id); -->
    <delete id="deleteById">
        DELETE
        FROM
            pms_album
        WHERE
            id=#{id}
    </delete>

	批量删
    <!-- int deleteByIds(Long[] ids); -->
    <delete id="deleteByIds">
        DELETE
        FROM
            pms_album
        WHERE id IN 
        (
            <foreach collection="array" item="id" separator=",">
                #{id}
            </foreach>
        )
    </delete>

三、改<update>节点

  • 使用<update>节点配置修改SQL语句,每个节点必须配置id属性,用于指定对应的抽象方法。

  • 修改数据时,也会使用到动态SQL的机制,当传入某个字段对应的值时,SQL中才会包含修改此字段的部分,反之,如果没有传入某个字段对应的值,则SQL语句不会包含修改此字段的部分!

  • 这样的功能可以通过动态SQL的<if>标签来实现!

  • 假设需要实现修改相册数据,传入的参数中包含哪些数据,就修改哪些数据,不包含的部分将不会被修改。

  • 例:

<!-- int updateFullInfoById(Brand brand); -->
    <update id="updateFullInfoById">
        update pms_brand
        <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="pinyin!=null">
                pinyin=#{pinyin},
            </if>
            <if test="logo!=null">
                logo=#{logo},
            </if>
            <if test="description!=null">
                description=#{description},
            </if>
            <if test="keywords!=null">
                keywords=#{keywords},
            </if>
            <if test="sort!=null">
                sort=#{sort},
            </if>
            <if test="sales!=null">
                sales=#{sales},
            </if>
            <if test="productCount!=null">
                product_count=#{productCount},
            </if>
            <if test="commentCount!=null">
                comment_count=#{commentCount},
            </if>
            <if test="positiveCommentCount!=null">
                positive_comment_count=#{positiveCommentCount},
            </if>
            <if test="enable!=null">
                enable=#{enable},
            </if>
        </set>
        where id=#{id}
    </update>

四、查<select>节点

  • 使用<select>节点配置查询SQL语句,每个节点必须配置id属性,用于指定对应的抽象方法。
  • 在配置<select>时,必须配置resultMapresultType这2个属性中的某1个。
  • 当使用resultType时,此属性的值取决于抽象方法的返回值类型,如果是基本数据类型(例如int等),则resultType属性的值就是类型名,如果是引用数据类型(例如String、Album等),则resultType属性的值就是类型的全限定名(在java.lang包下的可以省略包名)。
  • 一般对于自定义引用数据类型使用<resultMap>节点用于指导Mybatis封装查询结果,在<resultMap>内部,使用<result>节点,配置其columnproperty属性,用于指定列名与属性名的对应关系。
  • 可以使用<sql>节点封装SQL语句片段,并使用<include>节点进行引用,通常,使用<sql>封装字段列表。
  • <select>节点中也可以使用聚合函数。
  • 例:
	统计查询
  <!-- int countByName(String name); -->
    <select id="countByName" resultType="int">
        SELECT count(*) FROM pms_album WHERE name=#{name}
    </select>

    <!-- int sumStockBySpuId(Long spuId); -->
    <select id="sumStockBySpuId" resultType="int">
        SELECT SUM(stock)
        FROM pms_sku
        WHERE spu_id = #{spuId}
    </select>
	
	查询标准信息
    <!-- AlbumStandardVO getByName(String name); -->
    <select id="getByName" resultMap="StandardResultMap">
        select
        <include refid="StandardQueryFields"/>
        from pms_album
        where name = #{name}
    </select>
    
    <sql id="StandardQueryFields">
        <if test="true">
            id, name, description, sort
        </if>
    </sql>

    <resultMap id="StandardResultMap" type="cn.tedu.csmall.product.pojo.vo.AlbumStandardVO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="description" property="description"/>
        <result column="sort" property="sort"/>
    </resultMap>
  • 查询列表与查询某1个数据的开发过程相差不大,主要区别在于:
    查询列表时,需要查询的字段通常更少。
    Mybatis会自动使用List集合来封装查询到的多个数据,所以,抽象方法的返回值类型必须是List类型的。
  • 例:
    <!-- List<AlbumListItemVO> list(); -->
    <select id="list" resultMap="ListItemResultMap">
        SELECT
            <include refid="ListItemQueryFields" />
        FROM
            pms_album
        ORDER BY
            sort DESC, id
    </select>

 	<sql id="ListItemQueryFields">
        <if test="true">
            id,name,description,sort
        </if>
    </sql>

    <resultMap id="ListItemResultMap" type="cn.tedu.csmall.product.pojo.vo.AlbumListItemVO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="description" property="description"/>
        <result column="sort" property="sort"/>
    </resultMap>
  • 关于关联查询
  • 如果查询的是两个表的交集数据则使用等值连接或内连接(推荐)
  • 如果查询的是一张表的全部和另外一张表的交集则使用外连接
  • 例:
	根据属性id查询属性详细信息(含所属模板详情)
 <!-- AttributeDetailsVO getDetailsById(Long id); -->
    <select id="getDetailsById" resultMap="DetailsResultMap">
        SELECT
        <include refid="DetailsQueryFields"/>
        FROM
        pms_attribute
        LEFT JOIN
        pms_attribute_template
        ON
        pms_attribute_template.id=pms_attribute.template_id
        WHERE
        pms_attribute.id=#{id}
    </select>

 <!-- 详情查询字段列表 -->
    <sql id="DetailsQueryFields">
        <if test="true">
            pms_attribute.id,
            pms_attribute.name,
            pms_attribute.description,
            pms_attribute.type,
            pms_attribute.input_type,
            pms_attribute.value_list,
            pms_attribute.unit,
            pms_attribute.sort,
            pms_attribute.is_allow_customize,
            pms_attribute_template.id AS attribute_template_id,
            pms_attribute_template.name AS attribute_template_name,
            pms_attribute_template.pinyin AS attribute_template_pinyin,
            pms_attribute_template.keywords AS attribute_template_keywords
        </if>
    </sql>

    <!-- 详情查询映射结果 -->
    <resultMap id="DetailsResultMap" type="cn.tedu.mall.pojo.product.vo.AttributeDetailsVO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="description" property="description"/>
        <result column="type" property="type"/>
        <result column="input_type" property="inputType"/>
        <result column="value_list" property="valueList"/>
        <result column="unit" property="unit"/>
        <result column="sort" property="sort"/>
        <result column="is_allow_customize" property="allowCustomize"/>
        <result column="attribute_template_id" property="attributeTemplateId"/>
        <result column="attribute_template_name" property="attributeTemplateName"/>
        <result column="attribute_template_pinyin" property="attributeTemplatePinyin"/>
        <result column="attribute_template_keywords" property="attributeTemplateKeywords"/>
    </resultMap>

总结

提示:这里对文章进行总结:

以上就是今天要讲的内容,本文仅仅简单介绍了Mybatis配置抽象方法映射的SQL语句使用方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值