Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
 List<DeptAuditUgStudent> getStudentsByDeptAndTermWithSubjects(
             @Param("deptId") String deptId, 
             @Param("termCode") String termCode);

Making It All Work

Now that all the pieces are in place, how do we actually retrieve real data from the database?

The sample project uses a Spring-MyBatis module that enables the app to configure the MyBatis system at startup via bean definitions, and provides for dependency injection of the DeptAuditUgStudentMapper interface into classes where it's needed. The sample app's config is in src/main/resources/applicationContext

  1. Similar to Hibernate, we configure a data source and a MyBatis "SqlSessionFactory".
  2. We configure a MapperScannerConfigurer bean so that MyBatis knows where to find the mapping xmls.

To exercise all of this setup, there are several JUnit tests in csf.DeptAuditTest - the test that runs the query we've described above is testGetStudentsByDeptAndTermWithSubjects(). The test class is set up to run using Spring's JUnit runner: it loads the applicationContext and injects the DeptAuditUgStudentMapper interface. The test itself simple calls the relevant interface method (getStudentsByDeptAndTermWithSubjects) passing in department ID and term code as parameters. It receives a list of DeptAuditUgStudent objects; each of these object (representing a single student) has its subjects property fully populated with DeptAuditSubject objects.