MyBatis动态SQL-foreach-数组/List
生活随笔
收集整理的這篇文章主要介紹了
MyBatis动态SQL-foreach-数组/List
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*** foreach標簽, foreach標簽實現(xiàn)in集合(或數(shù)組)動態(tài)查詢SysUser信息.* 根據(jù)用戶id集合查詢* @param idList* @param userNameList* @return* */
List<SysUser> selectSysUserByIdListAndUsernameList(@Param(value="idLists") List<Long> idList, @Param(value = "userNameLists") List<String> userNameList);/*** foreach標簽, foreach標簽實現(xiàn)in集合(或數(shù)組)動態(tài)查詢SysUser信息.* 根據(jù)用戶id集合查詢* @param idArray* @return */
List<SysUser> selectSysUserByIdArray(Long[] idArray);
<!-- foreach標簽,Array[]數(shù)組,實現(xiàn)IN集合,動態(tài)插入數(shù)據(jù). -->
<select id="selectSysUserByIdArray" resultType="com.learn.mybatis.pojo.SysUser">SELECT id,user_name userName,user_password userPassword,user_email userEmail,user_info userInfo,head_img headImg,create_time createTimeFROM sys_userWHERE id IN<foreach collection="array" open="(" close=")" separator="," item="id" index="i">#{id}</foreach>
</select><if test="array.length > 0"></if>
<!-- foreach標簽,多個List<T>參數(shù),實現(xiàn)IN集合,動態(tài)插入數(shù)據(jù). -->
<select id="selectSysUserByIdListAndUsernameList" resultType="com.learn.mybatis.pojo.SysUser">SELECT id,user_name userName,user_password userPassword,user_email userEmail,user_info userInfo,head_img headImg,create_time createTimeFROM sys_userWHERE id IN<foreach collection="idLists" open="(" close=")" separator="," item="id" index="i">#{id}</foreach>OR user_name IN<foreach collection="userNameLists" open="(" close=")" separator="," item="userName" index="i">#{userName}</foreach>
</select>
<list test="list.size>0"></list>
<!-- foreach標簽,List<T>參數(shù),批量插入數(shù)據(jù). -->
<!-- 說明:目前僅Mysql數(shù)據(jù)庫支持在批量插入數(shù)據(jù)過程中,可批量返回自增主鍵id值。配置 useGeneratedKeys、keyProperty這2個參數(shù),其目的是批量返回插入信息的自增主鍵. -->
<insert id="insertSysUserList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">INSERT INTO sys_user(user_name, user_password, user_email,user_info, head_img, create_time) VALUES<foreach collection="list" item="user" separator=",">(#{user.userName}, #{user.userPassword}, #{user.userEmail},#{user.userInfo}, #{user.headImg, jdbcType=BLOB},#{user.createTime, jdbcType=TIMESTAMP})</foreach>
</insert>
?
總結(jié)
以上是生活随笔為你收集整理的MyBatis动态SQL-foreach-数组/List的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis注解开发使用二级缓存
- 下一篇: MyBatis从缓存查找数据的依据