Mybatis之动态sql和分页

一、Mybatis的foreach标签

if标签

<update id="updateByPrimaryKeySelective" parameterType="com.javaxl.model.Book" >
    update t_mvc_book
    <set >
      <if test="bname != null" >
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null" >
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>

之前自定义mvc的时代:

修改的SQL语句:

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

弊端:
意味着后台meetingInfo实体类 只有title、content属性值不为空,其他为空
所以只要把 if标签 的拼接条件放在配置文件里面去拼接即可。

foreach标签:

 @Test
    public void test3() {
    int[] ints={1,2,3,4,5,6};
//    将数据编程字符串  1,2,3,4,5,6
        StringBuffer sb=new StringBuffer();
        for (int i:ints){
        sb.append(",").append(i);
        }
        String s=sb.toString();
        System.out.println(s.substring(1));
    }

在这里插入图片描述
但是
在这里插入图片描述
所以就需要用到forEach标签
BookMapper.xml:

<!--bookIds:1,2,3,4,5,6-->
  <select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid in
    <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
      #{bid}
    </foreach>
  </select>

BookMapper :

package com.zking.mapper;
 
import com.javaxl.model.Book;
import org.apache.ibatis.annotations.Param;
 
import java.util.List;
 
public interface BookMapper {
    int deleteByPrimaryKey(Integer bid);
 
    int insert(Book record);
 
    int insertSelective(Book record);
 
    Book selectByPrimaryKey(Integer bid);
 
    int updateByPrimaryKeySelective(Book record);
 
    int updateByPrimaryKey(Book record);
 
//    通过in关键字进行查询,讲解foreach标签的使用
//    如果说参数是非实体类(book,Order,....),那么记得加上注解 @param,bookIds是对应collection属性的
    List<Book> selectByIn(@Param("bookIds") List bookIds);
 
}

BookBizImpl :

package com.zking.mapper.biz.impl;
 
import com.javaxl.model.Book;
import com.zking.mapper.BookMapper;
import com.zking.mapper.biz.BookBiz;
 
import java.util.List;
 
/**
 * @author 周周
 * @create 2022-08-10 23:07
 */
public class BookBizImpl implements BookBiz {
    private BookMapper bookMapper;
 
    //alt+insert  快速提供set/get/toString/构造方法
    //alt+enter  快速构建实现类,填充代码的前半部分  Ctrl+1
    public BookMapper getBookMapper() {
        return bookMapper;
    }
 
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }
 
    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return bookMapper.deleteByPrimaryKey(bid);
    }
 
    @Override
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }
 
    @Override
    public List<Book> selectByIn(List bookIds) {
        return bookMapper.selectByIn(bookIds);
    }
}

BookBiz :

package com.zking.mapper.biz;
 
import com.javaxl.model.Book;
 
import java.util.List;
 
/**
 * @author 周周
 * @create 2022-08-10 23:02
 */
public interface BookBiz {
 
    int deleteByPrimaryKey(Integer bid);
 
 
    Book selectByPrimaryKey(Integer bid);
 
    List<Book> selectByIn(List bookIds);
 
 
}

BookBizImplTest :(测试)

package com.zking.mapper.biz.impl;
 
import com.zking.mapper.BookMapper;
import com.zking.mapper.util.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * @author 周周
 * @create 2022-08-10 23:21
 */
public
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值