mybatis 联结查询
一、查询 员工(tbl_employee)时,关联 查询出 员工对于的部门信息 (tab1_dept),一对一查询,或者多对一查询 适用
emp bean里面 包含 部门bean dept属性对象
1.select 如下,
1 2 3 4 5 | < select id="getEmpAndDept" resultMap="MyDifEmp"> SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id, d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=#{id} </ select > |
2. resultMap:有两种方式,
第一种,直接用关联的dept.id.
第二种,association可以指定联合的javaBean对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <!-- 联合查询:级联属性封装结果集 --> < resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp"> < id column="id" property="id"/> < result column="last_name" property="lastName"/> < result column="gender" property="gender"/> < result column="did" property="dept.id"/> < result column="dept_name" property="dept.departmentName"/> </ resultMap > <!-- 使用association定义关联的单个对象的封装规则; --> < resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2"> < id column="id" property="id"/> < result column="last_name" property="lastName"/> < result column="gender" property="gender"/> <!-- association可以指定联合的javaBean对象 property="dept":指定哪个属性是联合的对象 javaType:指定这个属性对象的类型[不能省略] --> < association property="dept" javaType="com.atguigu.mybatis.bean.Department"> < id column="did" property="id"/> < result column="dept_name" property="departmentName"/> </ association > </ resultMap > |
二、association分步查询 ,
1、先按照员工id查询员工信息
2、根据查询员工信息中的d_id值去部门表查出部门信息
3、部门设置到员工中;
sql:
1 2 3 4 5 6 | < select id= "getEmpByIdStep" resultMap= "MyEmpByStep" > select * from tbl_employee where id=#{id} <if test= "_parameter!=null" > and 1=1 </if> </ select > |
resultMap:指定association 标签里面,定义一个select 引入查询部门的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <resultMap type= "com.atguigu.mybatis.bean.Employee" id= "MyEmpByStep" > <id column = "id" property= "id" /> <result column = "last_name" property= "lastName" /> <result column = "email" property= "email" /> <result column = "gender" property= "gender" /> <! -- association定义关联对象的封装规则 select :表明当前属性是调用 select 指定的方法查出的结果 column :指定将哪一列的值传给这个方法 流程:使用 select 指定的方法(传入 column 指定的这列参数的值)查出对象,并封装给property指定的属性 --> <association property= "dept" select = "com.atguigu.mybatis.dao.DepartmentMapper.getDeptById" column = "d_id" > </association> </resultMap> |
三、查询部门时,查出所有下的员工,一对多
sql:
1 2 3 4 5 6 7 8 | < select id= "getDeptByIdPlus" resultMap= "MyDept" > SELECT d.id did,d.dept_name dept_name, e.id eid,e.last_name last_name,e.email email,e.gender gender FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id WHERE d.id=#{id} </ select > |
resultMap:使用collection标签定义关联的集合类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <! --嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 --> <resultMap type= "com.atguigu.mybatis.bean.Department" id= "MyDept" > <id column = "did" property= "id" /> <result column = "dept_name" property= "departmentName" /> <! -- collection定义关联集合类型的属性的封装规则 ofType:指定集合里面元素的类型 --> <collection property= "emps" ofType= "com.atguigu.mybatis.bean.Employee" > <! -- 定义这个集合中元素的封装规则 --> <id column = "eid" property= "id" /> <result column = "last_name" property= "lastName" /> <result column = "email" property= "email" /> <result column = "gender" property= "gender" /> </collection> </resultMap> |
四、collection:分段查询
sql
1 2 3 | < select id= "getDeptByIdStep" resultMap= "MyDeptStep" > select id,dept_name from tbl_dept where id=#{id} </ select > |
resultMap:指定collection标签里面,定义一个select 引入查询员工的
1 2 3 4 5 6 7 8 | <! -- collection:分段查询 --> <resultMap type= "com.atguigu.mybatis.bean.Department" id= "MyDeptStep" > <id column = "id" property= "id" /> <id column = "dept_name" property= "departmentName" /> <collection property= "emps" select = "com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId" column = "{deptId=id}" fetchType= "lazy" ></collection> </resultMap> |
五、鉴别器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <! -- =======================鉴别器============================ --> <! -- <discriminator javaType=""></discriminator> 鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为 封装Employee: 如果查出的是女生:就把部门信息查询出来,否则不查询; 如果是男生,把last_name这一列的值赋值给email; --> <resultMap type= "com.atguigu.mybatis.bean.Employee" id= "MyEmpDis" > <id column = "id" property= "id" /> <result column = "last_name" property= "lastName" /> <result column = "email" property= "email" /> <result column = "gender" property= "gender" /> <! -- column :指定判定的列名 javaType:列值对应的java类型 --> <discriminator javaType= "string" column = "gender" > <! --女生 resultType:指定封装的结果类型;不能缺少。/resultMap--> < case value= "0" resultType= "com.atguigu.mybatis.bean.Employee" > <association property= "dept" select = "com.atguigu.mybatis.dao.DepartmentMapper.getDeptById" column = "d_id" > </association> </ case > <! --男生 ;如果是男生,把last_name这一列的值赋值给email; --> < case value= "1" resultType= "com.atguigu.mybatis.bean.Employee" > <id column = "id" property= "id" /> <result column = "last_name" property= "lastName" /> <result column = "last_name" property= "email" /> <result column = "gender" property= "gender" /> </ case > </discriminator> </resultMap> |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 想让你多爱自己一些的开源计时器
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析