官方文档https://mybatis.org/mybatis-3/zh/index.html

基于MyBaits开发的过程

image

1.sqlSession.selectList("EmpMapper.findAll"):SqlSession中封装的操作数据库的方法,MyBatis中操作数据库最主要的对象和所有的方法。
2.EmpMapper.findAll:String字符串,方法中要执行的sql语句的位置,即映射的sql语句。
3.<mapper namespace="EmpMapper">:所有的sql语句存在的小区
4.<select id="findAll" resultType="Ring1270.pra.MyBatis.pojo.Emp">类型
    select * from emp
</select>:具体sql所在的门牌号和sql本身+结果集
问题:
封装在Sqlsession对象中的方法在寻找sql的映射时,传入的参数是一个需要拼接的(namespace+id)的字符串,这种方式容易发生字符串拼写错误且编译时期不会提示的问题,存在一定的风险。
解决方式:Mapper接口开发
利用接口作为桥梁,在寻找sql映射时,带着相应的参数先走接口,接口对传来的参数进行了检查判断,同时接口会进行编译也就避免了字符串拼写错误和编译不提示的问题。

基于MyBatis接口开发的过程

image

1.sqlSession.getMapper(EmpMapper.class):SqlSession中封装的操作数据库的getMapper()`<T> T getMapper(Class<T> type)`方法
2.EmpMapper.class:接口,规定了方法名(id),参数和返回值接收类型
3.<mapper namespace="Ring1270.pra.MyBatis.Mapper.EmpMapper">:所有的sql语句被规定的小区
4.<select id="findAll" resultType="Ring1270.pra.MyBatis.pojo.Emp">
    select * from emp
</select>:具体sql所在的门牌号和sql本身+结果集类型(和接口中的相对应)
接口Mapper
package Ring1270.pra.MyBatis.Mapper;
import Ring1270.pra.MyBatis.pojo.Emp;
import java.util.List;
public interface EmpMapper {
    /**
 * 根据id查询员工信息
 * @param id
 * @return Emp
 */ public Emp findById(Integer id);
    public List<Emp> findAll();
}
1、创建一个接口,接口的全限定类名和mapper文件的namespace值要相同
2、mapper文件中每条要执行的SQL语句,在接口中要添加一个对应的方法,并且接口中的方法名和SQL标签上的id值相同
3、Mapper接口中方法接收的参数类型,和mapper.xml中定义的sql的接收的参数类型要相同
4、接口中方法的返回值类型和SQL标签上的resultType即返回值类型相同(如果方法返回值是集合,resultType只需要指定集合中的泛型)

关于XxxMapper.xml

