浏览 1983 次
锁定老帖子 主题:ibatis关联对象的使用04
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-04-16
1、两个相互关联的对象 public class Classes { private int id; private String cname; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } } public class Student { private int id; private String name; private String sex; private Classes classes; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } }
从student 中关联到class 对象
Student s = (Student)sqlMapper.queryForObject("findStudent", 1); System.out.println(s.getName()+"属于班级:"+s.getClasses().getCname());
使用别名映射 <select id="findStudent" parameterClass="int" resultClass="Student"> select s.name, s.sex, s.cid as "classes.id", c.cname as "classes.cname" from t_student s,t_classes c where s.cid = c.id and s.id = #id# </select>
a插入的用点导航
<insert id="saveStudent" parameterClass="Student"> insert into t_student values (null,#name#,#sex#,#classes.id#) </insert> b写两条sql使用resultMap映射
<resultMap class="Student" id="findStudent-result"> <result property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="classes" column="cid" select="findClassesById"/> </resultMap> <select id="findStudent" parameterClass="int" resultMap="findStudent-result"> select * from t_student where id = #id# </select>
<select id="findClassesById" parameterClass="int" resultClass="Classes"> select * from t_classes where id = #cid# </select>
2、
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-07-02
最后修改:2010-07-02
select
s.name, s.sex, s.cid as "classes.id", c.cname as "classes.cname" from t_student s,t_classes c where s.cid = c.id and s.id = #id# as 后面加引号是什么意思 解释一下 而且resultClass是 stutent 但这里要要查Class类的name 这样行不行 |
|
返回顶楼 | |
发表时间:2010-08-27
最后修改:2010-08-30
已经删除 |
|
返回顶楼 | |