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变成name1mybatis这么锉???还是我用的不对??
还有,各位有好的mybatis的教程推荐么,官方的教程好像太简略了
}
{
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变成name1mybatis这么锉???还是我用的不对??
还有,各位有好的mybatis的教程推荐么,官方的教程好像太简略了
}
解决方案 »
- js无法取得表单中的值。。
- jpa+hibernate一对多双向关联关系更新问题
- 谁有map的详细用法!分享下!谢谢各位大哥大姐了!~~~
- 请教:这个错误怎么引起的啊?包已经在工程里了???
- 数据库连接池释放连接之问
- 注册的时候,邮箱验证总是报错
- 在Hibernate中,怎样做修改记录?请高手们帮手!!
- log4j:ERROR Failed to rename 100分
- 谈恋爱难于ejb开发
- Spring MVC的Controller是线程安全的么?
- 急需帮助!spring属性注入的问题,help
- 写了一个WebService入门demo,报com.jingtian.demo.jaxws.Add is not found
像这种情况的话,一般会有两层,一个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层中的方法