码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis中的一对多的查询

时间:2017-09-24 23:29:43      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:return   ide   print   sql语句   property   find   包含   where   .com   

一对多分为单条sq语句l和多条sql语句

下面就以员工和就职部门为例:

 

部门实体类

private Integer deptno;
private String deptname;
//植入员工实体集合
private List<Emp> emps=new ArrayList<Emp>();

public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}

public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}

public List<Emp> getEmps() {return emps;}
public void setEmps(List<Emp> emps) {
this.emps = emps;
}

员工实体类
private Integer empno;
private String empname;
private Integer deptno;
public Integer getEmpno() {return empno;}
public void setEmpno(Integer empno) {
this.empno = empno;
}

public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}

public Integer getDeptno() {return deptno;}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
 单条sql语句

接口

/**
* 根据部门对象编号,查询部门对象,对象里面包含的员工集合
*一对多,一条sql语句
* @param deptno
* @return
*/
public Dept findEmpByDept(int deptno);


xml文件(小配置)
<!--根据部门对象编号,查询部门对象,对象里面包含的员工集合
一对多 一条sql语句
-->
<resultMap id="deptMapper" type="Dept">
<id column="deptno" property="deptno"></id>
<result column="deptname" property="deptname"></result>
<collection property="emps" ofType="Emp">
<id column="empno" property="empno"></id>
<result column="empname" property="empname"></result>
</collection><!--由于此处是集合,所以用cillection-->
</resultMap>

<select id="findEmpByDept" resultMap="deptMapper">
SELECT dept.deptno,dept.deptname,empname,empno
FROM Dept,Emp
WHERE dept.deptno=emp.deptno AND emp.deptno=#{deptno}
</select>


测试类

/**
* 根据部门对象编号,查询部门对象,对象里面包含的员工集合
* 一对多 一条sql语句
*/
@Test
public void findEmpByDept(){
SqlSession session = MyBatisUtil.getSession();
IDeptDAO mapper = session.getMapper(IDeptDAO.class);
Dept dept = mapper.findEmpByDept(1);
System.out.println(dept.getDeptname());
for(Emp item:dept.getEmps()){
System.out.println(item.getEmpname());
}
session.commit();
session.close();
}

多条sql语句

接口
/**
* 根据部门对象编号,查询部门对象,对象里面包含的员工集合
*一对多,多条sql语句
* @param deptno
* @return
*/
public Dept findEmpByDeptManySql(int deptno);



xml文件(小配置)

<!--根据部门对象编号,查询部门对象,对象里面包含的员工集合-->
<!-- 一对多 多条sql语句-->
<resultMap id="deptMapperManySql" type="Dept">
<id column="deptno" property="deptno"></id>
<result column="deptname" property="deptname"></result>
<collection property="emps" ofType="Emp" select="getEmpByDeptNo" column="deptno"></collection>
</resultMap>
<select id="getEmpByDeptNo" resultType="Emp">
SELECT *from Emp where deptno=#{deptno}
</select>
<select id="findEmpByDeptManySql" resultMap="deptMapperManySql">
SELECT * FROM Dept WHERE deptno=#{deptno}
</select>



测试类

/**
* 根据部门对象编号,查询部门对象,对象里面包含的员工集合
* 一对多 多条sql语句
*/
@Test
public void findEmpByDeptManySql(){
SqlSession session = MyBatisUtil.getSession();
IDeptDAO mapper = session.getMapper(IDeptDAO.class);
Dept dept = mapper.findEmpByDeptManySql(3);
System.out.println(dept.getDeptname());
for(Emp item:dept.getEmps()){
System.out.println(item.getEmpname());
}
session.commit();
session.close();
}




 
 
 


 

mybatis中的一对多的查询

标签:return   ide   print   sql语句   property   find   包含   where   .com   

原文地址:http://www.cnblogs.com/sujulin/p/7588955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!