1.代码 <select id="getStudents" resultType="student" parameterType="map"> select * from mybatis_test.student <if test="id != null"> where id = #{id} </if> </select> 当map中的id对应的值为空的时候,查询所有的学生当map中的id对应的值不为空的时候,则加上where id = #{id}这一条件 2.使用 public static void main(String[] args) { SqlSession sqlSession = MybatisUtils.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); HashMap map = new HashMap(); map.put("id",1); List<Student> students= mapper.getStudents(map); for (Student student:students) { System.out.println(student); } sqlSession.close(); }