MyBatis 自关联以及一对多单条语句,多条语句

    DeptDao接口:
public interface DeptDao {
    //根据部门编号,检索部门名称以及该部门下所有员工的信息 单条SQL
    public Dept getDeptNO(int  deptNO);  //一对一单条SQL

    public Dept MoseDetpNo(int deptNO);  //一对多,多条SQL
}
  
   DaptDao.xml小配置:
<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bdqn.dao.DeptDao">
<!--根据部门编号,查询部门对象,对象里面包含着员工的集合-->
<resultMap id="depeMper" type="Dept">
    <id column="deptNo" property="deptNo"></id>
    <result column="deptName" property="deptName"/>
    <collection property="emps" ofType="Emp">
        <id column="empNo" property="empNo"></id>
        <result column="empName" property="empName"/>
    </collection>
</resultMap>

    有下划线的是属于一个
    <resultMap id="deprmerr" type="Dept">
        <id column="deptNo" property="deptNo"></id>
        <result column="deptName" property="deptName"/>
        <collection property="emps" ofType="Emp" select="MoseDetpNoss" column="deptNo"></collection>
    </resultMap>

<select id="getDeptNO" resultMap="depeMper">
    select dept.deptNo,deptName,empNo,empName
    from dept,emp
    where dept.deptNo=emp.deptNo
    and dept.deptNo=#{deptNo}
</select>

    <select id="MoseDetpNo" resultMap="deprmerr">
        select deptNo,deptName
        from dept
        where deptNo=#{deptNo}
    </select>

    <select id="MoseDetpNoss" resultType="Emp">
        select * from emp where deptNo=#{deptNo}
    </select>

</mapper>

    Depttest测试类
    //单条SQl
@Test
    public   void  testDept() throws IOException {
        Userinfoutil  user=new Userinfoutil();
        SqlSession  sqlSession=user.sqls();
        DeptDao  dept=sqlSession.getMapper(DeptDao.class);
        Dept  dent=dept.getDeptNO(3);
        System.out.println(dent.getDeptName());
        for (Emp emp:dent.getEmps()) {
            System.out.println(emp.getEmpName());
        }
        sqlSession.close();
    }
    //多条SQL
    @Test
    public   void  testDeptmuil() throws IOException {
        Userinfoutil  user=new Userinfoutil();
        SqlSession  sqlSession=user.sqls();
        DeptDao  dept=sqlSession.getMapper(DeptDao.class);
        Dept  dent=dept.MoseDetpNo(3);
        System.out.println(dent.getDeptName());
        for (Emp emp:dent.getEmps()) {
            System.out.println(emp.getEmpName());
        }
        sqlSession.close();
    }

EmpDao接口
public interface EmpDao {
    //根据员工的编号,  获取到员工姓名   和所属部门名称
    public Emp  getEmopNo(int  empNo); //多对一单条SQL

    public  Emp  MostEmp(int empno); //多对多,多条SQL
}

EmpDao.xml小配置
       
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bdqn.dao.EmpDao">

    <resultMap id="empmerr" type="Emp">
      <id column="empNo" property="empNo"></id>
       <result column="empName" property="empName"></result>
        <association property="dept" javaType="Dept">
            <id column="deptNo" property="deptNo"></id>
            <result column="deptName" property="deptName"></result>
        </association>
    </resultMap>

  具有下划线的是属于一个方法的
    <resultMap id="mostemmper" type="Emp">
        <id column="empNo" property="empNo"></id>
        <result column="empName" property="empName"/>
        <result  column="deptNo" property="deptNo"/>
        <association property="dept" javaType="Dept" select="mosetmers" column="deptNo"/>

    </resultMap>

    <select id="mosetmers" resultType="Dept">
          select * from dept where deptNo=#{deptNo}
    </select>

    <select id="MostEmp" resultMap="mostemmper">
           select empNo,empName,deptNo
        from emp
        where empNo=#{empNo}
    </select>


    <select id="getEmopNo" resultMap="empmerr">
        select dept.deptNo,deptName,empNo,empName
        from dept,emp
        where dept.deptNo=emp.deptNo
        and empNo=#{empNo}
    </select>


