2023-09-12
StoreHouseMapper
/** * 批量添加 */ void insertMoreSH(@Param("storeHouses") List<StoreHouse> storeHouses); /** * 批量删除方式一 */ void deleteMoreSH(@Param("ids")Integer[] ids); /** * 批量删除方式二 */ void deleteMoreSHTwo(@Param("ids")Integer[] ids);
StoreHouseMapper.xml
<insert id="insertMoreSH"> insert into t_storehouse(id,s_name,s_type,s_description) values <foreach collection="storeHouses" item="storeHouse" separator=","> (null,#{storeHouse.sName},#{storeHouse.sType},#{storeHouse.sDescription}) </foreach> </insert> <delete id="deleteMoreSH"> delete from t_storehouse where id in ( <foreach collection="ids" item="id" separator=","> #{id} </foreach> ) </delete> <!-- 批量删除方式二--> <delete id="deleteMoreSHTwo"> delete from t_storehouse where <foreach collection="ids" item="id" separator="or"> id = #{id} </foreach> </delete>
测试
@Test public void testInsertSH(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); StoreHouseMapper mapper = sqlSession.getMapper(StoreHouseMapper.class); StoreHouse storeHouse1 = new StoreHouse(null,"山西仓库","自用仓库","存放醋"); StoreHouse storeHouse2 = new StoreHouse(null,"云南仓库","自用仓库","存放米线"); StoreHouse storeHouse3 = new StoreHouse(null,"陕西仓库","自用仓库","存放玉米"); List<StoreHouse> list = Arrays.asList(storeHouse1, storeHouse2, storeHouse3); mapper.insertMoreSH(list); sqlSession.close(); } @Test public void testDeleteSH(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); StoreHouseMapper mapper = sqlSession.getMapper(StoreHouseMapper.class); Integer[] ids = new Integer[]{5,6}; mapper.deleteMoreSH(ids); sqlSession.close(); } @Test public void testDeleteSHTwo(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); StoreHouseMapper mapper = sqlSession.getMapper(StoreHouseMapper.class); Integer[] ids = new Integer[]{8,9}; mapper.deleteMoreSHTwo(ids); sqlSession.close(); }
标签:StoreHouseMapper,批量,StoreHouse,void,ids,sqlSession,添加,mybatis,Integer From: https://www.cnblogs.com/isDaHua/p/17696263.html