基于本人的这篇文章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 sqlMap
PUBLIC "-//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 class="Clazz" 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" parameterClass="int" resultMap="get-clazz-result">
<![CDATA[
select id,classname
from clazz
where id=#id#
]]>
</select>
<select id="getStudentByClazzId" parameterClass="int" resultClass="Student">
select clazzid,studentname,age
from student
where clazzid=#clazzid#
</select>
</sqlMap>
Mytest.java:
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 stub
try {
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 exception
e.printStackTrace();
}
}
}
标签:学习,clazzid,int,void,public,ibatis,import,关联,id From: https://blog.51cto.com/u_4427045/6238084