StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--mapper:是个整体文件的大标签,用来开始和结束xml文件属性: namespace指定命名空间(相当于包名)用来区分不同mapper.xml文件中相同的id属性--><mapper namespace="zar"><!--完成查询全部学生的功能之前是List<Student> getAll();resultType:指定查询返回的结果集的类型,如果是集合则必须是泛型的类型parameterType: 如果有参数,则通过它来指定参数的类型--><select id="getAll" resultType="org.pojo.Student">select * from student</select><!--按学生名模糊查询List<Student> getByName(String name);--><select id="getByName" parameterType="string" resultType="org.pojo.Student">select * from student where name like '%${name}%'</select></mapper>
public class AppTest{
@Test
public void testLikeSelect() throws IOException {
//读取配置文件信息
InputStream in = Resources.getResourceAsStream("mybatis.xml");
//创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//取出SqlSession
SqlSession sqlSession = factory.openSession();
//模糊查询
List<Student> list = sqlSession.selectList("zar.getByName","李");
list.forEach(student -> System.out.println(student));
//关闭SqlSession
sqlSession.close();
}
}
