笔记:mybatis笔记

11 篇文章 0 订阅
4 篇文章 0 订阅

1.注解insert

加@insert注解

参数是实体类;

   @Insert("insert into mcLog values(#{logDate},#{logContent})")
    public boolean insertLog(McLog mcLog);

占位符内容设置实体类的属性;

2.查询Select多参数

@Select("select * from mcUser where userId like #{userId} and userWord like #{userWord}")
public McUser login(@Param("userId") String userId,
                    @Param("userWord") String userWord);

@Param注解绑定占位符参数

3.子查询Select

    @Select("select * from mcSong where songName " +
            "in " +
            "(select songName from userSong where userName like #{userName})")
    public List<McSong> selectBuy(String userName);

子查询也支持select

4.模糊查询小技巧

注解语句

    @Select("select * from mcLog where logContent like #{logContent}")
    public List<McLog> selectUserLog(String logContent);

调用**参数加%%**进行模糊查询件

adminMapper.selectUserLog("%" + userName + "%");

5.不定参数update的xml使用set拼接写法

参数是int类型使用==0判断

    <update id="songUpdate" parameterType="McSong">
        update mcSong
        <set>
            <if test="songBuy != 0">songBuy=#{songBuy},</if>
            <if test="songCollect != 0">songCollect=#{songCollect},</if>
            <if test="songLike != 0">songLike=#{songLike},</if>
            <if test="songDown != 0">songDown=#{songDown},</if>
            <if test="songListen != 0">songListen=#{songListen},</if>
        </set>
        where songName like #{songName}
    </update>

参数类型是string判空

    <update id="update_1" parameterType="Employee">
        update EMPLOYEE
        <set>
            <if test="ename != null and ename.trim().length>0">ENAME=#{ename},</if>
            <if test="job != null and job.trim().length>0">JOB=#{job},</if>
            <if test="mgr != null">MGR=#{mgr},</if>
            <if test="hiredate != null">HIREDATE=#{hiredate},</if>
            <if test="sal != null">SAL=#{sal},</if>
            <if test="comm != null">COMM=#{comm},</if>
            <if test="deptno != null">DEPTNO=#{deptno},</if>
        </set>
       <!-- <where>如果带多条件的更依然可以使<where>元素动态生成where子句</where> -->
       where EMPNO=#{empno}
    </update>

6.不定参数update的xml使用trim拼接写法

    <update id="update_2" parameterType="Employee">
        update EMPLOYEE
        <trim prefix="set" suffixOverrides=",">
            <if test="ename != null and ename.trim().length>0">ENAME=#{ename},</if>
            <if test="job != null and job.trim().length>0">JOB=#{job},</if>
            <if test="mgr != null">MGR=#{mgr},</if>
            <if test="hiredate != null">HIREDATE=#{hiredate},</if>
            <if test="sal != null">SAL=#{sal},</if>
            <if test="comm != null">COMM=#{comm},</if>
            <if test="deptno != null">DEPTNO=#{deptno},</if>
        </trim>
       <!-- <where>如果带多条件的更依然可以使<where>元素动态生成where子句</where> -->
       where EMPNO=#{empno}
    </update>

7.delete的where语句

<delete id="delete" parameterType="Employee">
        delete EMPLOYEE
        <where>
            <if test="empno != null">
                and EMPNO=#{empno}
            </if>
            <if test="ename != null">
                and ENAME=#{ename}
            </if>
            <if test="job != null and job.trim().length>0">
                and JOB=#{job}
            </if>
            <if test="loSal != null">
                and SAL>=#{loSal}
            </if>
            <if test="hiSal != null">
                <![CDATA[ and SAL<=#{hiSal} ]]>
            </if>
            <if test="deptno != null">
                and DEPTNO=#{deptno}
            </if>
        </where>
    </delete>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值