1. 程式人生 > >mybayis項目使用的Mapping文件使用總結參考(二)

mybayis項目使用的Mapping文件使用總結參考(二)

總結 sco != ont 查看 tel ctype use upd

針對in字句中的數組使用方法

<select id="getCpProfileNamesByIds" resultType="string">
  select name from mega_mis_smpp where id in
  <foreach item="ids" index="index" collection="array" open="(" separator="," close=")">
   #{ids}
  </foreach> 
 </select>

針對in字句中的Collection使用方法

存儲過程的使用:

 <select id="FactoryNumberDao.getNewFactoryCode" resultMap="BaseResultMap" >
  DECLARE @factoryCode varchar(5)
  EXEC P_Factory_Number
  @factoryCode = @factoryCode OUTPUT
  SELECT @factoryCode as factoryCode
 </select>

Ognl的使用:

 <select id="AreaDao.findByPageArea.count"
resultType="java.lang.Long"> SELECT count(*) from gs_area <include refid="areaDaoDynamicWhere"/> </select> <sql id="areaDaoDynamicWhere"> <!-- ognl訪問靜態方法的表達式 [email protected]@method(args),以下為調用框架中的Ognl.isNotEmpty()方法,還有其它方法如isNotBlank()可以使用,具體請查看Ognl類
--> <where> <!-- del_flag = ‘0‘ --> <if test="@[email protected](areaCode)"> and area_code = #{areaCode} </if> <if test="@[email protected](areaName)"> and area_name like ‘%${areaName}%‘ </if> </where> </sql>

sql標簽:

<sql id="Base_Column_List" >
    factory_code, factory_name, factory_user, factory_desc, tel, email, update_time, 
    state_flag
  </sql>
  <select id="FactoryDao.selectByPrimaryKey" resultMap="FactoryInfoMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from dbo.gs_factory
    where factory_code = #{factoryCode,jdbcType=VARCHAR}
  </select>
   <select id="LineDao.findByPageLine.count"
  resultType="java.lang.Long">
  SELECT count(*) from gs_line 
  <include refid="lineDaoDynamicWhere"/>
</select>
  <sql id="lineDaoDynamicWhere">
  <!--
   ognl訪問靜態方法的表達式
   [email protected]@method(args),以下為調用框架中的Ognl.isNotEmpty()方法,還有其它方法如isNotBlank()可以使用,具體請查看Ognl類
  -->
  <where>
   <!-- del_flag = ‘0‘ -->
   <if test="@[email protected](lineCode)">
    and line_code = #{lineCode}
     </if>
   <if test="@[email protected](lineName)">
    and line_name like ‘%${lineName}%‘
     </if>
  </where>
 </sql>
  <sql id="stationTaskReportChartDynamicWhere">
  <!--
   ognl訪問靜態方法的表達式
   [email protected]@method(args),以下為調用框架中的Ognl.isNotEmpty()方法,還有其它方法如isNotBlank()可以使用,具體請查看Ognl類
  -->
               <choose>
                      <when test="endDate!=null and startDate!=null">
                         and  g.plan_task_date BETWEEN #{startDate,jdbcType=TIMESTAMP}  AND #{endDate,jdbcType=TIMESTAMP} 
                     </when>
                      <when test="endDate==null and startDate!=null">
                         and  g.plan_task_date >= #{startDate,jdbcType=TIMESTAMP} 
                     </when>
                       <when test="endDate!=null and startDate==null">
                          <![CDATA[
                             and  g.plan_task_date <= #{startDate,jdbcType=TIMESTAMP} 
                           ]]>
                     </when>
                  </choose> 
                  <if test="stationCodeList!=null">
                              and g.station_code in
                             <foreach item="stationCode" index="index" open="(" close=")"  separator="," collection="stationCodeList">
                                  #{stationCode}
                             </foreach>
                  </if>
                   <if test="userCodeList!=null">
                              and g.user_code in
                             <foreach item="userCode" index="index" open="(" close=")"  separator="," collection="userCodeList">
                                  #{userCode}
                             </foreach>
                  </if>
 </sql>  

