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 分钟前
Mysql:数据库和表增删查改基本语句
数据库·mysql·数据库备份
团儿.1 小时前
解锁MySQL高可用新境界:深入探索MHA架构的无限魅力与实战部署
数据库·mysql·架构·mysql之mha架构
程序猿小D1 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
CHICX12291 小时前
【Hadoop】改一下core-site.xml和hdfs-site.xml配置就可以访问Web UI
xml·大数据·hadoop
权^2 小时前
MySQL--聚合查询、联合查询、子查询、合并查询(上万字超详解!!!)
大数据·数据库·学习·mysql
Code成立2 小时前
1、深入理解Redis线程模型
数据库·redis·bootstrap
缘友一世4 小时前
macos安装mongodb
数据库·mongodb·macos
万事大吉CC5 小时前
mysql单表查询·3
数据库·mysql
bin91536 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
Miqiuha7 小时前
lock_guard和unique_lock学习总结
java·数据库·学习