# 超过1000条条件的foreach循环查询
SELECT * FROM table
WHERE (1,id) IN
<foreachcollection="idList"index="index"open="("close=")"item="item"separator=",">
(1,#{item})
</foreach>
根据id 批量更新多个字段数据
<updateid="updateList"parameterType="java.util.List">
update allocation_details
<trimprefix="set"suffixOverrides=","><trimprefix="collect_num =case"suffix="end,"><foreachcollection="list"item="i"index="index"><iftest="i.collectNum != null ">
when id = #{i.id} then #{i.collectNum}
</if></foreach></trim></trim>
where
<foreachcollection="list"separator="or"item="i"index="index">
id = #{i.id}
</foreach></update>
# 获取模板信息联动数据集合
mapper方法: MsgTemplateDTO findTemplateById(@Param("id") Long id);
对应的xml sql:
<selectid="findTemplateById"resultMap="dtoMap"parameterType="long">
select * from msg_template where id = #{id}
</select><resultMapid="dtoMap"type="com.wehgc.remote.message.dto.req.MsgTemplateDTO"><idcolumn="id"property="id"/><resultcolumn="code"property="code"/><resultcolumn="title"property="title"/><resultcolumn="sub_title"property="subTitle"/><resultcolumn="send_name"property="sendName"/><collectionproperty="msgAttributesDTOList"ofType="com.wehgc.remote.message.dto.req.MsgAttributesDTO"javaType="java.util.List"column="id"select="com.wehgc.message.msg.mapper.MsgAttributesMapper.listByTempId"/></resultMap>
联动集合:
mapper方法: List<MsgAttributesDTO> listByTempId(@Param("id") Long tempId);
对应的xml sql:
<selectid="listByTempId"resultMap="dataMap"parameterType="long">
select * from msg_attributes where temp_id = #{id} and is_del = 0
</select><resultMapid="dataMap"type="com.wehgc.remote.message.dto.req.MsgAttributesDTO"><idcolumn="id"property="id"/><resultcolumn="code"property="code"/><resultcolumn="name"property="name"/><resultcolumn="value"property="value"/><resultcolumn="sort"property="sort"/><resultcolumn="temp_id"property="tempId"/><collectionproperty="msgAttributesStyleDTOList"ofType="com.wehgc.remote.message.dto.req.MsgAttributesStyleDTO"javaType="java.util.List"column="id"select="com.wehgc.message.msg.mapper.MsgAttributesStyleMapper.listByAttrId"/></resultMap>
联动集合:
mapper方法: List<MsgAttributesStyleDTO> listByAttrId (@Param("id") Long attrId);
对应的xml sql:
<selectid="listByAttrId"resultType="com.wehgc.remote.message.dto.req.MsgAttributesStyleDTO">
select * from msg_attributes_style where attr_id = #{id} and is_del = 0
</select>
共同sql:
# 去重selectdistinct name from test_user;# 查询数量selectcount(1)fromtable;# 排序null值排在最后select*fromtableorderby id desc nulls last;# 查询某个字段存在多条数据并展示数据select t.name,count(1)from sys_user t groupby t.name havingcount(1)>1;
oracle中在plsql编辑器
# 直接修改查询后的表select t.*,t.rowid fromtable t;select*from table_name forupdate;# 查询只返回一条数据select*fromtablewhere rownum =1;# 查询时间排序最新一条数据: select k.bookdate as bookDate from(select a.bookdate, row_number()over(partitionby a.id by a.datedesc) rn fromtable a where a.id=‘1000’)k where k.rn =1;