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 对象的属性值将数据插入相应的字段中。
相关推荐
资讯第一线18 分钟前
MSXML 6.0 SP1 x86 x64 (XML解析器) 下载
xml
小光学长38 分钟前
基于vue框架的防疫科普网站0838x(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
极限实验室44 分钟前
使用 Docker Compose 简化 INFINI Console 与 Easysearch 环境搭建
数据库·docker·devops
秋千码途1 小时前
小架构step系列08:logback.xml的配置
xml·java·logback
飞翔的佩奇1 小时前
Java项目:基于SSM框架实现的旅游协会管理系统【ssm+B/S架构+源码+数据库+毕业论文】
java·数据库·mysql·毕业设计·ssm·旅游·jsp
float_六七3 小时前
SQL六大核心类别全解析
数据库·sql·oracle
Code季风5 小时前
将 gRPC 服务注册到 Consul:从配置到服务发现的完整实践(上)
数据库·微服务·go·json·服务发现·consul
Boilermaker19925 小时前
【Java EE】SpringIoC
前端·数据库·spring
霸王龙的小胳膊5 小时前
泛微虚拟视图-数据虚拟化集成
数据库
灵犀学长6 小时前
解锁Spring Boot多项目共享Redis:优雅Key命名结构指南
数据库·redis