mybatis06:动态sql:就是在拼接sql(+sql语句案例)

mybatis06:动态sql:就是在拼接sql(+sql语句案例)


文章目录


前言:





提示:以下是本篇文章正文内容:

一、if(用来逻辑判断)(test里面写判断表达式)

xml 复制代码
// if + where
<select id="queryBlogIF" parameterType="map" resultType="com.zhu.pojo.Blog">
select *from blog
<where>
    <if test="title != null">
        title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</where>
</select>


二、choose_when_otherwise 类比于 Switch_case_default

xml 复制代码
// choose_when_otherwise + where
<select id="getTitleIF2" parameterType="map" resultType="Blog">
select *from mybatis.blog
<where>
    <choose>
        <when test="title!=null">
            title=#{title}
        </when>
        <when test="author!=null">
            author=#{author}
        </when>
        <otherwise>
            views=#{views}
        </otherwise>
    </choose>
</where>
</select>


三、trim (where, set) where,set就够用了,我们可以不用使用trim去定制化了

xml 复制代码
// update + set
<update id="updateBlog" parameterType="map">
update mybatis.blog
<set>
    <if test="title!=null">
        title=#{title},
    </if>
    <if test="author!=null">
        author=#{author},
    </if>
    <if test="views!=null">
        views=#{views},
    </if>
</set>
where id=#{id}
</update>

// 如果where元素 没有按照正常套路出牌,我们可以通过 自定义trim元素 来 定制where元素的功能
// 例如:和where元素等价的自定义trim元素为:
<trim prefix="WHERE" prefixOverrides="AND|OR">
......
</trim>

// 和set元素等价的自定义trim元素表示为:
<trim prefix="SET" suffixOverrides="," >
......
</trim>


四、foreach:



五.学会写sql语句:😊😊😊


UserMapper接口:



UserMapper.xml :



UserMapper接口:

java 复制代码
public interface UserMapper {
    // add,增加用户信息
    int add(User user)throws Exception;

    // getLoginUser,通过userCode获取User
    User getLoginUser(@Param("userCode") String userCode)throws Exception;

    // getUserList,通过条件查询-userList
    List<User> getUserList(@Param("userName")String userName,@Param("userRole")int userRole,
                           @Param("from")int currentPageNo,@Param("pageSize")int pageSize)throws Exception;

    // getUserCount,通过条件查询-用户表记录数
    int getUserCount(@Param("userName") String userName,@Param("userRole") int userRole)throws Exception;

    // deleteUserById,通过userId删除user
    int deleteUserById(@Param("delId") int delId)throws Exception;

    // getUserById,通过userId获取user
    User getUserById(@Param("id")int id)throws Exception;

    // modify,修改用户信息
    int modify(User user)throws Exception;

    // updatePwd,修改当前用户密码
    int updatePwd( @Param("id")int id,@Param("pwd") String pwd)throws Exception;
}

UserMapper.xml :

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="com.my.dao.user.UserMapper">

    <!--增加用户信息-->
    <insert id="add" parameterType="User">
        insert into smbms_user(id,userCode,userName,userPassword,gender,birthday,phone,address,userRole,createdBy,creationDate)
        values(#{id},#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate})
    </insert>

    <!--通过userCode获取User-->
    <select id="getLoginUser" parameterType="String" resultType="User">
        select * from smbms_user where userCode = #{userCode}
    </select>

    <!--通过条件查询-userList-->
    <select id="getUserList" resultType="User">
        select * from smbms_user
        <where>
            <if test="userName!=null">
                userName = #{userName}
            </if>
            <if test="userRole!=0">
                and userRole = #{userRole}
            </if>
        </where>
        limit #{from},#{pageSize}
    </select>

    <!--通过条件查询-用户表记录数-->
    <select id="getUserCount" parameterType="String" resultType="_int">
        select count(*) from smbms_user
        <where>
            <if test="userName!=null">
                userName = #{userName}
            </if>
            <if test="userRole!=0">
                and userRole = #{userRole}
            </if>
        </where>
    </select>

    <!--通过userId删除user-->
    <delete id="deleteUserById" parameterType="_int">
        delete from smbms_user where id = #{delId}
    </delete>

    <!--通过userId获取user-->
    <select id="getUserById" parameterType="_int" resultType="User">
        select * from smbms_user where id = #{id}
    </select>

    <!--修改用户信息-->
    <update id="modify" parameterType="User">
        update smbms_user
        <set>
            <if test="userCode!=null">
                userCode = #{userCode},
            </if>
            <if test="userName!=null">
                userName = #{userName},
            </if>
            <if test="userPassword!=null">
                userPassword = #{userPassword},
            </if>
            <if test="gender!=null">
                gender = #{gender},
            </if>
            <if test="birthday!=null">
                birthday = #{birthday},
            </if>
            <if test="phone!=null">
                phone = #{phone},
            </if>
            <if test="address!=null">
                address = #{address},
            </if>
            <if test="userRole!=null">
                userRole = #{userRole},
            </if>
            <if test="modifyBy!=null">
                modifyBy = #{modifyBy},
            </if>
            <if test="modifyDate!=null">
                modifyDate = #{modifyDate}
            </if>
        </set>
        where id = #{id}
    </update>

    <!--修改当前用户密码-->
    <update id="updatePwd">
        update smbms_user set userPassword = #{pwd}
        where id = #{id}
    </update>

</mapper>

总结

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

💕💕💕

熟练的书写sql语句是一件十分重要的事情!!!🤦‍♂️🤦‍♂️🤦‍♂️

相关推荐
ClickHouseDB2 小时前
ClickHouse Cloud 与 Snowflake 实时性价比对比:CostBench 测试中的 412 倍差距来
运维·数据库·系统架构
m0_639906942 小时前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
时间售卖铺3 小时前
触发器与函数实战:MySQL高效编程秘诀
运维·数据库·mysql·云原生
qetfw3 小时前
UOS MariaDB 数据库:安装、最小权限与备份验证
linux·数据库·mariadb·uos
数智园区合作社4 小时前
问数平台怎么把同一个指标锁成一个数
大数据·数据库·人工智能·ai
BIBI20494 小时前
换 Git 托管平台怎么做?基于 TortoiseGit 的跨平台迁移教程
windows·git·tortoisegit·问题解决·版本控制·最佳实践
一木 之林5 小时前
Dify学习笔记 03 · RAG检索增强生成:三代范式、评估与落地(第40-45、55-56集)
笔记·小程序·交互·dify
lbb 小魔仙5 小时前
Windows搭建PaddleOCR+飞书Webhook自动打卡通知机器人:识别、推送与远程触发完整教程
数据库·人工智能·prometheus·时序数据库
chushiyunen5 小时前
autoHotKey、windows自动化快捷键工具
运维·windows·自动化
apple-mapping5 小时前
网络RTK定位学习总结
学习·网络rtk