正文

iBATIS的多对多映射配置方法之三(转)2012-09-19 15:05:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lovebugs/53816.html

分享到:


iBATIS的多对多映射配置7,测试

  1.  package com.lsm.test;  
  2.  
  3. import java.io.Reader;  
  4. import java.sql.SQLException;  
  5. import java.util.List;  
  6. import com.ibatis.common.resources.Resources;  
  7. import com.ibatis.sqlmap.client.SqlMapClient;  
  8. import com.ibatis.sqlmap.client.SqlMapClientBuilder;  
  9. import com.lsm.domain.Student;  
  10. import com.lsm.domain.Teacher;  
  11.  
  12. public class Many2Many  
  13. {  
  14.  
  15.  private static SqlMapClient sqlMapClient = null;  
  16.  static 
  17.  {  
  18.   try 
  19.   {  
  20.    Reader reader = Resources.getResourceAsReader("com/lsm/cfg/SqlMapConfig.xml");  
  21.    sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);  
  22.   }  
  23.   catch(Exception e)  
  24.   {  
  25.    e.printStackTrace();  
  26.   }  
  27.  }  
  28.  /**  
  29.   * @param args  
  30.   */ 
  31.  public static void main(String[] args)  
  32.  {  
  33.   Many2Many m2m = new Many2Many();  
  34.   List studentlist = null;  
  35.   studentlist = m2m.getStudentInfo();  
  36.   for(int i=0;i﹤studentlist.size();i++)  
  37.   {  
  38.    Student s = new Student();  
  39.    s = (Student) studentlist.get(i);  
  40.    System.out.println("name:"+s.getName() + "\t" + "birthday:"+s.getBirthday());  
  41.    List tlist = s.getTeachers();  
  42.    if(tlist!=null)  
  43.    {  
  44.     System.out.println("his teachers as follows:");  
  45.     {  
  46.      for(int ti=0;ti﹤tlist.size();ti++)  
  47.      {  
  48.       Teacher t = new Teacher();  
  49.       t = (Teacher) tlist.get(ti);  
  50.       System.out.println("teacher name:" + t.getName());  
  51.      }  
  52.     }  
  53.    }  
  54.   }  
  55.     
  56.   List teacherlist = null;  
  57.   teacherlist = m2m.getTeacherInfo();  
  58.   for(int i=0;i﹤teacherlist.size();i++)  
  59.   {  
  60.    Teacher t = new Teacher();  
  61.    t = (Teacher) teacherlist.get(i);  
  62.    System.out.println("name:"+t.getName() + "\t" + "subject:" + t.getSubject());  
  63.    List slist = t.getStudents();  
  64.    if(slist!=null)  
  65.    {  
  66.     System.out.println("his students as follows:");  
  67.     for(int si=0;si﹤slist.size();si++)  
  68.     {  
  69.      Student s = new Student();  
  70.      s = (Student) slist.get(si);  
  71.      System.out.println("student name:"+s.getName());  
  72.     }  
  73.    }  
  74.   }  
  75.  }  
  76.    
  77.  // 获取学生信息  
  78.  public List getStudentInfo()  
  79.  {  
  80.   List studentList = null;  
  81.   try 
  82.   {  
  83.    System.out.println("学生信息如下:");  
  84.    studentList = sqlMapClient.queryForList("getStudents");  
  85.   }  
  86.   catch (SQLException e)  
  87.   {  
  88.    e.printStackTrace();  
  89.   }  
  90.   return studentList;  
  91.  }  
  92.    
  93.  // 获取老师信息  
  94. //  获取学生信息  
  95.  public List getTeacherInfo()  
  96.  {  
  97.   List studentList = null;  
  98.   try 
  99.   {  
  100.    System.out.println("老师信息如下:");  
  101.    studentList = sqlMapClient.queryForList("getTeachers");  
  102.   }  
  103.   catch (SQLException e)  
  104.   {  
  105.    e.printStackTrace();  
  106.   }  
  107.   return studentList;  
  108.  }  
  109.  
  110. }  
  111.  

8,输出

  1. 学生信息如下:  
  2. name:张三 birthday:1982-01-01  
  3. his teachers as follows:  
  4. teacher name:Jerry  
  5. teacher name:Tom  
  6. name:李四 birthday:1983-02-02  
  7. his teachers as follows:  
  8. teacher name:Jerry  
  9. name:王五 birthday:1984-03-03  
  10. his teachers as follows:  
  11. teacher name:Tom  
  12. name:赵六 birthday:1985-04-04  
  13. his teachers as follows:  
  14. 老师信息如下:  
  15. name:Jerry subject:语文  
  16. his students as follows:  
  17. student name:张三  
  18. student name:李四  
  19. name:Tom subject:数学  
  20. his students as follows:  
  21. student name:张三  
  22. student name:王五  
  23. name:Steven subject:英语  
  24. his students as follows: 

查询学生时带出老师信息,查询老师时带出学生信息,说明多对多映射成功。

iBATIS的多对多映射配置的情况就向你介绍到这里,希望对你有所帮助。


阅读(2494) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册