</mapper>

Emptest测试类
package cn.bdqn.test;

import cn.bdqn.dao.EmpDao;
import cn.bdqn.entity.Emp;
import cn.bdqn.util.Userinfoutil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by java on 2017/7/14.
 */
public class Emptest {

    //多对一单条数据
    @Test
    public  void  emputil() throws IOException {
        Userinfoutil   user=new Userinfoutil();
        SqlSession  sqlSession=user.sqls();
        EmpDao  emp=sqlSession.getMapper(EmpDao.class);
        Emp  em=emp.getEmopNo(3);
        System.out.println(em.getEmpName());
        System.out.println(em.getDept().getDeptName());
    }

    @Test
    public   void  testemptmuil() throws IOException {
        Userinfoutil  user=new Userinfoutil();
        SqlSession  sqlSession=user.sqls();
        EmpDao  dept=sqlSession.getMapper(EmpDao.class);
        Emp  dent=dept.MostEmp(3);
        System.out.println(dent.getEmpName());
        System.out.println(dent.getDept().getDeptName());
        sqlSession.close();
    }


}
CategoryDao 接口 自关联   
public interface CategoryDao {
    //根据pid=查询所有子分类的集合
    public List<Category> getChileBypid(int pid);
}

CategoryDao.xml小配置 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bdqn.dao.CategoryDao">
     
    <resultMap id="CteatMapper" type="Category">
        <id column="cid" property="cid"></id>
        <result column="cname" property="cname"></result>
        <result column="pid" property="pid"></result>
        <collection property="cates" ofType="Category" select="getChileBypid" column="cid"></collection>
    </resultMap>

    <select id="getChileBypid" resultMap="CteatMapper">
        SELECT  *  FROM   category where  pid=#{pid}
    </select>
      
</mapper>

Categorytest测试类
  
//自关联
@Test
public   void   testSel() throws IOException {
    Userinfoutil  user=new Userinfoutil();
    SqlSession  session=user.sqls();
    CategoryDao  ct=session.getMapper(CategoryDao.class);
    List<Category>  list=ct.getChileBypid(1);
    for (Category c:list) {
        System.out.println(c.getCname());
    }
    session.close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中实现自关联表的一对多查询,可以使用嵌套查询语句(Nested Select),具体步骤如下: 1. 在MyBatis的Mapper XML文件中,先定义一个查询语句,用来查询所有父节点信息,如下所示: ``` <select id="getParents" resultType="Parent"> SELECT * FROM parent WHERE parent_id = #{parentId} </select> ``` 2. 接着在同一个Mapper XML文件中,定义一个嵌套查询语句,用来查询某个父节点下的所有子节点信息,如下所示: ``` <select id="getChildren" resultType="Child"> SELECT * FROM child WHERE parent_id = #{parentId} </select> ``` 3. 最后,在Mapper XML文件中,将两个查询语句组合在一起,形成一个完整的查询语句,如下所示: ``` <select id="getParentsWithChildren" resultMap="ParentWithChildren"> SELECT * FROM parent WHERE parent_id = #{parentId} <collection property="children" resultMap="ChildResultMap"> SELECT * FROM child WHERE parent_id = #{parentId} </collection> </select> ``` 其中,`resultMap`属性指定了查询结果映射的对象类型,`property`属性指定了父节点对象中的子节点集合属性名,`resultMap`属性指定了子节点对象类型的结果映射配置。 4. 最后,在Java应用程序中,通过MyBatis的`SqlSession`对象执行查询语句,获取查询结果,如下所示: ``` SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); Parent parent = mapper.getParentsWithChildren(parentId); List<Child> children = parent.getChildren(); } finally { sqlSession.close(); } ``` 这样,就可以通过MyBatis实现自关联表的一对多查询了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值