MyBatis implements delete operation

MyBatis implements delete operation

1 delete a

interface

/**
 * 根据id删除
 */
void deleteById(int id);

SQL mapping

<!--根据id删除-->
<delete id="deleteById">
    delete
    from tb_brand
    where id = #{id};
</delete>

test

before deletion

image-20220815200817474

  @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(true);
//        3. 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        4. 执行方法
        brandMapper.deleteById(id);
//        5. 释放资源
        sqlSession.close();
    }

after deletion

image-20220815200939965

2 Batch delete

The initial sql configuration is

<delete id="deleteByIds">
    delete
    from tb_brand
    where id in (?,?,?);
</delete>

The problem with this is that it cannot be deleted dynamically

The question mark is a placeholder, but I don’t know how many to delete, so I don’t know how many placeholders to put

Solution:

use tags

Notice:

MyBatis will encapsulate the array parameters as a Map collection.

  • Default: array = array
  • Use the @Param annotation to change the name of the default key of the map collection

The way to use @Param annotation this time

interface

/**
 * 批量删除
 */
void deleteByIds(@Param("ids") int[] ids);

sql mapping

<!--    批量删除-->
<delete id="deleteByIds">
    delete
    from tb_brand
    where id
    in (
    <foreach collection="ids" item="id" separator=",">
        #{id}
    </foreach>
    );
</delete>

test

delete 7 8 9

image-20220815202428287

 @Test
    public void testDeleteByIds() throws IOException {
    
    
//        设置参数
        int[] ids = {
    
    7, 8, 9};

//    1.获取SQLSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        2. 获取SqlSession对象
//        设置为自动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        3. 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        4. 执行方法
        brandMapper.deleteByIds(ids);
//        5. 释放资源
        sqlSession.close();
    }

image-20220815202639936

optimization

Optimize sql mapping configuration file

Use the attributes open and close to omit manually adding parentheses outside, which are automatically added by MyBatis

<delete id="deleteByIds">
    delete
    from tb_brand
    where id
    in
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
    ;
</delete>

Guess you like

Origin blog.csdn.net/qq_45842943/article/details/126356894