一、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