文件 pri 由于 包含 sql语句 pan rman spa gets
一对多分为单条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中的一对多的查询
来源: http://www.bubuko.com/infodetail-2323778.html