_Mybatis根据id删除数据
-
在mapper接口中定义方法:
-
在对应的xml映射文件中编写sql语句:
-
在test文件中编写java代码实现sql语句对数据库的数据删除功能:
-
// 修改数据库中的数据 public void update() { int status = 0; String brand_name = "智能老人机"; String address = "北京"; int ordered = 100; int id = 5; // 封装参数 Brand brand = new Brand(); brand.setId(id); brand.setStates(status); brand.setName(brand_name); brand.setAddress(address); brand.setOrdered(ordered); InputStream is = null; try { is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is); SqlSession sqlSession = sf.openSession(true); // 获取mapper接口 BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); int result = mapper.update(brand); // 控制台返回执行结果 System.out.println("结果为:"+result); // 释放资源 sqlSession.close(); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } }
-
-
_根据id批量删除数据
-
在mapper接口文件中创建批量删除的方法接口:
-
在xml文件中编写sql实现批量删除:
-
在test文件中编写java程序实现数据库数据的批量删除:
-
// 批量删除数据库中的数据 public void deleteDates() { int[] id = {4,6}; InputStream is = null; try { is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is); SqlSession sqlSession = sf.openSession(true); // 获取mapper接口 BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); int result = mapper.deleteByIds(id); // 控制台返回执行结果 System.out.println("受影响的行数为:"+result); // 释放资源 sqlSession.close(); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } =============================================== 受影响的行数为:2 Process finished with exit code 0
-
-
_注解完成增删改查
-
注意:注解开发只适用于简单的增删改查语句,复杂的查询语句还是需要使用配置文件的方式完成(比如动态查序就需要配置文件的方式实现)
-
格式:(例如查询语句)
-
@Select("select * from tb_brand where id = #{id}") public Brand selectById(int id);
-
-
直接在mapper接口文件中在查询的方法上方加上@注释,后接sql语句并用’’ ‘’ 包起来
-
在test文件中编写java程序实现sql语句查询:
-
// 注解查询tb_brand表中的所有数据 public void selectAllPar() { InputStream is = null; try { is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is); SqlSession sqlSession = sf.openSession(true); // 获取mapper接口 BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); List<Brand> result = mapper.selectAll(); // 控制台返回执行结果 System.out.println("查询结果为:"+result); // 释放资源 sqlSession.close(); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } ============================================== 查询结果为:[Brand{id=1, brand_name='华为', ordered=50, address='广州', states=0}, Brand{id=2, brand_name='小米', ordered=30, address='北京', states=1}, Brand{id=3, brand_name='字节跳动', ordered=20, address='北京', states=0}] Process finished with exit code 0
-