mybatis遇到的一个问题

cumt_TTR 2013-06-06 11:12:44
public class Employee
{
private int id;
private String name;
private int age;
private int gender;

//get、set方法。。。。
}

public class Department
{
private int id;
private String name;
private int accounts;
private List<Employee> employees;

//get、set方法。。。。
}

===============语句映射文件================
<mapper namespace="inter.InterOperation">
<select id="getDepartment" resultMap="resultDepartment" >
select d.did,
d.name,
d.accounts,
e.eid,
e.age,
e.name1,
e.gender
from
department d,employee e
where e.employee_department_id=d.did and d.did=#{id}
</select>

<resultMap type="Department" id="resultDepartment">
<id property="did" column="id"/>
<result property="name" column="name"/>
<result property="accounts" column="accounts"/>
<collection property="employees" ofType="Employee">
<id property="eid" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="gender" column="gender"/>
</collection>
</resultMap>
</mapper>

=============测试代码=====================

InterOperation interOperation = sqlSession
.getMapper(InterOperation.class);
Department d = interOperation.getDepartment(1);
System.out.println("d.getName:"+d.getName());
assertEquals("dep1", d.getName());
System.out.println(d.getEmployees().size());
for(Employee e:d.getEmployees())
System.out.println("e.getName:"+e.getName());
//在这里打印出的本应该是Employee所对应的name,发现打印出的都是Department的name
发现解决办法是把数据库里的Employee的name变成name1

mybatis这么锉???还是我用的不对??
还有,各位有好的mybatis的教程推荐么,官方的教程好像太简略了
}
...全文
132 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yeehua 2013-06-08
  • 打赏
  • 举报
回复
引用 4 楼 cumt_TTR 的回复:
[quote=引用 1 楼 yeehua 的回复:] lz的关于对于这种部门与职员之间的一对多关系的查询,还是头一次看到可以这么用,可能是自己孤陋寡闻,不知道可以这么查。。。。 我以前的方案是,将部门跟职员之间的查询解耦,弄一个根据部门查询部门职员的方法,然后在生成部门对象时,将该部门对象的所有职员查出来。。。
新手,求指教,求代码 [/quote] 像这种情况的话,一般会有两层,一个dao层,还有一个service层,dao层定义数据访问的接口及其实现,service层则在在dao层之上,调用dao层接口的方法,对数据对象进行处理。 你这里可以在dao层的EmployeeDao接口中,定义一个List<Employee> getEmployeesByDepartmentId(int id)方法, 在DepartmentDao接口中,定义一个Department getDepartment(String departmentName)方法,其中返回的Department对象中的employees是空的, 在service层,定义一个DepartmentService类,里面定义一个Department getDepartment(String departmentName)方法,用DepartmentDao中的getDepartment方法获得一个employees为空的Department对象,再通过EmployeeDao中的getEmployeesByDepartmentId方法获得该Department对象的employees,并将其设置到Department中。 外面不直接调用dao层的方法,调用service层中的方法。。。
cumt_TTR 2013-06-07
  • 打赏
  • 举报
回复
引用 1 楼 yeehua 的回复:
lz的关于对于这种部门与职员之间的一对多关系的查询,还是头一次看到可以这么用,可能是自己孤陋寡闻,不知道可以这么查。。。。 我以前的方案是,将部门跟职员之间的查询解耦,弄一个根据部门查询部门职员的方法,然后在生成部门对象时,将该部门对象的所有职员查出来。。。
新手,求指教,求代码
cumt_TTR 2013-06-07
  • 打赏
  • 举报
回复
不太懂,新手,求指教
只是_曾经 2013-06-07
  • 打赏
  • 举报
回复
贴代码没有高亮,看着头疼!
Yeehua 2013-06-06
  • 打赏
  • 举报
回复
lz的关于对于这种部门与职员之间的一对多关系的查询,还是头一次看到可以这么用,可能是自己孤陋寡闻,不知道可以这么查。。。。 我以前的方案是,将部门跟职员之间的查询解耦,弄一个根据部门查询部门职员的方法,然后在生成部门对象时,将该部门对象的所有职员查出来。。。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