1.接口文件中:
public interface StudentDao
{
List<Student> selectLike(String name);
}
2.mapper.xml文件
<select id="selectLike" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name like #{name}
</select>
#{name} 会被替换成问号,即:
select id,name,email,age from student where name like #{name}
3.测试类
SqlSession sqlSession=MyBatisUtils.getSqlSession();
StudentDao dao=sqlSession.getMapper(StudentDao.class);
List<Student> students=dao.selectLike("%李%");
for (Student stu:students)
{
System.out.println("#####学生="+stu);
}
sqlSession.close();
所以最后运行的sql语句是:
select id,name,email,age from student where name like “%李%”
其中%表示0~任意个字符
所以最后查询结果: