目录
1、<resultMap>
mybatis 的标签除了 select \insert \update\delete 还有:
还有很多其他的标签,<resultMap>、<parameterMap>、<sql>、<include>、<selectKey>,加上动态sql的9个标签,trim|where|set|foreach|if|choose|when|otherwise|bind等,其中<sql>为sql片段标签,通过<include>标签引入sql片段,<selectKey>为不支持自增的主键生成策略标签.
标签使用实例:
以 com.mytest.testMybatisMapper.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="com.mytest.testMybatisInterface">
1、<resultMap>
<resultMap type="java.util.HashMap" id="tenant-info">
<result property="siteName" column="siteName"/>
<result property="siteId" column="siteId"/>
<result property="activeDate" column="activeDate"/>
<result property="inactiveDate" column="inactiveDate"/>
<result property="subscriptionId" column="subscriptionId"/>
<result property="serviceStatus" column="serviceStatus"/>
<result property="custCompany" column="custCompany"/>
<result property="locationAddress" column="locationAddress"/>
</resultMap>
2、<selectKey> + if
<select id="getTenantPageByMap" parameterType="java.util.Map" resultMap="tenant-info">
SELECT siteName, siteId,activeDate,inactiveDate, subscriptionId,serviceStatus,custCompany, locationAddress
FROM (SELECT e.*, ROWNUM rn
from(select grp.group_name as siteName,
grp.group_id as siteId,
subs.active_date as activeDate,
subs.inactive_date as inactiveDate,
subs.subscription_id as subscriptionId,
subs.service_status as serviceStatus,
subs.cust_company as custCompany,
grp.detail_address as locationAddress
from ucs_subscription subs, ucs_group grp
where grp.group_id = subs.site_id
<if test="customCode!=null">
and subs.cust_company like '%'||#{customCode}||'%'
</if>
<if test="siteId!=null">
and grp.group_id like '%'||#{siteId}||'%'
</if>
<if test="siteName!=null">
and grp.group_name like '%'||#{siteName}||'%'
</if>
) e
WHERE ROWNUM < #{endNum}
<if test="sortField!=null">
order by #{sortField}
<if test="sortOrder!=null">
#{sortOrder}
</if>
</if>
)
WHERE rn > #{beginNum}
</select>
3、sql+select +if +where
<sql id="iomTmplJson">
u_id as u_id,
provincecode as provincecode,
command_id as commandId,
tmple_desc as tmpleDesc,
json_config as jsonConfig,
iom_msg_type as iomMsgType,
order_type as orderType
</sql>
<select id="getIomTmplJsonInfo" parameterType="java.util.Map" resultType="com.test.iom.bean.testCfgIomTmplJson">
SELECT <include refid="iomTmplJson"/> FROM test_Cfg_Iom_TmplJson
<where>
<if test="provincecode!=null">
provincecode = #{provincecode}
</if>
<if test="commandId!=null">
and command_id = #{commandId}
</if>
<if test="iomMsgType!=null">
and iom_msg_type = #{iomMsgType}
</if>
<if test="orderType!=null">
and order_type = #{orderType}
</if>
</where>
</select>
4、update +if
<update id="updateInvoiceStatus" parameterType="java.util.Map">
update acc_printinvoice_log t
set t.invoice_status = #{invoiceStatus},
<if test="invoiceStatus=='102'">
t.check_staff_id =#{staffId},
t.check_date = sysdate()
</if>
where t.serial_code =#{serialCode}
</update>
5、trim +insert
<insert id="insertGroupIdRela" parameterType="com.test.core.bean.CbssGroupIdRela">
insert into test_GROUPID_MAPPING
<trim prefix="(" suffix=")" suffixOverrides=",">
U_ID,
PROVINCECODE,
GENERAL_GROUPID,
PROVINCE_GROUPID,
GROUPTYPE,
CREATE_DATE,
STATUS
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
(SELECT decode(MAX(u_id),NULL,1,MAX(u_id)+1) FROM test_GROUPID_MAPPING),
#{provinceCode},
#{general_groupId},
#{province_groupId},
#{groupType},
sysdate,
'1'
</trim>
</insert>
6、insert +foreach + 序列
<insert id="insertBookInfoList" parameterType="map">
insert into test_book_info_${tablestr} (U_ID,SONBR,BookTime)
<foreach collection="list" item="item" index="index" open="(" close=")" separator="union all">
SELECT #{item.u_ID} as u_ID, #{item.soNbr} as soNbr,sysdate from DUAL
</foreach>
</insert>
6.2、参数多的foreach
<insert id="insertBillSettleFileLog" parameterType="map">
<selectKey resultType="long" keyProperty="settleId" order="BEFORE">
SELECT SEQ_BILLSETTLEID.NEXTVAL FROM DUAL
</selectKey>
insert into BILL_SETTLE_FILE_LOG_${currTime}
( SETTLE_ID,DISPUTE_FILE_ID,ACCOUNT_ID,CYCLE_MONTH,REQUEST_ID,FEE_TYPE,BILL_SUBJECT_ID,RECORD_NO,DEAL_STATUS,ERROR_CAUSE,CREATE_TIME,
OPERATOR_ID,PROVINCE_CODE,REGION_ID,REGION_CODE,BUS_ID,SETTLE_FEE )
select 'STF'||SEQ_BILLSETTLEID.NEXTVAL,t.* from (
<foreach collection="list" item="item" index="index" separator="union all">
SELECT #{item.disputeFileId} as disputeFileId, #{item.accountId,jdbcType=VARCHAR} as accountId,
#{item.cycleMonth,jdbcType=VARCHAR} as cycleMonth, #{item.requestId,jdbcType=VARCHAR} as requestId,
#{item.feeType,jdbcType=VARCHAR} as feeType, #{billSubjId}, #{item.recordNo} as recordNo, #{item.dealStatus} as dealStatus,
#{item.errorCause,jdbcType=VARCHAR} as errorCause,sysdate, #{item.operatorId,jdbcType=VARCHAR} as operatorId,
#{item.provinceCode,jdbcType=VARCHAR} as provinceCode, #{item.regionId,jdbcType=VARCHAR} as regionId,
#{item.regionCode,jdbcType=VARCHAR} as regionCode, #{item.busId,jdbcType=VARCHAR} as busId, #{item.settleFee} as settleFee from DUAL
</foreach> ) t
</insert>
7、update+foreach
<update id="updateBmsRestItemBatch" parameterType="java.util.List" >
<foreach collection="list" item="item" open="begin" close="end;" >
update bms_request_item
<set>
STATUS=1
</set>
where SEQ_ID=#{item.seqId} ;
</foreach>
</update>
<update id="updateBmsRestItemByResIds" parameterType="java.util.List" >
update bms_request_item set status = 2 where request_id in (
<foreach collection="list" item="item" index="index" separator=",">
#{item}
</foreach>
)
</update>
8、select+choose+when
<select id="queryNtaxFeeDetailFull" parameterType="java.util.Map" resultType="java.util.Map">
select a.request_id, a.component_id, a.mu_type, a.real_amount_ntax,a.compute_data,a.reference_amount_ntax,a.subscription_id
from ucs_request_fee a
where
<choose>
<when test="tradeType=='14'">
#{activeTime} between a.effect_time and a.expire_time
</when>
<otherwise>
sysdate between a.effect_time and a.expire_time
</otherwise>
</choose>
and a.request_id = #{requestId}
</select>
9、select+bind
<select id="selectFlowForUpdate" resultMap="BaseResultMap" parameterType="java.lang.String">
<bind name="ENTR_ID" value="'%' + _parameter" />
select * from TC_ENTR_FLOW where ENTR_ID like #{ENTR_ID} for update
</select>
</mapper>