批量删除
mybatis会将数组自动封装为Map集合,所以在接口函数处使用@Param注解改变map集合的默认key名称
不使用的话可以用默认名array
接口函数:
使用了@Param改变了默认的array的名称
1 | int deleteByIds( @Param ( "ids" ) int [] ids); |
mysql映射函数 collection名为ids,如果没有使用@Param则为默认的array
<delete id="deleteByIds"> delete from tb_brand where id in <foreach collection="ids" item="id" separator="," open="(" close=")"> #{id} </foreach> ; </delete>
执行文件
@Test public void DeleteByIds() throws IOException { //接收变量 int[] ids = {6,7}; //String brandname = ""; //处理数据 //companyname = "%" + companyname + "%"; //brandname = "%" + brandname + "%"; //封装类 /* Map map = new HashMap(); map.put("status",status); map.put("companyname",companyname); map.put("brandname",brandname);*/ //1、获取sqlSessionFactory String rescource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(rescource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2、获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //3、获取Mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); //4、执行方法 int count = brandMapper.deleteByIds(ids); sqlSession.commit(); // //List<Brand> brands = brandMapper.selectBycondition(map); System.out.println(count); //5、释放资源 sqlSession.close(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?