Mybatis学习日志(二)

学习了动态SQL

where+if;choose+when+otherwise;updata+set+if+where

复制代码
<select id="getCustomerif"
            resultType="com.swpu.domain.Customer"
            parameterType="com.swpu.domain.Customer">
        select * from customer
        <where>
            <if test=" name!= null and name != '' ">
                name like concat('%',#{name},'%')
            </if>
            <if test=" job != null and job != '' ">
                and job like concat('%',#{job},'%')
            </if>
        </where>
    </select>
where+if示例
复制代码

 

复制代码
<select id="getCustomerchoose"
            resultType="com.swpu.domain.Customer"
            parameterType="com.swpu.domain.Customer">
        select * from customer
        <choose>
            <when test="name!= null and name != '' ">
                name like concat('%',#{name},'%')
            </when>
            <when test=" job != null and job != '' ">
                and job like concat('%',#{job},'%')
            </when>
            <otherwise>
                where phone is not null and phone!=''
            </otherwise>
        </choose>
    </select>
choose+when+otherwise示例
复制代码

需要注意:在select语句末或者otherwise语句头要加where 否则报错SQL语句错误。

复制代码
<update id="setCustomer" parameterType="com.swpu.dao.CustomerDao">
        update customer
        <set>
            <if test="name!= null and name != '' ">
                name = #{name}
            </if>
            <if test=" job != null and job != '' ">
                , job = #{job}
            </if>
        </set>
        where id=#{id}
    </update>
updata+set+if+where
复制代码

需要注意:多个if语句后续不能用and,只能用',' ,否则会报错:Truncated incorrect DOUBLE value【and将字符串视为double类型处理了】

foreach示例:

 

复制代码
<select id="getCustomerforeach" resultType="com.swpu.domain.Customer">
        select * from customer
        <where>
            <foreach collection="list" item="id" open="id in(" separator="," close=")">
                #{id}
            </foreach>
        </where>
    </select>
select
复制代码

 

复制代码
    <insert id="insertCustomers" parameterType="com.swpu.domain.Customer">
        insert customer values
        <foreach collection="list" item="Customer" separator=",">
            (null,#{Customer.name},#{Customer.job},#{Customer.phone},#{Customer.age})
        </foreach>
    </insert>
insert
复制代码
复制代码
    <delete id="deleteCustomers">
        delete from customer
        <where>
            <foreach collection="list" item="id" open="id in(" separator="," close=")">
                #{id}
            </foreach>
        </where>
    </delete>
delete
复制代码

 

以防万一,这里把普通的CUDR语句也写上了

 

复制代码
    <!--    1-->
    <select id="getAllUsers" resultType="com.swpu.domain.User">
        select * from user
    </select>
    <!--    2-->
    <select id="getUserById" resultType="com.swpu.domain.User" parameterType="int">
        select * from user where id=#{id}
    </select>
    <!--    3-->
    <select id="getUsersByName" resultType="com.swpu.domain.User">-- parameterType="String">
        select * from user where name like concat("%",#{name},"%")
    </select>
    <!--    4-->
    <insert id="InsertUser" parameterType="com.swpu.domain.User">
        insert into user(name,sex,address) values (#{name}, #{sex},#{address})
    </insert>
    <!--    5-->
    <update id="updateUser" parameterType="com.swpu.domain.User">
        update user set name=#{user.name},sex=#{user.sex},address=#{user.address} where id=#{id}
    </update>
    <!--    6-->
    <delete id="deleteUser" parameterType="int">
        delete from user where id=#{id}
    </delete>
普通SQL
复制代码

 

 

 

【Over】

posted @   Renhr  阅读(5)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
点击右上角即可分享
微信分享提示