Mybatis映射文件动态SQL语句-02
2024-09-04 22:36:15
foreach
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hao.mapper.UserMapper">
<select id="findByCondition" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
<if test="password!=null">
and password=#{password}
</if>
</where>
</select>
<select id="findByIds" parameterType="list" resultType="user">
select * from user
<where>
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
</mapper>
测试
@Test
public void test1() throws IOException {
InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(stream).openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<Integer> ids=new ArrayList<>();
ids.add(1);
ids.add(2);
List<User> byIds = mapper.findByIds(ids);
System.out.println(byIds);
}
观看打印的日志
-collection 表示要做foreach循环的对象
- item 表示集合中每一个元素或者该集合的对象,支持对象点属性的方式获取属性。
- index 表示循环的下标,从0开始
- open 表示以什么开始
- separator 表示每次进行迭代之间以什么符号作为分隔符
- close表示以什么结束
最新文章
- 并查集 poj2236
- mysql load file
- 【Oralce】时间操作
- 在C#中使用C++编写的类
- 正则基础之——环视(Lookaround)
- WebConfig配置文件详解(转载自逆心的博客)
- Application+Handle+Task
- 代码高亮插件推荐——SyntaxHighlighter++
- js导航栏样式变换
- windows 2008 R2操作系统上使用iis服务运行php和mysql数据库的网站遇到的验证码不显示问题?
- tomcat 构建问题记录
- python - Random常用方法记录
- Python - IPython
- #incldue<;cctype>;函数系列
- AS语法
- com.sun.awt.AWTUtilities.setWindowOpacity相关说明
- Edit Control响应全选(Ctrl+A)
- Animator 设置动画效果
- fillna()
- Sqlserver 批量数据更改