標籤:manage 大量刪除 大量操作 collect 刪除 解決 增刪改查 sts 順序
此文主要是講mybatis在串連oracle資料庫時的一些大量操作,請各位對號入座
(最後回來補充一下,所有都是在Spring+MVC的架構下實現的)
不廢話,上代碼:
1、批量插入(網上很多,是針對MySQL的,被坑慘!oracle mybatis裡面只能用以下這種配置)
DAO:int autoFullPass(@Param("listAutoData")List<SatisfactionSurvey> listAutoData, @Param("evaluate")String evaluate);
XML:
<insert id="autoFullPass" parameterType="java.util.List" useGeneratedKeys="false">
INSERT ALL
<foreach collection="listAutoData" item="item" index="index">
into SATISFACTION_SURVEY
(PROJECT_NO, EVALUATE, EMPLOYEE_NO, SUBMIT_TIME, as_side)
values
(#{item.projectNo}, #{evaluate}, #{item.employeeNo}, sysdate, 0)
</foreach>
SELECT 1 FROM DUAL
</insert>
補充說明:1、foreach外包了一層sql,解決核心;2、注意foreach裡面的屬性配置,沒有一般的begin、end、分隔字元等;
3、useGeneratedKeys="false" 筆者也不清楚是不是必須加這句,大家可以不加試下
2、大量刪除
DAO:int deleteByListSon(@Param("listSon")List<String> listSon);
XML:
<delete id="deleteByListSon" parameterType="java.util.List">
delete from WECHAT_MENU where id in
<foreach collection="listSon" item="listSon" index="index" separator="," open="(" close=")">
#{listSon}
</foreach>
</delete>
補充說明:這個沒啥好說的,基本sql格式,比較簡單
3、批次更新
(1)DAO:int changeOnlineByListSon(@Param("listSon")List<String> listSon, @Param("deleted")int deleted);
XML:
<update id="changeOnlineByListSon">
update WECHAT_MENU
<set>
<if test="deleted == 0" >
DELETED = 2,
</if>
<if test="deleted == 2" >
DELETED = 0,
</if>
PUBLISH = 0,
UPDATE_DATE = sysdate,
</set>
where ID in
<foreach collection="listSon" item="item" index="index" separator="," open="(" close=")">
#{item}
</foreach>
</update>
補充說明:這種還算簡單的,是list<string>類型,foreach比較容易,筆者這個sql功能在於切換某菜單的上、下線狀態,所以有deleted(沒用status有原因、勿噴)這個值(0:上線;2下線)
(2)DAO:public int saveRegionKpi(@Param("updateToDB")List<HwRegionWeekly> updateToDB, @Param("employeeNo")String employeeNo);
XML:
<update id="saveRegionKpi" parameterType="java.util.List" >
<foreach collection="updateToDB" item="record" index="index" open="begin" close=";end;" separator=";">
update HW_REGION_WEEKLY
<set >
<if test="record.updateUser != null" >
UPDATE_USER = #{employeeNo},
</if>
<if test="record.updateTime != null" >
UPDATE_TIME = sysdate,
</if>
<if test="record.annualHosKpi != null" >
ANNUAL_HOS_KPI = #{record.annualHosKpi,jdbcType=DECIMAL},
</if>
<if test="record.ytdPlanHos != null" >
YTD_PLAN_HOS = #{record.ytdPlanHos,jdbcType=DECIMAL},
</if>
<if test="record.status == 1" >
STATUS = 1,
SUBMIT_USER = #{employeeNo},
SUBMIT_TIME = sysdate,
</if>
<if test="record.manageMeeting != null" >
MANAGE_MEETING = #{record.manageMeeting,jdbcType=DECIMAL},
</if>
<if test="record.remark != null" >
REMARK = #{record.remark}
</if>
</set>
where ID = #{record.id}
</foreach>
</update>
補充說明:首先,注意比較和(1)的區別,(1)裡面所有更新的行中,每個欄位更改的值都是一樣的;
其次,(2)更新的多行裡面。同樣的欄位也可能更新不同的值,其實(2)就是多條並存執行的更新語句,只不過用了mybatis的特性,
最後,主要還是要注意,(2)這種情況,foreach標籤的正確寫法
4、批量查詢
DAO:List<SatisfactionSurvey> listJointData(@Param("listProNo")List<Project> listProNo);
XML:
<select id="listJointData" resultMap="BaseMap">
select r.project_no as PROJECT_NO, r.party_a_manager_no as employee_no, 0 as as_side
from requirement r
where r.project_no in
<foreach collection="listProNo" index="index" item="val" separator="," open="(" close=")">
#{val.projectNo}
</foreach>
</select>
補充說明:這種沒啥好說的,主要是foreach裡面的配置,也是拼接出資料庫能識別的sql語句
5、總結比較
(1)、大量操作,重點在foreach的配置,不熟悉的夥伴可以先多去瞭解一下mybatis foreach標籤的屬性和用法
(2)、上面的例子還摻雜了其他很多,可以學習和借鑒的點,仔細看,多發現(以上例子都是,從已上線的項目中抽出來的,絕對是通過測試的)
(3)、以上的例子傳入mybatis的參數,基本都是list集合(項目上用到的也幾乎全是list),實際情況也可能是map,這裡特別附上一例(map的批量查詢,加UNION ALL)
附:批量查詢--map參數
DAO:
List<ShowEvaluateList> getListShowInfo(@Param("allProNo")Map<String, List<String>> allProNo, @Param("limit")int limit, @Param("page")int page,
@Param("beginDate")String beginDate, @Param("endDate")String endDate,
@Param("keyProName")String keyProName, @Param("keyPartyA")String keyPartyA, @Param("keyPartyB")String keyPartyB);(後面的參數都是條件過濾和分頁用的<貌似分頁功能沒實現>)
Map<String, List<String>> allNoEva = new HashMap<>();
allNoEva.put("A", allNoEvaA);
allNoEva.put("B", allNoEvaB);
說明:allNoEvaA、allNoEvaB都是List<String>類型的集合(也就是做為甲方和乙方時,未評價的項目的projectNo的集合)
XML:
<select id="getListShowInfo" resultMap="ListShowMap">
<foreach collection="allProNo.keys" index="k" item="ent" separator="UNION ALL">
select row_number() over(order by 1) as rowIndex,
r.project_name as PRO_NAME, r.project_no as PRO_NO,
<choose>
<when test=‘ent == "A"‘>
r.party_a_manager as PER, r.party_a_manager_no as PER_ID,
p.actual_online as ACTUAL_END, 0 as WHICH_PARTY
</when>
<otherwise>
r.party_b_manager as PER, r.party_b_manager_no as PER_ID,
p.actual_online as ACTUAL_END, 1 as WHICH_PARTY
</otherwise>
</choose>
FROM requirement r
left join project p on p.project_no = r.project_no
where (p.status = 3 or p.status = 4)
<include refid="filterSql"></include>
<choose>
<when test="allProNo[ent].size() != 0">
and r.project_no in
<foreach collection="allProNo[ent]" index="v" item="val" separator="," open="(" close=")">
#{val}
</foreach>
</when>
<otherwise>
and r.project_no = ‘sssssssssssss‘
</otherwise>
</choose>
</foreach>
</select>
補充說明:(1)主要是參考map參數,在mybatis裡面怎麼迴圈取健和取值
(2)map的話要結合union all使用(關於和union的區別,貌似是union all不去重,union會去重且有些情況會排序取出的資料順序)
最後,特別加上條件過濾用的sql,以供參考,如下:
<sql id="filterSql">
and 1 = 1
<if test="beginDate != null and beginDate != ‘‘">
and p.ACTUAL_ONLINE > to_date(#{beginDate}, ‘YYYY/MM/DD‘)
</if>
<if test="endDate != null and endDate != ‘‘">
and to_date(#{endDate}, ‘YYYY/MM/DD‘) > p.ACTUAL_ONLINE
</if>
<if test="keyProName != null and keyProName != ‘‘">
and p.name like ‘%${keyProName}%‘
</if>
<if test="keyPartyA != null and keyPartyA != ‘‘">
and r.party_a_manager like ‘%${keyPartyA}%‘
</if>
<if test="keyPartyB != null and keyPartyB != ‘‘">
and r.party_b_manager like ‘%${keyPartyB}%‘
</if>
</sql>
mybatis+oracle:大量操作(增刪改查)