trim標簽的使用:

  <insert id="AssetTypeDao.insert" parameterType="com.easyway.eamsg.assetmgt.domain.AssetTypeInfo" >
    insert into dbo.gs_asset_type
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="assetTypeCode != null" >
        asset_type_code,
      </if>
      <if test="assetTypeName != null" >
        asset_type_name,
      </if>
      <if test="assetTypeDesc != null" >
        asset_type_desc,
      </if>
      <if test="natureFlag != null" >
        nature_flag,
      </if>
      <if test="featureFlag != null" >
        feature_flag,
      </if>
      <if test="codingFlag != null" >
        coding_flag,
      </if>
      <if test="matchFlag != null" >
        match_flag,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
      <if test="stateFlag != null" >
        state_flag,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="assetTypeCode != null" >
        #{assetTypeCode,jdbcType=VARCHAR},
      </if>
      <if test="assetTypeName != null" >
        #{assetTypeName,jdbcType=VARCHAR},
      </if>
      <if test="assetTypeDesc != null" >
        #{assetTypeDesc,jdbcType=VARCHAR},
      </if>
      <if test="natureFlag != null" >
        #{natureFlag,jdbcType=CHAR},
      </if>
      <if test="featureFlag != null" >
        #{featureFlag,jdbcType=CHAR},
      </if>
      <if test="codingFlag != null" >
        #{codingFlag,jdbcType=CHAR},
      </if>
      <if test="matchFlag != null" >
        #{matchFlag,jdbcType=CHAR},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="stateFlag != null" >
        #{stateFlag,jdbcType=CHAR},
      </if>
    </trim>
  </insert>

set標簽的使用

 <update id="AssetTypeDao.update" parameterType="com.easyway.eamsg.assetmgt.domain.AssetTypeInfo" >
    update dbo.gs_asset_type
    <set >
      <if test="assetTypeName != null" >
        asset_type_name = #{assetTypeName,jdbcType=VARCHAR},
      </if>
      <if test="assetTypeDesc != null" >
        asset_type_desc = #{assetTypeDesc,jdbcType=VARCHAR},
      </if>
      <if test="natureFlag != null" >
        nature_flag = #{natureFlag,jdbcType=CHAR},
      </if>
      <if test="featureFlag != null" >
        feature_flag = #{featureFlag,jdbcType=CHAR},
      </if>
      <if test="codingFlag != null" >
        coding_flag = #{codingFlag,jdbcType=CHAR},
      </if>
      <if test="matchFlag != null" >
        match_flag = #{matchFlag,jdbcType=CHAR},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="stateFlag != null" >
        state_flag = #{stateFlag,jdbcType=CHAR},
      </if>
    </set>
    where asset_type_code = #{assetTypeCode,jdbcType=VARCHAR}
  </update> 

存儲過程:

   <select id="SiteAssetsSoftWareNumberDao.getNewsiteAssetsSoftwareCode" resultMap="BaseResultMap" parameterType="com.easyway.eamsg.assetmgt.domain.SiteAssetsSoftWareNumbeInfo" >
  DECLARE @siteAssetsSoftwareCode varchar(13)
  EXEC P_Site_Assets_Software_Number
  @stationCode = #{stationCode,jdbcType=VARCHAR},
  @assetTypeCode = #{assetTypeCode,jdbcType=VARCHAR},
  @siteAssetsSoftwareCode = @siteAssetsSoftwareCode OUTPUT
  SELECT @siteAssetsSoftwareCode as siteAssetsSoftwareCode
 </select>

存儲過程2:

  <resultMap id="BaseResultMap" type="com.easyway.eamsg.assetmgt.domain.SparePartsNumbeInfo" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="remarks" property="remarks" jdbcType="VARCHAR" />
    <result column="sparePartsCode" property="sparePartsCode" jdbcType="VARCHAR" />
  </resultMap>
   <select id="SparePartsNumberDao.getNewSparePartsCode" resultMap="BaseResultMap" >
  DECLARE @sparePartsCode varchar(17)
  EXEC P_Spare_Parts_Number
  @sparePartsCode = @sparePartsCode OUTPUT
  SELECT @sparePartsCode as sparePartsCode
 </select> 

mybayis項目使用的Mapping文件使用總結參考(二)