mybatis(动态SQL和分页)

本文详细探讨了Mybatis的动态SQL,包括if和foreach的使用,以及模糊查询的方法和注意事项。同时,解释了为何需要重写Mybatis的分页,并介绍了分页查询的实现步骤。此外,还涉及了特殊字符处理在XML映射文件中的处理方式。
摘要由CSDN通过智能技术生成

目录

一、mybatis动态sql

1、if

2、foreach

 二、模糊查询

 总结

三、结果集处理

四、分页查询

为什么要重写mybatis的分页?

五、特殊字符处理 


一、mybatis动态sql

1、if

<update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.User" >
    update t_yh
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        sex = #{sex,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

 自定义MVC

update t_oa_meeting_info set id=?,title=?,content=?,zhuchiren=?,author=?,..... where id=?

弊端:

会议编辑界面 inforEdit.jsp

<form>
        <input name="title">
        <input name="content">
</form>

意味着后台meetingInfo实体类 只有title、content属性值不为空,其他为空

进一步的

update t_oa_meeting_info set id=?,title=aa,content=cc,zhuchiren=null,author=null,..... where id=?
 update t_oa_meeting_info

        title= 纳新,
      
        price = #{content,jdbcType=REAL},
    
    where bid = #{bid,jdbcType=INTEGER}

2、foreach

@Test
    public void test3() {
        int[] ints={1,2,3,4,5};
        //将数组变成字符串
        StringBuffer sb = new StringBuffer();
        for (int i:ints){
            sb.append(i).append(",");
            System.out.println(sb);
        }
    }

运行结果: 

 案例:

UserMapper.xml
 <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
    select
    <include refid="Base_Column_List" />
    from t_yh
    where id in
    <foreach collection="userIds" open="(" close=")" separator="," item="id">
      #{id}
    </foreach>
  </select>
UserMapper.java
 List<User> selectByIn(@Param("userIds") List userIds);

 UserBiz.java 

package com.cxy.biz;

import com.cxy.model.User;

import java.util.List;


public interface UserBiz {
    int deleteByPrimaryKey(Integer id);

    User selectByPrimaryKey(Integer id);

    List<User> selectByIn(List userIds);

}

 UserBizImpl .java

package com.cxy.biz.impl;

import com.cxy.biz.UserBiz;
import com.cxy.mapper.UserMapper;
import com.cxy.model.User;

import java.util.List;

public class Us
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值