上代碼;sql
DemoMapper接口:數組
/** * 經過商品id修改商品名 * @param map * @return */ Integer updateGoods_nameById(Map<String,String > map);
DemoMapper.xml:app
<update id="updateGoods_nameById"> UPDATE goods <trim prefix="set" suffix="WHERE goods_id = #{userId}"> <if test="userName!=null and userName !=''"> goods_name = #{userName} </if> </trim> </update>
效果:ide
------------------------------------------------------------------------測試
直接上代碼:spa
DemoMapper接口;3d
/**
* 經過數組傳參 foreach遍歷查詢信息
* @param its
* @return
*/
List<Goods> getGoodsListBy_in(Integer [] its);
DemoMapper.xml:code
</select> <select id="getGoodsListBy_in" resultType="com.pojo.Goods"> SELECT * FROM goods where goods_id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
測試類:xml
@Test public void test04(){ SqlSession sqlSession = MyBatisUtil.createSqlSession(); DemoMapper mapper = sqlSession.getMapper(DemoMapper.class); // Map<String,String> map = new HashMap<String, String>(); Integer [] its = {1,2,3}; List<Goods> list = mapper.getGoodsListBy_in(its); for (Goods goods : list) { System.out.println(goods.getGoods_code()+" "+goods.getGoods_name()); } MyBatisUtil.closeSqlSession(sqlSession); }
測試結果:blog
--------------------------------------------------------------------------------------
直接上代碼:
接口:
/** * 使用Map集合傳參,經過foreach遍歷查詢信息 * @return */ List<Goods> getGoodsListBy_map(Map<String,Object> map);
xml:
<select id="getGoodsListBy_map" resultType="com.pojo.Goods"> SELECT * FROM goods where goods_id in
這裏的collection是至關於Map的key 咱們要經過這個key找到對應的集合而後遍歷,因此這裏的collection = 測試的its---- key; <foreach collection="its" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
測試類:
@Test public void test05(){ SqlSession sqlSession = MyBatisUtil.createSqlSession(); DemoMapper mapper = sqlSession.getMapper(DemoMapper.class); Map<String,Object> map = new HashMap<String, Object>(); List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); map.put("its",list); List<Goods> goodsListBy_map = mapper.getGoodsListBy_map(map); for (Goods goods : goodsListBy_map) { System.out.println(goods.getGoods_code()+" "+goods.getGoods_name()); } MyBatisUtil.closeSqlSession(sqlSession); }
測試結果: