1.刪除一個
點選檢視程式碼
void deleteById(int id);
<delete id="deleteById">
delete from tb_brand where id = #{id};
</delete>
@Test
public void testDeleteById() throws IOException {
//接收引數
int id = 6;
//1. 獲取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 獲取SqlSession物件
SqlSession sqlSession = sqlSessionFactory.openSession();
//SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3. 獲取Mapper介面的代理物件
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 執行方法
brandMapper.deleteById(id);
//提交事務
sqlSession.commit();
//5. 釋放資源
sqlSession.close();
}
點選檢視程式碼
void deleteByIds(int[] ids);
<!--
mybatis會將陣列引數,封裝為一個Map集合。
* 預設:array = 陣列
* 使用@Param註解改變map集合的預設key的名稱
-->
<delete id="deleteByIds">
delete from tb_brand where id
in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>
@Test
public void testDeleteByIds() throws IOException {
//接收引數
int[] ids = {5,7,8};
//1. 獲取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 獲取SqlSession物件
SqlSession sqlSession = sqlSessionFactory.openSession();
//SqlSession sqlSession = sqlSessionFactory.openSession(true);
//3. 獲取Mapper介面的代理物件
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 執行方法
brandMapper.deleteByIds(ids);
//提交事務
sqlSession.commit();
//5. 釋放資源
sqlSession.close();
}