sql标签表示一段sql代码,可以是表的名称、几个字段或则where条件,标签内内容一般是可以在其他位置复用sq内容。
下面的案例引用的是最常用方式,引入的是带查询的字段,id表示代码段的名称;
id,name,age,address
在待引用sql语句的地方使用
说明:查询语句使用了上一章节讲解的foreach,所以案例略显复杂,如不熟悉可翻阅上一章节学习
@Testpublic void selectStudentByIds() { //把配置文件读取到数据流 InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); //创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //创建SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); //使用反射获取StudentMapper StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); //构造查询的集合 List list=new ArrayList<>(); //构造学生信息 Student student=new Student(); student.setCalzzno("002"); //添加学生信息 list.add(student); //查询数据 List students = studentMapper.selectStdByStuClazzNo(list); //打印结果 students.forEach(stu -> System.out.println("stu = " + stu));}
留言与评论(共有 0 条评论) “” |