关于mybatis:学习笔记-MyBatis-MyBatis-开发

48次阅读

共计 18846 个字符,预计需要花费 48 分钟才能阅读完成。

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

基于 MyBaits 开发的过程

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 接口开发的过程

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 片段,防止反复 

正文完
 0