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 对象的属性值将数据插入相应的字段中。
相关推荐
l1t16 小时前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
l1t18 小时前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
MarkHard12320 小时前
如何利用redis使用一个滑动窗口限流
数据库·redis·缓存
island13141 天前
【Redis#10】渐进式遍历 | 数据库管理 | redis_cli | RES
数据库·redis·bootstrap
心想事成的幸运大王1 天前
Redis的过期策略
数据库·redis·缓存
倔强的石头_1 天前
CentOS 上安装KingbaseES(ISO包)详细教程
数据库
2401_897930061 天前
使用Docker轻松部署Neo4j图数据库
数据库·docker·neo4j
诗句藏于尽头1 天前
Django模型与数据库表映射的两种方式
数据库·python·django
寻星探路1 天前
数据库造神计划第六天---增删改查(CRUD)(2)
java·大数据·数据库
盖世英雄酱581361 天前
Read timed out问题 排查
java·数据库·后端