实际开发中的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="xxx.xxx.xxx.xx.dao.OrgUnitsMapper">
    <resultMap id="BaseResultMap" type="xxx.xxx.xxx.xx.OrgUnits">
        <id column="id" jdbcType="BIGINT" property="id"/> 
        <result column="create_by" jdbcType="BIGINT" property="createBy"/> 
        <result column="create_time" jdbcType="DATE" property="createTime"/> 
        <result column="update_by" jdbcType="BIGINT" property="updateBy"/> 
        <result column="update_time" jdbcType="DATE" property="updateTime"/> 
        <result column="delete_flag" jdbcType="TINYINT" property="deleteFlag"/>
        <result column="unit_update" jdbcType="TINYINT" property="unitUpdate"/>
        <result column="import_flag" jdbcType="TINYINT" property="importFlag"/> 
        <result column="company_id" jdbcType="BIGINT" property="companyId"/> 
        <result column="unit_code" jdbcType="VARCHAR" property="unitCode"/> 
        <result column="unit_type" jdbcType="VARCHAR" property="unitType"/> 
        <result column="unit_fname_cn" jdbcType="VARCHAR" property="unitFnameCn"/> 
        <result column="unit_sname_cn" jdbcType="VARCHAR" property="unitSnameCn"/> 
        <result column="unit_fname_en" jdbcType="VARCHAR" property="unitFnameEn"/> 
        <result column="unit_sname_en" jdbcType="VARCHAR" property="unitSnameEn"/> 
        <result column="unit_pid" jdbcType="BIGINT" property="unitPid"/> 
        <result column="unit_scope" jdbcType="VARCHAR" property="unitScope"/> 
        <result column="unit_leveltype" jdbcType="VARCHAR" property="unitLeveltype"/> 
        <result column="unit_empid" jdbcType="BIGINT" property="unitEmpid"/> 
        <result column="unit_chargeid" jdbcType="BIGINT" property="unitChargeid"/> 
        <result column="unit_hcityid" jdbcType="BIGINT" property="unitHcityid"/> 
        <result column="unit_wcityid" jdbcType="BIGINT" property="unitWcityid"/> 
        <result column="unit_cotcid" jdbcType="BIGINT" property="unitCotcid"/> 
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="unit_project" jdbcType="BIGINT" property="unitProject"/>
        <result column="unit_sdate" jdbcType="DATE" property="unitSdate"/> 
        <result column="unit_edate" jdbcType="DATE" property="unitEdate"/> 
        <result column="unit_invreason" jdbcType="VARCHAR" property="unitInvreason"/> 
        <result column="comment" jdbcType="VARCHAR" property="comment"/>
        <result column="unit_oaid" jdbcType="VARCHAR" property="unitOaid"/>
        <result column="unit_qywxid" jdbcType="VARCHAR" property="unitQywxid"/>
        <result column="unit_ddid" jdbcType="VARCHAR" property="unitDdid"/>
        <result column="unit_dlocation" jdbcType="VARCHAR" property="unitDlocation"/>
        <result column="unit_dlocationX" jdbcType="VARCHAR" property="unitDlocationX"/>
        <result column="unit_dlocationY" jdbcType="VARCHAR" property="unitDlocationY"/>
        <result column="unit_ischart" jdbcType="TINYINT" property="unitIschart"/>
        <result column="unit_posttype" jdbcType="TINYINT" property="unitPosttype"/>
        <result column="unit_company" jdbcType="BIGINT" property="unitCompany"/>
        <result column="unit_dept" jdbcType="BIGINT" property="unitDept"/>
    </resultMap>
    <sql id="BaseColumnList">
        id,create_by,create_time,update_by,update_time,delete_flag,unit_update,import_flag,company_id,unit_code,unit_type,unit_fname_cn,unit_sname_cn,unit_fname_en
        ,unit_sname_en,unit_pid,unit_scope,unit_leveltype,unit_empid
        ,unit_chargeid,unit_hcityid,unit_wcityid,unit_cotcid
        ,status,unit_project,unit_sdate,unit_edate,unit_invreason,comment,unit_oaid,unit_qywxid,unit_ddid
        ,unit_dlocation,unit_dlocationX,unit_dlocationY,unit_ischart,unit_posttype,unit_company,unit_dept
    </sql>
    
    <select id="selPageByOrgUnits" resultMap="BaseResultMap">
      select
      <include refid="BaseColumnList" />
      from org_units
      where 1=1
      <if test="orgUnits != null ">
         <if test="orgUnits.id!=null and orgUnits.id != ''  "> 
        and id = #{orgUnits.id}
        </if><if test="orgUnits.createBy!=null and orgUnits.createBy != ''  "> 
        and create_by = #{orgUnits.createBy}
        </if><if test="orgUnits.createTime!=null and orgUnits.createTime != ''  "> 
        and create_time = #{orgUnits.createTime}
        </if><if test="orgUnits.updateBy!=null and orgUnits.updateBy != ''  "> 
        and update_by = #{orgUnits.updateBy}
        </if><if test="orgUnits.updateTime!=null and orgUnits.updateTime != ''  "> 
        and update_time = #{orgUnits.updateTime}
        </if><if test="orgUnits.deleteFlag!=null and orgUnits.deleteFlag != ''  "> 
        and delete_flag = #{orgUnits.deleteFlag}
        </if><if test="orgUnits.unitUpdate!=null and orgUnits.unitUpdate != ''  ">
        and unit_update = #{orgUnits.unitUpdate}
        </if><if test="orgUnits.importFlag!=null and orgUnits.importFlag != ''  ">
        and import_flag = #{orgUnits.importFlag}
        </if><if test="orgUnits.companyId!=null and orgUnits.companyId != '' and orgUnits.companyId != 0 "> 
        and company_id = #{orgUnits.companyId}
        </if><if test="orgUnits.unitCode!=null and orgUnits.unitCode != ''  "> 
        and unit_code = #{orgUnits.unitCode}
        </if><if test="orgUnits.unitType!=null and orgUnits.unitType != ''  "> 
        and unit_type = #{orgUnits.unitType}
        </if><if test="orgUnits.unitFnameCn!=null and orgUnits.unitFnameCn != ''  "> 
        and unit_fname_cn like concat('%',#{orgUnits.unitFnameCn},'%')
        </if><if test="orgUnits.unitSnameCn!=null and orgUnits.unitSnameCn != ''  "> 
        and unit_sname_cn = #{orgUnits.unitSnameCn}
        </if><if test="orgUnits.unitFnameEn!=null and orgUnits.unitFnameEn != ''  "> 
        and unit_fname_en = #{orgUnits.unitFnameEn}
        </if><if test="orgUnits.unitSnameEn!=null and orgUnits.unitSnameEn != ''  "> 
        and unit_sname_en = #{orgUnits.unitSnameEn}
        </if><if test="orgUnits.unitPid!=null and orgUnits.unitPid != ''  "> 
        and unit_pid = #{orgUnits.unitPid}
        </if><if test="orgUnits.unitScope!=null and orgUnits.unitScope != ''  "> 
        and unit_scope = #{orgUnits.unitScope}
        </if><if test="orgUnits.unitLeveltype!=null and orgUnits.unitLeveltype != ''  "> 
        and unit_leveltype = #{orgUnits.unitLeveltype}
        </if><if test="orgUnits.unitEmpid!=null and orgUnits.unitEmpid != ''  "> 
        and unit_empid = #{orgUnits.unitEmpid}
        </if><if test="orgUnits.unitChargeid!=null and orgUnits.unitChargeid != ''  "> 
        and unit_chargeid = #{orgUnits.unitChargeid}
        </if><if test="orgUnits.unitHcityid!=null and orgUnits.unitHcityid != ''  "> 
        and unit_hcityid = #{orgUnits.unitHcityid}
        </if><if test="orgUnits.unitWcityid!=null and orgUnits.unitWcityid != ''  "> 
        and unit_wcityid = #{orgUnits.unitWcityid}
        </if><if test="orgUnits.unitCotcid!=null and orgUnits.unitCotcid != ''  "> 
        and unit_cotcid = #{orgUnits.unitCotcid}
        </if><if test="orgUnits.status!=null and orgUnits.status != ''  ">
        and status = #{orgUnits.status}
            <!-- 生效状态卡生效、失效时间 -->
 <if test="orgUnits.status == '02valid'">
                and (now() between unit_sdate and unit_edate)
                <!-- and (date_format(sdate,'%Y-%m') &lt;= date_format(now(),'%Y-%m') and date_format(edate,'%Y-%m') &gt; date_format(now(),'%Y-%m')) -->
 </if>
        </if><if test="orgUnits.unitProject!=null and orgUnits.unitProject != ''  ">
            and unit_project = #{orgUnits.unitProject}
        </if><if test="orgUnits.unitSdate!=null and orgUnits.unitSdate != ''  ">
        and unit_sdate = #{orgUnits.unitSdate}
        </if><if test="orgUnits.unitEdate!=null and orgUnits.unitEdate != ''  "> 
        and unit_edate = #{orgUnits.unitEdate}
        </if><if test="orgUnits.unitInvreason!=null and orgUnits.unitInvreason != ''  ">
        and unit_invreason = #{orgUnits.unitInvreason}
        </if><if test="orgUnits.comment!=null and orgUnits.comment != ''  "> 
        and comment = #{orgUnits.comment}
        </if><if test="orgUnits.unitIschart!=null and orgUnits.unitIschart != ''  ">
            and unit_ischart = #{orgUnits.unitIschart}
        </if>
      </if>
   </select>
    <select id="selectTreeById" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from (
        SELECT
        <include refid="BaseColumnList" />
        ,@le:= IF (unit_pid = 0 ,0,    IF( LOCATE(CONCAT('|',unit_pid,':'), @pathlevel) > 0 ,SUBSTRING_INDEX(SUBSTRING_INDEX(@pathlevel,CONCAT('|',unit_pid,':'),-1),'|',1) +1    ,@le+1)) levels,
        @pathlevel:= CONCAT(@pathlevel,'|',id,':', @le ,'|') pathlevel,
        @pathnodes:= IF( unit_pid =0,',0', CONCAT_WS(',', IF( LOCATE(CONCAT('|',unit_pid,':'),@pathall) > 0 ,SUBSTRING_INDEX(SUBSTRING_INDEX(@pathall,CONCAT('|',unit_pid,':'),-1),'|',1) ,@pathnodes ) ,unit_pid ) ) paths,
        @pathall:=CONCAT(@pathall,'|',id,':', @pathnodes ,'|') pathall
        FROM org_units,    (SELECT @le:=0,@pathlevel:='', @pathall:='',@pathnodes:='') vv
        where delete_flag=0
        <!-- ORDER BY unit_pid,unit_order -->
 ORDER BY unit_pid
        ) src
        <where>
            (FIND_IN_SET(#{nodeId}, paths) OR id = #{nodeId}) and delete_flag = 0
            <if test="status!=null and status != '' ">
                and  status in
                <foreach collection="status.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
                <if test="status == '02valid'">
                    and (now() between unit_sdate and unit_edate)
                    <!-- and (date_format(sdate,'%Y-%m') &lt;= date_format(now(),'%Y-%m') and date_format(edate,'%Y-%m') &gt; date_format(now(),'%Y-%m')) -->
 </if>
            </if>
            <if test="unitType!=null">
                and unit_type in
                <foreach collection="unitType.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
            </if>
            <!--
 <if test="unitCode!=null"> and unit_code like '%${unitCode}%' </if> --> #{LangWhere}
        </where>
        <!-- order by unit_order -->
 </select>
    <select id="selectTreeById2" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from org_units
        <where>
            delete_flag = 0
            <if test="companyId!=null and companyId!='' and companyId!=0">
                and company_id = #{companyId}
            </if>
            <if test="id!=null">
                <!-- and id=${id} -->
 and id in
                <foreach collection="id.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
            </if>
            <if test="status!=null and status != ''">
                and status in
                <foreach collection="status.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
                <if test="status.contains('02valid') ">
                    and (now() between unit_sdate and unit_edate)
                    <!-- and (date_format(sdate,'%Y-%m') &lt;= date_format(now(),'%Y-%m') and date_format(edate,'%Y-%m') &gt; date_format(now(),'%Y-%m')) -->
 </if>
            </if>
            <if test="unitPid!=null and unitPid!=''">
                and (unit_pid in
                <foreach collection="unitPid.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
                or id in
                <foreach collection="unitPid.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
                )
            </if>
            <if test="unitTypes!=null">
                and unit_type in
                <foreach collection="unitTypes.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
            </if>com.hrocloud.znjs.orgemp.model.OrgUnitsChart
            <if test="unitCode!=null">
                and unit_code like concat('%',#{unitCode},'%')
            </if>
        </where>
    </select>
    <select id="selectByObjIds" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from org_units
        <where>
            delete_flag = 0
            <!-- and (date_format(sdate,'%Y-%m') &lt;= date_format(now(),'%Y-%m') and date_format(edate,'%Y-%m') &gt;= date_format(now(),'%Y-%m')) -->
 and status = '02valid' and (now() between unit_sdate and unit_edate)
            <if test="orgUnits.unitPid!=null and orgUnits.unitPid!=''">
                and unit_pid in
                <foreach collection="orgUnits.unitPid.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
            </if>
            <if test="orgUnits.unitType!=null and orgUnits.unitType!=''">
                and unit_type in
                <foreach collection="orgUnits.unitType.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
                </foreach>
            </if>
        </where>
    </select>
    <!--查询父节点和子节点-->
 <select id="selectIdandPid" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from org_units
        <where>
            delete_flag = 0 and unit_update = 0
            <!-- and (date_format(sdate,'%Y-%m') &lt;= date_format(now(),'%Y-%m') and date_format(edate,'%Y-%m') &gt;= date_format(now(),'%Y-%m')) -->
--             and status = '02valid' and (now() between unit_sdate and unit_edate)
            and ( unit_pid in
            <foreach collection="orgUnits.unitPid.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            or id in
            <foreach collection="orgUnits.unitPid.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </where>
    </select>
    
    <select id="selectSubUnitById" resultMap="BaseResultMap">
      select <include refid="BaseColumnList" />
      from org_units
      where delete_flag = 0 and unit_update = 0
      <if test="orgUnits != null ">
         <if test="orgUnits.id!=null and orgUnits.id != ''  "> 
           and id = #{orgUnits.id}
           </if><if test="orgUnits.qcUnitAuth!=null and orgUnits.qcUnitAuth != '' ">
           <!-- 数据权限 -->
 and id in
            <foreach collection="orgUnits.qcUnitAuth.split(',')" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
           </if><if test="orgUnits.companyId!=null and orgUnits.companyId != '' and orgUnits.companyId != 0 "> 
           and company_id = #{orgUnits.companyId}
           </if><if test="orgUnits.unitCode!=null and orgUnits.unitCode != ''  "> 
           and unit_code like concat('%',#{orgUnits.unitCode},'%')
           </if><if test="orgUnits.unitType!=null and orgUnits.unitType != ''  "> 
           and unit_type = #{orgUnits.unitType}
           </if><if test="orgUnits.unitFnameCn!=null and orgUnits.unitFnameCn != ''  "> 
           and unit_fname_cn like concat('%',#{orgUnits.unitFnameCn},'%')
           </if><if test="orgUnits.unitSnameCn!=null and orgUnits.unitSnameCn != ''  "> 
           and unit_sname_cn like concat('%',#{orgUnits.unitSnameCn},'%')
           </if><if test="orgUnits.unitFnameEn!=null and orgUnits.unitFnameEn != ''  "> 
           and unit_fname_en like concat('%',#{orgUnits.unitFnameEn},'%')
           </if><if test="orgUnits.unitSnameEn!=null and orgUnits.unitSnameEn != ''  "> 
           and unit_sname_en like concat('%',#{orgUnits.unitSnameEn},'%')
           </if><if test="orgUnits.unitPid!=null and orgUnits.unitPid != ''  ">
              <choose>
                 <when test="orgUnits.unitType!=null and orgUnits.unitType == '02dept'.toString()  ">
                    <!-- 所有下级部门 -->
 and (FIND_IN_SET(id,fun_getAllSubOrgDept(#{orgUnits.unitPid}))
                 </when>
                 <otherwise>
                    <!-- 所有下级 -->
 and (FIND_IN_SET(id,fun_getAllSubOrgUnits(#{orgUnits.unitPid}))
                 </otherwise>
              </choose>
              <!-- 是否包含自己 -->
 <choose>
               <when test="orgUnits.qcIncludeSelf == '1'.toString() ">
                  or id=#{orgUnits.unitPid} )
               </when>
               <otherwise>
                  )
               </otherwise>
            </choose>
           </if><if test="orgUnits.unitLeveltype!=null and orgUnits.unitLeveltype != ''  "> 
           and unit_leveltype = #{orgUnits.unitLeveltype}
           </if><if test="orgUnits.unitEmpid!=null and orgUnits.unitEmpid != ''  "> 
           and unit_empid = #{orgUnits.unitEmpid}
           </if><if test="orgUnits.unitChargeid!=null and orgUnits.unitChargeid != ''  "> 
           and unit_chargeid = #{orgUnits.unitChargeid}
           </if><if test="orgUnits.unitHcityid!=null and orgUnits.unitHcityid != ''  "> 
           and unit_hcityid = #{orgUnits.unitHcityid}
           </if><if test="orgUnits.unitWcityid!=null and orgUnits.unitWcityid != ''  "> 
           and unit_wcityid = #{orgUnits.unitWcityid}
           </if><if test="orgUnits.unitCotcid!=null and orgUnits.unitCotcid != ''  "> 
           and unit_cotcid = #{orgUnits.unitCotcid}
            </if><if test="orgUnits.unitIschart!=null and orgUnits.unitIschart != ''  ">
            and unit_ischart = #{orgUnits.unitIschart}
           </if><if test="orgUnits.status!=null and orgUnits.status != ''  ">
           and status = #{orgUnits.status}
               <!-- 生效状态卡生效、失效时间 -->
 <if test="orgUnits.status == '02valid'.toString()">
                  <choose>
                     <when test="orgUnits.qcDate!=null and orgUnits.qcDate !='' ">
                      and (Date(#{orgUnits.qcDate}) between unit_sdate and unit_edate)
                     </when>
                  <otherwise>
                  and (now() between unit_sdate and unit_edate)
                  </otherwise>
                   </choose>
               </if>
           </if><if test="orgUnits.unitProject!=null and orgUnits.unitProject != ''  ">
               and unit_project = #{orgUnits.unitProject}
           </if>
      </if>
   </select>
   
   <!-- 2020-12-04 darren add 用于获取看板总公司-营销中心部门 -->
 <select id="selectSubUnitForKBZGSYXZXDETP" resultMap="BaseResultMap">
      select <include refid="BaseColumnList" />
      from org_units
      where delete_flag = 0 and unit_update = 0
      <if test="orgUnits != null ">
           <if test="orgUnits.unitType!=null and orgUnits.unitType != ''  "> 
           and unit_type = #{orgUnits.unitType}
           </if><if test="orgUnits.unitPid!=null and orgUnits.unitPid != ''  ">
              <choose>
                 <when test="orgUnits.unitType!=null and orgUnits.unitType == '02dept'.toString()  ">
                    <!-- 所有下级部门 -->
 and (FIND_IN_SET(id,fun_getAllSubOrgDept(#{orgUnits.unitPid}))
                 </when>
                 <otherwise>
                    <!-- 所有下级 -->
 and (FIND_IN_SET(id,fun_getAllSubOrgUnits(#{orgUnits.unitPid}))
                 </otherwise>
              </choose>
              <!-- 是否包含自己 -->
 <choose>
               <when test="orgUnits.qcIncludeSelf == '1'.toString() ">
                  or id=#{orgUnits.unitPid} )
               </when>
               <otherwise>
                  )
               </otherwise>
            </choose>
           </if><if test="orgUnits.status!=null and orgUnits.status != ''  ">
           and status = #{orgUnits.status}
               <!-- 生效状态卡生效、失效时间 -->
 <if test="orgUnits.status == '02valid'.toString()">
                  <choose>
                     <when test="orgUnits.qcDate!=null and orgUnits.qcDate !='' ">
                      and (Date(#{orgUnits.qcDate}) between unit_sdate and unit_edate)
                     </when>
                  <otherwise>
                  and (now() between unit_sdate and unit_edate)
                  </otherwise>
                   </choose>
               </if>
           </if>
      </if>
   </select>
    <!-- 组织架构图特殊处理mapper   -->
 <resultMap id="chartResultMap" type="xxxx.xxx.xxx.xxx.xx.OrgUnitsChart">
        <id column="id" jdbcType="BIGINT" property="unitId"/>
        <result column="unit_fname_cn" jdbcType="VARCHAR" property="label"/>
        <result column="unit_empid" jdbcType="VARCHAR" property="unitEmpid"/>
    </resultMap>
    <select id="selectChildren" resultMap="chartResultMap" parameterType="java.lang.Integer">
        select
        id,unit_fname_cn,unit_empid
        from org_units
        where delete_flag = 0
        and unit_update = 0
        and status = '02valid'
        and unit_ischart = 1
        and (now() between unit_sdate and unit_edate)
        and unit_pid = #{unitPid}
    </select>
    <select id="selectById" resultMap="chartResultMap" parameterType="java.lang.Integer">
        select
        id,unit_fname_cn,unit_empid
        from org_units
        where delete_flag = 0
        and unit_update = 0
        and status = '02valid'
        and unit_ischart = 1
        and (now() between unit_sdate and unit_edate)
        and id = #{id}
    </select>
    <select id="selectByPId" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from org_units
        <where>
            delete_flag = 0 and unit_update = 0
            <!-- and (date_format(sdate,'%Y-%m') &lt;= date_format(now(),'%Y-%m') and date_format(edate,'%Y-%m') &gt;= date_format(now(),'%Y-%m')) -->
 and status = '02valid' and (now() between unit_sdate and unit_edate)
            and  unit_pid =#{pid}
        </where>
    </select>
</mapper>
<?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="xxx.xxx.xxx.xx.dao.OrgUnitsMapper">
    <resultMap id="BaseResultMap" type="xxx.xxx.xxx.xx.OrgUnits">
    </resultMap>

    <sql id="BaseColumnList">
    </sql>
    <select id="selectChildren" resultMap="chartResultMap" parameterType="java.lang.Integer">
    </select>    
</mapper>
动态SQL标签
<if test>标签
<if>标签:是根据test属性中的布尔表达式的值,从而决定是否执行包含在其中的SQL片段。如果判断结果为true,则执行其中的SQL片段;如果结果为false,则不执行其中的SQL片段
<where>标签
<where>标签:用于对包含在其中的SQL片段进行检索,在需要时可以生成where关键字,并且在需要时会剔除多余的连接词(比如and或者or)
<choose when otherwise>标签
遇到choose标签判断when里面的条件,条件满足执行里面的sql,不满足继续判断下一个when标签的里面的条件,满足执行sql,不满足继续向下判断,都不满足执行otherwise标签里面的sql,类似switch,case,default。
<foreach>标签
foreach标签:可以对传过来的参数数组或集合进行遍历,以下是foreach标签上的各个属性介绍:
1.item:必需,若collection为数组或List集合时,item表示其中的元素,若collection为map中的key,item表示map中value(集合或数组)中的元素
2.open:可选,表示遍历生成的SQL片段以什么开始,最常用的是左括号'('
3.collection:必需,值为遍历的集合类型,例如:如果参数只是一个数组或List集合,则collection的值为array或list;如果传的是多个参数,用map封装,collection则指定为map中的key。
4.close:可选,表示遍历生成的SQL片段以什么结束,最常用的是右括号')'
5.separator:可选,每次遍历后给生成的SQL片段后面指定间隔符
6.index:当前迭代的序号
<include>标签
抽取公共sql片段,避免重复

Ring1270
1 声望1 粉丝