mapper.xml特殊SQL语句汇总(持续更新中)

1.通用查询结果列

xml 复制代码
<sql id="Base_Column_List">
    user_id, user_name, user_password
</sql>
xml 复制代码
用法:
<select id="selectUser" resultType="com.example.YourUser">
    SELECT
    <include refid="Base_Column_List"/>
	FROM your_table_name
</select>

2.动态sql语句更新表

java 复制代码
//mapper:
int updateUser(List<UserPo> userList);
xml 复制代码
<!--不需要进行条件判断时-->
    <update id="updateUser">
        <foreach collection="list" separator=";" item="item" index="index">
            update user_table set
            user_password=#{item.userPassword}, user_name=#{item.userName}, user_level=#{item.userLevel} where id=#{item.id}
        </foreach>
    </update>
xml 复制代码
<!--需要进行条件判断时-->
    <update id="updateUser">
        <foreach collection="list" separator=";" item="item" index="index">
            update user_table
            <set>
                <if test="item.userPassword!=null">user_password=#{item.userPassword},</if>
                <if test="item.userName!=null">user_name=#{item.userName},</if>
                <if test="item.userLevel!=null">user_level=#{item.userLevel},</if>
            </set>
            where id=#{item.id}
        </foreach>
    </update>
复制代码
<update id="updateUser">: 定义了一个 id 为 updateUser 的更新操作。

<foreach collection="list" separator=";" item="item" index="index">: 这里使用了 MyBatis 的 <foreach> 标签,它会遍历集合 list 中的元素,并对每个元素执行一次 SQL 操作。separator=";" 表示在每次循环之间用分号分隔,item="item" 和 index="index" 分别定义了在循环过程中当前元素和索引的别名。

<set>: 这里表示设置需要更新的字段部分。

<if test="item.userPassword!=null">user_password=#{item.userPassword},</if>: 使用 MyBatis 的 <if> 标签来进行条件判断,如果 item 对象中的 userPassword 字段不为 null,则将 user_password 字段赋值为 item.userPassword。

where id=#{item.id}: 这部分指定了更新操作的条件,根据 item 对象中的 id 字段的值来确定更新哪条具体记录。

3.动态sql语句新增表

java 复制代码
//mapper:
int insertUser(List<UserPo> userList);
xml 复制代码
    <insert id="insertUser" parameterType="com.bai.bean.req.UserReq"
            keyProperty="id" useGeneratedKeys="true">
        insert into user_table (user_no, user_name, user_level)
        values
        <foreach collection="list" separator="," item="item">
            (#{item.userNo},#{item.userName}, #{item.userLevel})
        </foreach>
    </insert>
复制代码
<insert id="insertUser" parameterType="com.bai.bean.req.UserReq" keyProperty="id" useGeneratedKeys="true">: 定义了一个 id 为 insertUser 的插入操作,指定了参数类型为 com.bai.bean.req.UserReq,keyProperty="id" 指定了插入后自动生成的主键值要设置到对象的 id 属性上,useGeneratedKeys="true" 表示使用数据库自动生成的主键。

insert into user_table ... values ...: 这里是插入语句的开始部分,指定了需要插入的表名和字段列表。

<foreach collection="list" separator="," item="item">: 使用 MyBatis 的 <foreach> 标签来遍历集合 list 中的元素,并对每个元素执行一次插入操作。separator="," 表示在每次循环之间用逗号分隔,item="item" 定义了在循环过程中当前元素的别名。

(#{item.userNo},#{item.userName}, #{item.userLevel}): 在 values 子句中,根据 item 对象的属性值将数据插入相应的字段中。
相关推荐
在未来等你1 小时前
SQL进阶之旅 Day 21:临时表与内存表应用
sql·mysql·postgresql·database·temporary-table·memory-table·sql-optimization
敖云岚1 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
zhuiQiuMX2 小时前
分享今天做的力扣SQL题
sql·算法·leetcode
LUCIAZZZ2 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
我在北京coding2 小时前
300道GaussDB(WMS)题目及答案。
数据库·gaussdb
小Tomkk2 小时前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
明月醉窗台3 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go4 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
老纪的技术唠嗑局4 小时前
重剑无锋,大巧不工 —— OceanBase 中的 Nest Loop Join 使用技巧分享
数据库·sql
未来之窗软件服务5 小时前
JAVASCRIPT 前端数据库-V6--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟·东方仙盟数据库