-- pgsql的nullif函数用法,如果两个参数相等返回null,否则返回第一个参数
select nullif(1,1);
SELECT NULLIF (1, 1); -- return NULL
SELECT NULLIF (1, 0); -- return 1
SELECT NULLIF ('A', 'B'); -- return A
-- coalesce函数用法,参数可以有无限个,总是返回第一个非空参数,如果所有参数都是null,则返回null。
SELECT COALESCE(1, 2); -- return 1
SELECT COALESCE(NULL, 2 , 1); -- return 2
-- foreach用法
<insert id="insertNaRulePara" parameterType="ArrayList" >
insert into NA_RULE_PARA(
RULE_ID,
PARA_ID,
PARA_TYPE_ID
)
VALUES
<foreach collection="list" item="item" index="index" separator="," >
(
#{item.ruleId},
#{item.paraId},
1
)
</foreach>
</insert>
-- separator="," 这里以逗号作为分隔符,
-- 后台实际执行的sql如下,同时插入两条(如果list多条,values后面继续拼接上数据)
insert into NA_RULE_PARA( RULE_ID, PARA_ID, PARA_TYPE_ID ) VALUES ( 4, 2022, 1 ) , ( 5, 2023, 1 )
-- 批量更新
update user
set phone = case id
when 1 then '13128387651'
when 2 then '13128387652'
end,
name= case id
when 1 then '陈维'
when 2 then '黄日'
end
where id in(1,2)
<update id="updateBasInfoW" parameterType="ArrayList">
update bas_info
set user_mod_w = case id
<foreach collection="list" item="item" index="index" separator=" " >
when #{item.id} then #{item.num}
</foreach>
end
<where>
id in
<foreach collection="list" item="item" open="(" separator="," close=")" >
#{item.id}
</foreach>
</where>
</update>
-- foreach说明
-- 你可以传递一个 list 实例或者 array 数组作为参数对象传给 MyBatis。当你这么做的时候,
-- MyBatis 会自动将它包装在一个 Map 中,用名称作为键。list 实例将会以 list 作为键,
-- 而 array 数组实例将会以 array 作为键
-- foreach 元素的属性主要有 item,index,collection,open,separator,close
-- item:表示集合中每一个元素进行迭代时的别名
-- index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置
-- open:表示该语句以什么开始
-- separator:表示在每次进行迭代之间以什么符号作为分隔符
-- close:表示以什么结束
-- 如果传入的是单参数且参数类型是一个 List 时,collection 属性值为 list
-- 如果传入的是单参数且参数类型是一个 array 数组时,collection 的属性值为 array
-- 如果传入的参数是多个时,可以把它们封装成一个 Map,collection 的属性值为 Map 的键
-- 如果传入的参数是多个时,也可以把它们封装成一个 Object
-- 传递单个list
public List<User> selectByList(List<User> list);
<select id="selectByList" parameterType="java.util.List" resultType="user">
select * from user
<where>
<!-- 传递List,List中是pojo -->
<if test="list!=null">
<foreach collection="list" item="item" open="and id in(" separator="," close=")">
#{item.id}
</foreach>
</if>
</where>
</select>
-- 传递单个数组
public int deleteByIds(String[] ids);
<delete id="deleteByIds" parameterType="java.lang.String">
DELETE FROM cms_article
WHERE article_id in
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
-- 封装成Map
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
Map<String, Object> map = new HashMap<>();
map.put("userids", list);
List<UserInfo> selectUserInfoByForEach(Map<String,Object> map);
<select id="selectUserInfoByForEach" parameterType="java.util.Map" resultMap="UserInfoResult">
select * from userinfo
<if test="userids!=null">
where userid in
<foreach item="ParamsId" collection="userids" index="index" open="(" separator="," close=")">
#{ParamsId}
</foreach>
</if>
</select>
collection="userids" 这个 userids 是 Map 的键
pgsql的insertOrUpdate用法::ON conflict ( ) DO 子句实现
-- 使用ON conflict ( ) DO子句,下面的意思即为当 'class_id'重复时,执行UPDATE 操作
-- ON conflict ( 'column1','column2' ) DO 中的字段要满足唯一性约束,否则sql执行时会报错
-- conflict里的字段必须为主键或者唯一索引,可以多个字段作为唯一索引,在数据库设置唯一
insert into class(class_id, stu_id, class_name)
VALUES(1,88,'郑州')
on conflict (class_id) do update
set stu_id = 001,class_name='广东';
<insert id="insertOrUpdateBath" parameterType="list">
<if test="list != null and list.size() > 0">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
insert into student(stu_id, stu_age, stu_time) VALUES
(#{item.stu_id,jdbc=varchar},#{item.stu_age,jdbc=varchar},#{item.stu_time,jdbc=varchar})
on conflict(stu_id) do update set
<if test="item.age != null">
stu_age = #{item.stu_age,jdbc=varchar},
</if>
<if test="item.time != null">
stu_time = #{item.stu_time,jdbc=varchar}
</if>
</foreach>
</if>
</insert>