mybatis中的一对多的查询

一对多分为单条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语句 */@Testpublic 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语句 */@Testpublic 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();}

 
 
 
				
时间: 2024-07-30 13:30:14

mybatis中的一对多的查询的相关文章

阶段3 1.Mybatis_12.Mybatis注解开发_7 Mybatis注解开发一对多的查询配置

一对多的配置,一个用户对应多个账户 需要在Accout里面增加根据用户的uid查询的方法 在user里面指定子一对多的查询配置 换行显示 测试 把这里注销掉.测试延迟加载,代码注释掉后,延迟加载就没有再执行.什么时候用才会去加载数据 测试只执行了 select * 原文地址:https://www.cnblogs.com/wangjunwei/p/11334815.html

mybatis一对一 和 一对多 嵌套查询

实际项目中的,接口对外VO  会出现 一对一 和 一对多的情况,举例:小区 下面有 楼栋  ,楼栋 下面有 房屋    ,   房屋里面又房间 小区Vo  : districtVo { id: name: List<buildVo> builds } 楼栋Vo :buildVo{ id; name; did; List<apartmentVo> apartments } 房屋Vo :apartmentVo{ id: name: List<RoomVo> } ......

myBatis中 collection 或 association 联合查询 中column 传入多个参数值

下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap"  type="com.maidan.daas.entity.AccoSysmanResource" >    <id column="pid" property="pid" jdbcType="INTEGER" />    <result column="cre

mybatis中使用mysql的模糊查询字符串拼接(like)

方法一: <!-- 根据hid,hanme,grade,模糊查询医院信息--> <select id="getHospitalLike" resultType="com.hand.hand.domain.Hospital"> SELECT * FROM hospital where hid like '%'||#{selectword}||'%' or hname like '%'||#{selectword}||'%' or grade l

mybatis collection 一对多关联查询,单边分页的问题总结!

若想直接通过sql实现多级关联查询表结构得有2 个必不可少的字段:id ,parentId,levelId id:主键id, parentId:父id levelId:表示第几级(表本身关联查询的时候需要用到,不然会有重复数据) 利用mybatis collection 实现一对多关联查询 Dto:(一级) public class ProvinceInfoDTO implements Serializable { private String id; private String name;

mybatis的动态sql编写以及一对一关系查询和一对多的查询

创建mybatis数据库,运行以下sql语句 /* SQLyog Ultimate v8.32 MySQL - 5.5.27 : Database - mybatis ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @[email protected]@UNIQU

mybatis一对多关联查询——(九)

1.需求: 查询所有订单信息及订单下的订单明细信息. 订单信息与订单明细为一对多关系. 2.      sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.o

SpringBoot使用Mybatis注解进行一对多和多对多查询(2)

SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地址信息address,每个用户可以有多量车car,类似如下结构 |-- user |-- address |-- carList |-- car1 |-- car2 二.对应的实体类如下 /省略setter/getter public class Address { private Long id;

mybatis中sql语句查询操作

动态sql where if where可以自动处理第一个and. <!-- 根据id查询用户信息 --> <!-- public User findUserById(int id); --> <select id="findUserById" parameterType="user" resultType="user"> select * from user <!-- 当有if条件成立时,where会自