上一篇:16-Mybatis动态sql之二 – < where>标签https://blog.csdn.net/fsjwin/article/details/109675625
where部分发生变化,解决方案:mybatis提供的标签
1. if
2. where
3. foreach
下面看一下foreach标签的使用
1. StudentDao
//foreach动态sql 必须参数为list,不能是其他的情况
public List<Student> selectStudentforeach1(List<Integer> list);
//foreach-动态sql 必须参数为实体类list,不能是其他的情况
public List<Student> selectStudentforeach2(@Param("studentList") List<Student> studentList);
2. StudentDao.xml
<select id="selectStudentforeach1" resultType="com.yuhl.domain.Student">
select id,name,email,age from student where id in
<foreach collection="list" item="myid" open="(" close=")" separator=",">
#{myid}
</foreach>
</select>
<select id="selectStudentforeach2" resultType="com.yuhl.domain.Student">
select id,name,email,age from student where id in
<foreach collection="studentList" item="stu" open="(" close=")" separator=",">
#{stu.id}
</foreach>
</select>
3. 测试
@Test
public void test20() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
List list = new ArrayList();
list.add(1001);
list.add(1002);
list.add(1003);
List<Student> studentList = studentDao.selectStudentforeach1(list);
studentList.forEach(stu -> System.out.println(stu));
}
@Test
public void test21() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
List<Student> list = new ArrayList();
list.add(new Student(1001, "张三", "qq.com", 10));
list.add(new Student(1002, "张三", "qq.com", 10));
list.add(new Student(1003, "张三", "qq.com", 10));
List<Student> studentList = studentDao.selectStudentforeach2(list);
studentList.forEach(stu -> System.out.println(stu));
}
4. 测试结果
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 770911223.
==> Preparing: select id,name,email,age from student where id in ( ? , ? , ? )
==> Parameters: 1001(Integer), 1002(Integer), 1003(Integer)
<== Columns: id, name, email, age
<== Row: 1001, 张三, zhangsan@qq.com, 20
<== Row: 1002, 李四, lisi@qq.com, 28
<== Row: 1003, yuhl, yuhl@163.com, 20
<== Total: 3
Student{id=1001, name='张三', email='zhangsan@qq.com', age=20}
Student{id=1002, name='李四', email='lisi@qq.com', age=28}
Student{id=1003, name='yuhl', email='yuhl@163.com', age=20}
Process finished with exit code 0
另一个测试结果
Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 230528013.
==> Preparing: select id,name,email,age from student where id in ( ? , ? , ? )
==> Parameters: 1001(Integer), 1002(Integer), 1003(Integer)
<== Columns: id, name, email, age
<== Row: 1001, 张三, zhangsan@qq.com, 20
<== Row: 1002, 李四, lisi@qq.com, 28
<== Row: 1003, yuhl, yuhl@163.com, 20
<== Total: 3
Student{id=1001, name='张三', email='zhangsan@qq.com', age=20}
Student{id=1002, name='李四', email='lisi@qq.com', age=28}
Student{id=1003, name='yuhl', email='yuhl@163.com', age=20}
Process finished with exit code 0
后一篇:18-Mybatis动态sql之四 – <sql>代码片段https://blog.csdn.net/fsjwin/article/details/109683308