本篇博客主要講解如何使用foreach標籤生成動態的Sql,主要包含如下3個場景:html
假設有這樣1個需求:根據傳入的用戶id集合查詢出全部符合條件的用戶,此時咱們須要使用到Sql中的IN,如 id in (1,1001)。java
首先,咱們在接口SysUserMapper中添加以下方法:git
/** * 根據用戶id集合查詢用戶 * * @param idList * @return */ List<SysUser> selectByIdList(List<Long> idList);
而後在對應的SysUserMapper.xml中添加以下代碼:程序員
<select id="selectByIdList" resultType="com.zwwhnly.mybatisaction.model.SysUser"> SELECT id, user_name, user_password, user_email, create_time FROM sys_user WHERE id IN <foreach collection="list" open="(" close=")" separator="," item="id" index="i"> #{id} </foreach> </select>
最後,在SysUserMapperTest測試類中添加以下測試方法:github
@Test public void testSelectByIdList() { SqlSession sqlSession = getSqlSession(); try { SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); List<Long> idList = new ArrayList<Long>(); idList.add(1L); idList.add(1001L); List<SysUser> userList = sysUserMapper.selectByIdList(idList); Assert.assertEquals(2, userList.size()); } finally { sqlSession.close(); } }
運行測試代碼,測試經過,輸出日誌以下:sql
DEBUG [main] - ==> Preparing: SELECT id, user_name, user_password, user_email, create_time FROM sys_user WHERE id IN ( ? , ? )數據庫
DEBUG [main] - ==> Parameters: 1(Long), 1001(Long)數組
TRACE [main] - <== Columns: id, user_name, user_password, user_email, create_time微信
TRACE [main] - <== Row: 1, admin, 123456, admin@mybatis.tk, 2019-06-27 18:21:07.0mybatis
TRACE [main] - <== Row: 1001, test, 123456, test@mybatis.tk, 2019-06-27 18:21:07.0
DEBUG [main] - <== Total: 2
經過日誌會發現,foreach元素中的內容最終生成的Sql語句爲(1,1001)。
foreach包含屬性講解:
也許有人會好奇,爲何collection的值是list?該值該如何設置呢?
上面的例子中只有一個集合參數,咱們把collection屬性的值設置爲了list,其實也能夠寫成collection,爲何呢?讓咱們看下DefaultSqlSession中的默認處理邏輯:
private Object wrapCollection(Object object) { DefaultSqlSession.StrictMap map; if (object instanceof Collection) { map = new DefaultSqlSession.StrictMap(); map.put("collection", object); if (object instanceof List) { map.put("list", object); } return map; } else if (object != null && object.getClass().isArray()) { map = new DefaultSqlSession.StrictMap(); map.put("array", object); return map; } else { return object; } }
雖然使用默認值,代碼也能夠正常運行,但仍是推薦使用@Param來指定參數的名字,以下所示:
List<SysUser> selectByIdList(@Param("idList") List<Long> idList);
<foreach collection="idList" open="(" close=")" separator="," item="id" index="i"> #{id} </foreach>
若是參數是一個數組參數,collection能夠設置爲默認值array,看了上面的源碼,應該不難理解。
/** * 根據用戶id數組查詢用戶 * * @param idArray * @return */ List<SysUser> selectByIdArray(Long[] idArray);
<select id="selectByIdArray" resultType="com.zwwhnly.mybatisaction.model.SysUser"> SELECT id, user_name, user_password, user_email, create_time FROM sys_user WHERE id IN <foreach collection="array" open="(" close=")" separator="," item="id" index="i"> #{id} </foreach> </select>
雖然使用默認值,代碼也能夠正常運行,但仍是推薦使用@Param來指定參數的名字,以下所示:
List<SysUser> selectByIdArray(@Param("idArray")Long[] idArray);
<foreach collection="idArray" open="(" close=")" separator="," item="id" index="i"> #{id} </foreach>
假設有這樣1個需求:將傳入的用戶集合批量寫入數據庫。
首先,咱們在接口SysUserMapper中添加以下方法:
/** * 批量插入用戶信息 * * @param userList * @return */ int insertList(List<SysUser> userList);
而後在對應的SysUserMapper.xml中添加以下代碼:
<insert id="insertList" 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>
最後,在SysUserMapperTest測試類中添加以下測試方法:
@Test public void testInsertList() { SqlSession sqlSession = getSqlSession(); try { SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); List<SysUser> sysUserList = new ArrayList<SysUser>(); for (int i = 0; i < 2; i++) { SysUser sysUser = new SysUser(); sysUser.setUserName("test" + i); sysUser.setUserPassword("123456"); sysUser.setUserEmail("test@mybatis.tk"); sysUserList.add(sysUser); } int result = sysUserMapper.insertList(sysUserList); for (SysUser sysUser : sysUserList) { System.out.println(sysUser.getId()); } Assert.assertEquals(2, result); } finally { sqlSession.close(); } }
運行測試代碼,測試經過,輸出日誌以下:
DEBUG [main] - ==> Preparing: INSERT INTO sys_user(user_name, user_password, user_email, user_info, head_img, create_time) VALUES (?,?,?,?,?,?) , (?,?,?,?,?,?)
DEBUG [main] - ==> Parameters: test0(String), 123456(String), test@mybatis.tk(String), null, null, null, test1(String), 123456(String), test@mybatis.tk(String), null, null, null
DEBUG [main] - <== Updates: 2
1035
1036
假設有這樣1個需求:根據傳入的Map參數更新用戶信息。
首先,咱們在接口SysUserMapper中添加以下方法:
/** * 經過Map更新列 * * @param map * @return */ int updateByMap(Map<String, Object> map);
而後在對應的SysUserMapper.xml中添加以下代碼:
<update id="updateByMap"> UPDATE sys_user SET <foreach collection="_parameter" item="val" index="key" separator=","> ${key} = #{val} </foreach> WHERE id = #{id} </update>
最後,在SysUserMapperTest測試類中添加以下測試方法:
@Test public void testUpdateByMap() { SqlSession sqlSession = getSqlSession(); try { SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", 1L); map.put("user_email", "test@mybatis.tk"); map.put("user_password", "12345678"); Assert.assertEquals(1, sysUserMapper.updateByMap(map)); SysUser sysUser = sysUserMapper.selectById(1L); Assert.assertEquals("test@mybatis.tk", sysUser.getUserEmail()); Assert.assertEquals("12345678", sysUser.getUserPassword()); } finally { sqlSession.close(); } }
運行測試代碼,測試經過,輸出日誌以下:
DEBUG [main] - ==> Preparing: UPDATE sys_user SET user_email = ? , user_password = ? , id = ? WHERE id = ?
DEBUG [main] - ==> Parameters: test@mybatis.tk(String), 12345678(String), 1(Long), 1(Long)
DEBUG [main] - <== Updates: 1
DEBUG [main] - ==> Preparing: SELECT id, user_name, user_password, user_email, create_time FROM sys_user WHERE id = ?
DEBUG [main] - ==> Parameters: 1(Long)
TRACE [main] - <== Columns: id, user_name, user_password, user_email, create_time
TRACE [main] - <== Row: 1, admin, 12345678, test@mybatis.tk, 2019-06-27 18:21:07.0
DEBUG [main] - <== Total: 1
上面示例中,collection使用的是默認值_parameter,也能夠使用@Param指定參數名字,以下所示:
int updateByMap(@Param("userMap") Map<String, Object> map);
<update id="updateByMap"> UPDATE sys_user SET <foreach collection="userMap" item="val" index="key" separator=","> ${key} = #{val} </foreach> WHERE id = #{userMap.id} </update>
源碼地址:https://github.com/zwwhnly/mybatis-action.git,歡迎下載。
劉增輝《MyBatis從入門到精通》
原創不易,若是以爲文章能學到東西的話,歡迎點個贊、評個論、關個注,這是我堅持寫做的最大動力。
若是有興趣,歡迎添加個人微信:zwwhnly,等你來聊技術、職場、工做等話題(PS:我是一名奮鬥在上海的程序員)。
原文出處:https://www.cnblogs.com/zwwhnly/p/11163638.html