一、父查询BaseChildResultMap:

<?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="com.dayhr.web.module.hr.td.elearn.mapper.TrainerMapper" > <!-- 表中字段 -->
<resultMap id="BaseResultMap" type="com.dayhr.web.module.hr.td.elearn.model.Trainer" >
<id column="id" property="id" jdbcType="CHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="recommend" property="recommend" jdbcType="INTEGER" />
<result column="trainer_type_id" property="trainerTypeId" jdbcType="CHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="pro_field" property="proField" jdbcType="VARCHAR" />
<result column="intro" property="intro" jdbcType="VARCHAR" />
<result column="head_img" property="headImg" jdbcType="VARCHAR" />
<result column="labels" property="labels" jdbcType="VARCHAR" />
<result column="corp_id" property="corpId" jdbcType="INTEGER" />
<result column="creater_id" property="createrId" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="modifier_id" property="modifierId" jdbcType="INTEGER" />
<result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" />
</resultMap> <!-- 返回页面Bean -->
<resultMap id="BaseResultRespMap" type="com.dayhr.web.module.hr.td.elearn.response.TrainerResp" >
<id column="id" property="id" jdbcType="CHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="recommend" property="recommend" jdbcType="INTEGER" />
<result column="trainer_type_id" property="trainerTypeId" jdbcType="CHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="pro_field" property="proField" jdbcType="VARCHAR" />
<result column="intro" property="intro" jdbcType="VARCHAR" />
<result column="head_img" property="headImg" jdbcType="VARCHAR" />
<result column="labels" property="labels" jdbcType="VARCHAR" />
<result column="corp_id" property="corpId" jdbcType="INTEGER" />
<result column="creater_id" property="createrId" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="modifier_id" property="modifierId" jdbcType="INTEGER" />
<result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" /> <result column="trainerTypeName" property="trainerTypeName" />
<result column="trainerLabels" property="trainerLabels" />
</resultMap> <!-- 子查询 -->
<resultMap id="BaseChildResultMap" type="com.dayhr.web.module.hr.td.elearn.response.TrainerResp" extends="BaseResultRespMap">
<collection property="trainerLabels"
ofType="com.dayhr.web.module.hr.td.elearn.response.TrainerLabelResp"
select="com.dayhr.web.module.hr.td.elearn.mapper.TrainerLabelMapper.selecLabels" column="labels">
</collection>
</resultMap> <!-- 表中基础字段 -->
<sql id="Base_Column_List">
t.id, t.name, t.title, t.recommend, t.trainer_type_id, t1.name AS trainerTypeName,
t.phone, t.email, t.address, t.pro_field, t.intro, t.head_img, t.labels, t.corp_id,
t.creater_id, t.create_time, t.modifier_id, t.modify_time
</sql> <!-- 查询条件 -->
<sql id="select_Where_Clause">
<where>
1=1
<if test="id != null and id != ''">
AND t.id = #{id }
</if>
<if test="name != null and name != ''">
AND t.name like CONCAT('%',#{name },'%' )
</if>
<if test="corpId != null and corpId != ''">
AND corp_id = #{corpId }
</if>
<if test="trainerTypeId != null and trainerTypeId != ''">
AND t.trainer_type_id = #{trainerTypeId }
</if>
<if test="orderBy != null and orderBy != ''">
ORDER BY ${orderBy } ${sortType }
</if>
</where>
</sql> <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from t_hr_td_trainer
where id = #{id }
</delete> <insert id="insert" parameterType="com.dayhr.web.module.hr.td.elearn.model.Trainer" useGeneratedKeys="true" keyProperty="id">
insert into t_hr_td_trainer (id, name, title,
recommend, trainer_type_id, phone,
email, address, pro_field,
intro, head_img, labels,
corp_id, creater_id, create_time,
modifier_id, modify_time)
values (#{id,jdbcType=CHAR}, #{name,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR},
#{recommend,jdbcType=INTEGER}, #{trainerTypeId,jdbcType=CHAR}, #{phone,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{proField,jdbcType=VARCHAR},
#{intro,jdbcType=VARCHAR}, #{headImg,jdbcType=VARCHAR}, #{labels,jdbcType=VARCHAR},
#{corpId,jdbcType=INTEGER}, #{createrId,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},
#{modifierId,jdbcType=INTEGER}, #{modifyTime,jdbcType=TIMESTAMP})
</insert> <update id="updateByPrimaryKey" parameterType="com.dayhr.web.module.hr.td.elearn.model.Trainer" >
UPDATE t_hr_td_trainer
<set>
<if test="name != null and name != ''">
name = #{name },
</if>
<if test="title != null and title != ''">
title = #{title },
</if>
<if test="recommend != null and recommend != ''">
recommend = #{recommend },
</if>
<if test="trainerTypeId != null and trainerTypeId != ''">
trainer_type_id = #{trainerTypeId },
</if>
<if test="phone != null and phone != ''">
phone = #{phone },
</if>
<if test="email != null and email != ''">
email = #{email },
</if>
<if test="address != null and address != ''">
address = #{address },
</if>
<if test="proField != null and proField != ''">
pro_field = #{proField },
</if>
<if test="intro != null and intro != ''">
intro = #{intro },
</if>
<if test="headImg != null and headImg != ''">
head_img = #{headImg },
</if>
<if test="labels != null and labels != ''">
labels = #{labels },
</if>
<if test="corpId != null and corpId != ''">
corp_id = #{corpId },
</if>
<if test="createrId != null and createrId != ''">
creater_id = #{createrId },
</if>
<if test="createTime != null and createTime != ''">
create_time = #{createTime },
</if>
<if test="modifierId != null and modifierId != ''">
modifier_id = #{modifierId },
</if>
<if test="modifyTime != null and modifyTime != ''">
modify_time = #{modifyTime }
</if>
</set>
WHERE id = #{id }
</update> <select id="selectByPrimaryKey" resultMap="BaseChildResultMap" parameterType="java.lang.String" >
SELECT
<include refid="Base_Column_List" />
FROM t_hr_td_trainer t
LEFT JOIN t_hr_td_trainertype t1 ON t.trainer_type_id = t1.id
WHERE t.id = #{id }
</select> <select id="selectAll" resultMap="BaseResultMap" >
select
id, name, title, recommend, trainer_type_id, phone, email, address, pro_field,
intro, head_img, labels, corp_id, creater_id, create_time, modifier_id, modify_time
from t_hr_td_trainer
</select> <select id="selectListByCondition" parameterType="com.dayhr.web.module.hr.td.elearn.param.TrainerQueryParam" resultMap="BaseChildResultMap">
SELECT
<include refid="Base_Column_List" />
FROM t_hr_td_trainer t
LEFT JOIN t_hr_td_trainertype t1 ON t.trainer_type_id = t1.id
<include refid="select_Where_Clause" />
</select> </mapper>

二:子查询id="selecLabels":注意此处value对应父查询结果中labels:

<?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="com.dayhr.web.module.hr.td.elearn.mapper.TrainerLabelMapper" > <!-- 对应表中字段 -->
<resultMap id="BaseResultMap" type="com.dayhr.web.module.hr.td.elearn.model.TrainerLabel" >
<id column="id" property="id" />
<result column="trainer_label" property="trainerLabel" jdbcType="VARCHAR" />
<result column="built_in" property="builtIn" jdbcType="INTEGER" />
<result column="corp_id" property="corpId" jdbcType="INTEGER" />
<result column="creater_id" property="createrId" jdbcType="INTEGER" />
<result column="creater_time" property="createrTime" jdbcType="TIMESTAMP" />
<result column="modifier_id" property="modifierId" jdbcType="INTEGER" />
<result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" />
</resultMap> <!-- 返回字段 -->
<resultMap id="BaseResultRespMap" type="com.dayhr.web.module.hr.td.elearn.response.TrainerLabelResp" >
<id column="id" property="id" />
<result column="trainer_label" property="trainerLabel" jdbcType="VARCHAR" />
<result column="built_in" property="builtIn" jdbcType="INTEGER" />
<result column="corp_id" property="corpId" jdbcType="INTEGER" />
<result column="creater_id" property="createrId" jdbcType="INTEGER" />
<result column="creater_time" property="createrTime" jdbcType="TIMESTAMP" />
<result column="modifier_id" property="modifierId" jdbcType="INTEGER" />
<result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" />
<result column="labels" property="labels" />
</resultMap> <!-- 表中基础字段 -->
<sql id="Base_Column_List">
id, trainer_label, built_in, corp_id, creater_id, creater_time, modifier_id, modify_time
</sql> <!-- 查询条件 -->
<sql id="select_Where_Clause">
<where>
1=1
<if test="id != null and id != ''">
AND id = #{id }
</if>
<if test="corpId != null and corpId != ''">
AND corp_id = #{corpId }
</if>
<if test="trainerLabel != null and trainerLabel != ''">
AND trainer_label like CONCAT('%',#{trainerLabel },'%' )
</if>
<if test="orderBy != null and orderBy != ''">
ORDER BY ${orderBy } ${sortType }
</if>
</where>
</sql> <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from t_hr_td_trainerlabel
where id = #{id,jdbcType=CHAR}
</delete> <insert id="insert" parameterType="com.dayhr.web.module.hr.td.elearn.model.TrainerLabel" useGeneratedKeys="true" keyProperty="id">
insert into t_hr_td_trainerlabel (id, trainer_label, built_in, corp_id,
creater_id, creater_time, modifier_id,
modify_time)
values (#{id },#{trainerLabel,jdbcType=VARCHAR}, #{builtIn,jdbcType=INTEGER}, #{corpId,jdbcType=INTEGER},
#{createrId,jdbcType=INTEGER}, #{createrTime,jdbcType=TIMESTAMP}, #{modifierId,jdbcType=INTEGER},
#{modifyTime,jdbcType=TIMESTAMP})
</insert> <update id="updateByPrimaryKey" parameterType="com.dayhr.web.module.hr.td.elearn.model.TrainerLabel" >
UPDATE t_hr_td_trainerlabel
<set>
<if test="trainerLabel != null and trainerLabel != ''">
trainer_label = #{trainerLabel },
</if>
<if test="builtIn != null and builtIn != ''">
built_in = #{builtIn },
</if>
<if test="corpId != null and corpId != ''">
corp_id = #{corpId },
</if>
<if test="createrId != null and createrId != ''">
creater_id = #{createrId },
</if>
<if test="createTime != null and createTime != ''">
create_time = #{createTime },
</if>
<if test="modifierId != null and modifierId != ''">
modifier_id = #{modifierId },
</if>
<if test="modifyTime != null and modifyTime != ''">
modify_time = #{modifyTime }
</if>
</set>
WHERE id = #{id }
</update> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
id, trainer_label, built_in, corp_id, creater_id, creater_time, modifier_id, modify_time
from t_hr_td_trainerlabel
where id = #{id }
</select> <select id="selectAll" resultMap="BaseResultMap" >
select
id, trainer_label, built_in, corp_id, creater_id, creater_time, modifier_id, modify_time
from t_hr_td_trainerlabel
</select> <!-- 验证标签是否存在 -->
<select id="checkLabel" parameterType="com.dayhr.web.module.hr.td.elearn.param.TrainerLabelQueryParam" resultType="Integer">
SELECT COUNT(*)
FROM t_hr_td_trainerlabel
<include refid="select_Where_Clause" />
</select> <!-- 按条件查询标签 -->
<select id="selectLabelList" resultMap="BaseResultRespMap" parameterType="com.dayhr.web.module.hr.td.elearn.param.TrainerLabelQueryParam" >
SELECT
<include refid="Base_Column_List" />
FROM t_hr_td_trainerlabel
<include refid="select_Where_Clause" />
</select> <!-- 子查询 in()会报错,加'0'处理 -->
<select id="selecLabels" resultMap="BaseResultRespMap" >
select
id, trainer_label, built_in, corp_id, creater_id, creater_time, modifier_id, modify_time
from t_hr_td_trainerlabel
where id in ('0'
      <if test="value != null and value.length !=0">
   ,${value }
   </if>
   )
</select> </mapper>

MyBatis子查询的更多相关文章

  1. Mybatis 子查询

    在查询数据库时,需要以查询结果为查询条件进行关联查询. 在mybatis中通过association标签和collection标签实现子查询. 1. collection(集合)和associatio ...

  2. coding++:mybatis 嵌套查询子查询column传多个参数描述

    mybatis 嵌套查询子查询column传多个参数如下: 2.代码示例 备注:注意,相同颜色的单词都是有关联的 <resultMap id="blogResult" typ ...

  3. Mybatis 一对多延迟加载,并且子查询中与主表字段不对应 (19)

    Mybatis  一对多延迟加载,并且子查询中与主表字段不对应应用说明. 实现一对多关联(懒加载),一个教研组对应多个教师,既:教师的教研编号与教研组的教研编号关联,并且教师关联教研组外键与教研组编号 ...

  4. mybatis中collection子查询注入参数为null

    具体实现参照网上,但是可能遇到注入参数为null的情况,经过查阅及自己测试记录一下: 子查询的参数中,有<if test="">之类,需要指定别名,通过 http:// ...

  5. MyBatis关联查询分页

    背景:单表好说,假如是MySQL的话,直接limit就行了. 对于多对多或者一对多的情况,假如分页的对象不是所有结果集,而是对一边分页,那么可以采用子查询分页,再与另外一张表关联查询,比如: sele ...

  6. mybatis分页查询的万能模板

    分页查询项目里太多了,而这种分页查询,在mybatis里面的配置几乎一模一样,今天就整理一个比较好和实用的模板,供以后直接Ctrl+C <select id="queryMember& ...

  7. mybatis分页查询,SqlServer 2008 查询速度很慢

    一个业务场景,需要进行union查询: 查询速度非常慢,大概要37秒: 直接复制sql在数据库客户端执行,速度很快,由此可知是mybatis的原因,在网上搜索,可以配置fetchSize=" ...

  8. MyBatis高级查询 一对一映射

    drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...

  9. MyBatis 关联查询的实现:一对多

    有2个实体:用户.订单,一个用户可以拥有多个订单,同时这多个订单属于一个用户,即一对多. user_tb: order_tb: 在“多”的一方(order)添加“一”的一方(user)的主键(user ...

随机推荐

  1. ubuntu16.04图形界面安装中文输入法,中文展示

    打开system Settings 设置   打开设置语言   安装Language Support   点击installed languages 选择chinese 打勾,安装   安装IBus框 ...

  2. 【shell 练习4】编写Shell用户管理脚本(二)

    一.创建.删除.查看用户,随机生成八位数密码 #!/bin/bash #Author:yanglt #!/bin/bash #Author:yanglt #Blog:https://www.cnblo ...

  3. Deep Residual Learning for Image Recognition论文笔记

    Abstract We present a residual learning framework to ease the training of networks that are substant ...

  4. AttributeError: 'TimeLimit' object has no attribute 'monitor'

    原报错代码部分: env.monitor.start(monitor_path, resume=True, video_callable=lambda count: count % record_vi ...

  5. “hello world!”团队第三次会议

    团队“hello world!”团队召开的第三次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 一.会议时间 2017年10 ...

  6. Thunder团队Beta周贡献分分配结果

    小组名称:Thunder 项目名称:爱阅app 组长:王航 成员:李传康.翟宇豪.邹双黛.苗威.宋雨.胡佑蓉.杨梓瑞 分配规则 规则1:基础分,拿出总分的20%(8分)进行均分,剩下的80%(32分) ...

  7. 重构 之 总结代码的坏味道 Bad Smell (一) 重复代码 过长函数 过大的类 过长参数列 发散式变化 霰弹式修改

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  8. 透过汇编另眼看世界之DLL导出函数调用

    前言:我一直对DLL技术充满好奇,一方面是因为我对DLL的导入/导出机制还不是特别的了解,另一面是因为我发现:DLL技术在Windows平台下占有重要的地位,几乎所有的Win32 API都是以导出函数 ...

  9. 201621044079 week13 网络

    作业13-网络 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以 ...

  10. UVA 167 R-The Sultan's Successors

    https://vjudge.net/contest/68264#problem/R The Sultan of Nubia has no children, so she has decided t ...