首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ibatis学习之一对多联系关系

2013-01-27 
ibatis学习之一对多关联基于本人的这篇文章http://chenzheng8975.iteye.com/blog/1718765的基础上,对ibati

ibatis学习之一对多关联

基于本人的这篇文章http://chenzheng8975.iteye.com/blog/1718765的基础上,对ibatis进行深入的学习:

Clazz.java:

package com.cz.model;import java.util.ArrayList;import java.util.List;public class Clazz {private int id;private String classname;private List<Student> student=new ArrayList<Student>();public int getId() {return id;}public void setId(int id) {this.id = id;}public String getClassname() {return classname;}public void setClassname(String classname) {this.classname = classname;}public List<Student> getStudent() {return student;}public void setStudent(List<Student> student) {this.student = student;}}

?

Student.java:

package com.cz.model;public class Student {private int id;private String studentname;private int age;private int clazzid;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentname() {return studentname;}public void setStudentname(String studentname) {this.studentname = studentname;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getClazzid() {return clazzid;}public void setClazzid(int clazzid) {this.clazzid = clazzid;}}

?

Clazz.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapPUBLIC "-//ibatis.apache.org//DTD SQL MAP 2.0//EN""http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap><typeAlias alias="Clazz"    type="com.cz.model.Clazz"/><typeAlias alias="Student" type="com.cz.model.Student"/><resultMap id="get-clazz-result"><result property="id" column="id"/><result property="classname" column="classname"/><result property="student" column="id" select="getStudentByClazzId"/></resultMap><select id="getClazz" parameterresultMap="get-clazz-result"><![CDATA[select id,classnamefrom clazzwhere id=#id#]]></select><select id="getStudentByClazzId" parameterresultname="code">package com.cz.test;import java.io.Reader;import java.util.List;import com.cz.model.Clazz;import com.cz.model.Student;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;public class MyTest {/** * @author xxx * @since Jan 9, 2013 * @Description: TODO(用一句话描述这个方法的作用) * @throws * @param args * void */public static void main(String[] args) {// TODO Auto-generated method stubtry {Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");           SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);           // sqlMap系统初始化完毕           Clazz clazz=(Clazz) sqlMap.queryForObject("getClazz", new Integer(1));           System.out.println("clazzid:"+clazz.getId()+",classname:"                              +clazz.getClassname());           List<Student> student=clazz.getStudent();           for(int i=0;i<student.size();i++){               System.out.println(student.get(i).getStudentname()+"-->"                              +student.get(i).getAge());           }   } catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}

?

热点排行