前一篇:09-Mybatis参数的使用之四–通过索引传值-了解
https://blog.csdn.net/fsjwin/article/details/109670182
使用map传参,也很简单,直接看栗子,了解即可,别用啊!
1. StudentDao
/**
* map传值
*
*/
public List<Student> selectStudentByMap(Map map);
2. StudentDao.xml
<!--Map传值-->
<select id="selectStudentByMap" resultType="com.yuhl.domain.Student">
select id,name,email,age from student where name =#{name} or age =#{age}
</select>
3. 测试
@Test
public void test7() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
Map map = new HashMap();
map.put("name", "yuhl");
map.put("age", 11);
List<Student> studentList = studentDao.selectStudentByMap(map);
studentList.forEach(stu -> System.out.println(stu));
}
测试通过
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
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 1739876329.
==> Preparing: select id,name,email,age from student where name =? or age =?
==> Parameters: yuhl(String), 11(Integer)
<== Columns: id, name, email, age
<== Row: 1003, yuhl, yuhl@163.com, 20
<== Total: 1
Student{id=1003, name='yuhl', email='yuhl@163.com', age=20}
4. 总结
简单参数、@Param、对象传参掌握。后面这两个就不要掌握了。知道就好。权当玩玩就行。