
一.demo中引出的概念和知识点
二.案例:
首先我们在数据库建了两张表,学生表和班级表建立了主外键关系
涉及到多表查询用mybatis框架改如何写呢?
我们代码现行,实体类和Dao层接口写好之后,我们新建一个StudentMapper.xml文件
可以根据命名空间namespace,去拿到接口,以这个接口为接口创建了一个代理类(使用studentMapper.xml创建的),来调用dao层里面对应的方法
select * from student s,classes c where s.classId=c.classId and s.stuId=#{stuId}
写好对应的mapper文件之后,我们将这个mapper文件引用到主配置文件mybatis-config.xml文件里面去
然后我们就可以新建一个Junit测试类来进行测试了
@Test
public void seletTestOne() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
//这是直接在mapper文件里面通过SQL的id去调用
//Student stu = sqlSession.selectOne("selectStu", 3);
//这是通过代理的方式,接口是不能调用方法的,所以这里的studentDao是代理类,根据命名空间namespace
// ,去拿到接口,以这个接口为接口创建了一个代理类(使用studentMapper.xml创建的),
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
Student stu = studentDao.selectStu(3);
System.out.println(stu);
sqlSession.commit();
}