1.連接倆個字段值,且可以添加空格
@a. selet 'A值' || ‘’ || ‘B值’ from dual 結果是 A值 B值 (注意中間會有空格)
@b. select concat('A值','B值') from dual 結果是 A值B值
注意:||可以拼接多個字段和空格, concat只能是倆個參數,拼接倆個字段,想拼接多個字段時需要嵌套使用。
2.mybatis標籤替換where1=1
<select id="findActiveBlogLike" resultType="Blog">
select * from dual
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
andtitle like #{title}
</if>
</where>
</select>
3 case when來判斷用不同的字段查詢
select * from dual dd where 1=1 and (case when dd.type='01' then dd.name when dd.type='02' then dd.srcname else dd.fullname )
like '%不確定名稱簡稱還是全稱%'
4.foreach標籤解釋
參考文檔 :https://www.cnblogs.com/a8457013/p/7825154.html
collection="":指明要遍歷的集合;
item="":將當前遍歷出的元素賦值給指定變量;
separator="":每個元素之間的分隔符;
open="(":遍歷出的所有結果以左括號爲開始字符;
close=")";遍歷出的所有結果以右括號爲結束字符;
index="";遍歷List的時候索引,item是值;遍歷map的時候是key,item是值;
5.遍歷刪除某個集合的數據
List dataList = new ArrayList();
delete from table t where t in
<foreach collection="dataList" item = "item" index = "index" open = "(" separator="," close=")">
#{item}
</foreach>
6.遍歷刪除某個,分割的字符串
參考文檔:https://blog.csdn.net/siyi1219/article/details/80900415
String dataList = "1,2,3,4";
delete from table t where t in
<foreach collection="dataList.split(',')" item = "item" index = "index" separator="," >
#{item}
</foreach>
7.遍歷修改(對分割符;有待驗證)
參考文檔:https://blog.csdn.net/weixin_33675507/article/details/91740050
begin
<foreach collection="list" item="item" index="index" separator=";" >
update table
<set>
age = #{item.age}
</set>
where id = #{item.id};
</foreach>
end;
8遍歷新增
a*:方式一
<insert id="batchSave">
into table (name,age) values
<foreach collection="list" item="item" separator=",">
(#{item.name}, #{item.age})
</foreach>
</insert>
注意: list 爲空 最好在代碼中判斷後 在執行
b* 方式二 實踐後,效果更好
<insert id="batchInsertPMePhoneData" parameterType="java.util.List">
begin
<foreach collection="list" item="item" index="index">
insert into table (name, age) values ( #{item.name,jdbcType=VARCHAR}, #{item.age,jdbcType=VARCHAR});
</foreach>
end;
</insert>
注意: list 爲空 最好在代碼中判斷後 在執行
c* 方式三
insert into table1 (name, age)
select name.age from table2 where id>10
注意: 這裏不需要 values
9.mybatis中begin end 執行多條sql
參考文檔:https://blog.csdn.net/qq_28869233/article/details/90053546
https://blog.csdn.net/xpy_java/article/details/73177173
begin
delete from table1 where id = '001';
delete frm tablle2 where name = '張三';
end;
*:每條sql後都有;
10.解決oracle排序時按照漢字拼音排序不正確的問題
參考文檔 : https://blog.csdn.net/nvhaixx/article/details/7994408
select name from table order by NLSSORT(name,'SCHINESE_PINYIN_M'); // *** 按照拼音排序
select name from table order by NLSSORT(name,' SCHINESE_STROKE_M'); //按照筆畫排序
select name from table order by NLSSORT(name,' SCHINESE_RADICAL_M'); //按照部首排序
11.Oracle把逗號分隔的字符串轉爲in可以引用的值
前臺傳來的字符串:'589,321'
SELECT * FROM TABLE T1
WHERE T1.O_CODE IN (
SELECT REGEXP_SUBSTR('589,321','[^,]+', 1, LEVEL) FROM DUAL
CONNECT BY REGEXP_SUBSTR('SMITH,ALLEN,WARD,JONES', '[^,]+', 1, LEVEL) IS NOT NULL
)