iBATIS的多对多映射配置7,测试
- package com.lsm.test;
- import java.io.Reader;
- import java.sql.SQLException;
- import java.util.List;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- import com.lsm.domain.Student;
- import com.lsm.domain.Teacher;
- public class Many2Many
- {
- private static SqlMapClient sqlMapClient = null;
- static
- {
- try
- {
- Reader reader = Resources.getResourceAsReader("com/lsm/cfg/SqlMapConfig.xml");
- sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- Many2Many m2m = new Many2Many();
- List studentlist = null;
- studentlist = m2m.getStudentInfo();
- for(int i=0;i﹤studentlist.size();i++)
- {
- Student s = new Student();
- s = (Student) studentlist.get(i);
- System.out.println("name:"+s.getName() + "\t" + "birthday:"+s.getBirthday());
- List tlist = s.getTeachers();
- if(tlist!=null)
- {
- System.out.println("his teachers as follows:");
- {
- for(int ti=0;ti﹤tlist.size();ti++)
- {
- Teacher t = new Teacher();
- t = (Teacher) tlist.get(ti);
- System.out.println("teacher name:" + t.getName());
- }
- }
- }
- }
- List teacherlist = null;
- teacherlist = m2m.getTeacherInfo();
- for(int i=0;i﹤teacherlist.size();i++)
- {
- Teacher t = new Teacher();
- t = (Teacher) teacherlist.get(i);
- System.out.println("name:"+t.getName() + "\t" + "subject:" + t.getSubject());
- List slist = t.getStudents();
- if(slist!=null)
- {
- System.out.println("his students as follows:");
- for(int si=0;si﹤slist.size();si++)
- {
- Student s = new Student();
- s = (Student) slist.get(si);
- System.out.println("student name:"+s.getName());
- }
- }
- }
- }
- // 获取学生信息
- public List getStudentInfo()
- {
- List studentList = null;
- try
- {
- System.out.println("学生信息如下:");
- studentList = sqlMapClient.queryForList("getStudents");
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return studentList;
- }
- // 获取老师信息
- // 获取学生信息
- public List getTeacherInfo()
- {
- List studentList = null;
- try
- {
- System.out.println("老师信息如下:");
- studentList = sqlMapClient.queryForList("getTeachers");
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return studentList;
- }
- }
8,输出
- 学生信息如下:
- name:张三 birthday:1982-01-01
- his teachers as follows:
- teacher name:Jerry
- teacher name:Tom
- name:李四 birthday:1983-02-02
- his teachers as follows:
- teacher name:Jerry
- name:王五 birthday:1984-03-03
- his teachers as follows:
- teacher name:Tom
- name:赵六 birthday:1985-04-04
- his teachers as follows:
- 老师信息如下:
- name:Jerry subject:语文
- his students as follows:
- student name:张三
- student name:李四
- name:Tom subject:数学
- his students as follows:
- student name:张三
- student name:王五
- name:Steven subject:英语
- his students as follows:
查询学生时带出老师信息,查询老师时带出学生信息,说明多对多映射成功。
iBATIS的多对多映射配置的情况就向你介绍到这里,希望对你有所帮助。
评论