Mybatis通过forEach批量删除数据的两种实现方法-----Mybatis框架

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.powernode.mybatis.mappers.CarMapper">
<!--    if标签的属性是必须的,if标签中test标签必须为true,if标签内的sql语句就会自动拼接,反过来则不会-->
<!--    test标签的值应该是一个表达式,结果为true或者false即可-->
<!--    如果使用了param注解,那么test标签写param指定的参数名-->
<!--    当没有使用使用这个param注解,则test中出现的是param1,param2....-->
<!--    当使用的是一个POJO对象,则填入的则为POJO的属性名字-->
<!--    mybatis的动态SQL中不能使用&&只能是and-->
    <select id="selectByMultiCondition" resultType="Car">
        select * from t_car where 1 = 1
        <if test="brand != null and brand != ''">
            and brand like '%${brand}%'
        </if>
        <if test="guidePrice != null and guidePrice != ''">
            and guide_price > #{guidePrice}
        </if>
        <if test="carType != null and carType != ''">
            and car_type = #{carType}
        </if>
    </select>
<!--    where标签是负责实现where子句的,如果我们没有where的条件,就不会再生成了-->
<!--    可以自动的去掉前面的and或者or,然后自动拼接出sql语句-->
<!--    在子句的后面的and和or不能去掉-->
    <select id="selectByMultiConditionWithWhere" resultType="Car">
        select * from t_car
        <where>
            <if test="brand != null and brand != ''">
                brand like '%${brand}%'
            </if>
            <if test="guidePrice != null and guidePrice != ''">
                and guide_price > #{guidePrice}
            </if>
            <if test="carType != null and carType != ''">
                and car_type = #{carType}
            </if>
        </where>
    </select>
<!--    prefixOverrides去掉前缀 suffix=加后缀 suffixOverrides去掉后缀 prefix加前缀-->
<!--    suffixOverrides去掉后缀:就是把SQL子句中多余的and或or去掉,这里用|就代表或者-->
<!--    没有任何sql子句的话,就不会添加前缀了(sql子句前缀)-->
    <select id="selectByMultiConditionWithTrim" resultType="Car">
        select * from t_car
        <trim suffixOverrides="and|or" prefix="where">
            <if test="brand != null and brand != ''">
                brand like '%${brand}%' and
            </if>
            <if test="guidePrice != null and guidePrice != ''">
                guide_price > #{guidePrice} and
            </if>
            <if test="carType != null and carType != ''">
                car_type = #{carType} and
            </if>
        </trim>
    </select>
<!--    局部更新,如果传递的值不为空,我们才实现更新数据-->
    <update id="updateById">
        update t_car set
            car_num = #{carNum},
            brand = #{brand},
            guide_price = #{guidePrice},
            produce_time = #{produceTime},
            car_type = #{carType}
        where
            id = #{id};
    </update>
    <update id="updateBySet">
        update t_car
        <set>
            <if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
            <if test="brand != null and brand != ''">brand = #{brand},</if>
            <if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if>
            <if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if>
            <if test="carType != null and carType != ''">car_type = #{carType}</if>
        </set>
        where
            id = #{id}
    </update>
<!--    先遇到就先进去,全部为空就走最后一个,和if-else语句类似,传进去一个null值-->
    <select id="selectWith" resultType="Car">
        select * from t_car
        <where>
            <choose>
                <when test="brand != null and brand != ''">
                    brand like '%${brand}%'
                </when>
                <when test="guidePrice != null and guidePrice != ''">
                    guide_price = #{guidePrice}
                </when>
                <otherwise>
                    car_type = #{carType}
                </otherwise>
            </choose>
        </where>
    </select>
<!--    foreach标签的属性collection=用于指定数组或者集合 item=代表数组或集合的元素 separator=循环之间的分隔符-->
    <delete id="deleteByIds">
        delete from t_car where id in
        <foreach collection="array" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>
<!--    批量插入信息,一次插入多条语句-->
    <insert id="insertBatch">
        insert into t_car values
        <foreach collection="cars" item="car" separator=",">
            (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
        </foreach>
    </insert>
    <delete id="deleteBatchById">
        delete from t_car where
        <foreach collection="ids" separator="or" item="id">
            id = #{id}
        </foreach>
    </delete>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.powernode.mybatis.mappers.CarMapper">
<!--    if标签的属性是必须的,if标签中test标签必须为true,if标签内的sql语句就会自动拼接,反过来则不会-->
<!--    test标签的值应该是一个表达式,结果为true或者false即可-->
<!--    如果使用了param注解,那么test标签写param指定的参数名-->
<!--    当没有使用使用这个param注解,则test中出现的是param1,param2....-->
<!--    当使用的是一个POJO对象,则填入的则为POJO的属性名字-->
<!--    mybatis的动态SQL中不能使用&&只能是and-->
    <select id="selectByMultiCondition" resultType="Car">
        select * from t_car where 1 = 1
        <if test="brand != null and brand != ''">
            and brand like '%${brand}%'
        </if>
        <if test="guidePrice != null and guidePrice != ''">
            and guide_price > #{guidePrice}
        </if>
        <if test="carType != null and carType != ''">
            and car_type = #{carType}
        </if>
    </select>
<!--    where标签是负责实现where子句的,如果我们没有where的条件,就不会再生成了-->
<!--    可以自动的去掉前面的and或者or,然后自动拼接出sql语句-->
<!--    在子句的后面的and和or不能去掉-->
    <select id="selectByMultiConditionWithWhere" resultType="Car">
        select * from t_car
        <where>
            <if test="brand != null and brand != ''">
                brand like '%${brand}%'
            </if>
            <if test="guidePrice != null and guidePrice != ''">
                and guide_price > #{guidePrice}
            </if>
            <if test="carType != null and carType != ''">
                and car_type = #{carType}
            </if>
        </where>
    </select>
<!--    prefixOverrides去掉前缀 suffix=加后缀 suffixOverrides去掉后缀 prefix加前缀-->
<!--    suffixOverrides去掉后缀:就是把SQL子句中多余的and或or去掉,这里用|就代表或者-->
<!--    没有任何sql子句的话,就不会添加前缀了(sql子句前缀)-->
    <select id="selectByMultiConditionWithTrim" resultType="Car">
        select * from t_car
        <trim suffixOverrides="and|or" prefix="where">
            <if test="brand != null and brand != ''">
                brand like '%${brand}%' and
            </if>
            <if test="guidePrice != null and guidePrice != ''">
                guide_price > #{guidePrice} and
            </if>
            <if test="carType != null and carType != ''">
                car_type = #{carType} and
            </if>
        </trim>
    </select>
<!--    局部更新,如果传递的值不为空,我们才实现更新数据-->
    <update id="updateById">
        update t_car set
            car_num = #{carNum},
            brand = #{brand},
            guide_price = #{guidePrice},
            produce_time = #{produceTime},
            car_type = #{carType}
        where
            id = #{id};
    </update>
    <update id="updateBySet">
        update t_car
        <set>
            <if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
            <if test="brand != null and brand != ''">brand = #{brand},</if>
            <if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if>
            <if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if>
            <if test="carType != null and carType != ''">car_type = #{carType}</if>
        </set>
        where
            id = #{id}
    </update>
<!--    先遇到就先进去,全部为空就走最后一个,和if-else语句类似,传进去一个null值-->
    <select id="selectWith" resultType="Car">
        select * from t_car
        <where>
            <choose>
                <when test="brand != null and brand != ''">
                    brand like '%${brand}%'
                </when>
                <when test="guidePrice != null and guidePrice != ''">
                    guide_price = #{guidePrice}
                </when>
                <otherwise>
                    car_type = #{carType}
                </otherwise>
            </choose>
        </where>
    </select>
<!--    foreach标签的属性collection=用于指定数组或者集合 item=代表数组或集合的元素 separator=循环之间的分隔符-->
    <delete id="deleteByIds">
        delete from t_car where id in
        <foreach collection="array" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>
<!--    批量插入信息,一次插入多条语句-->
    <insert id="insertBatch">
        insert into t_car values
        <foreach collection="cars" item="car" separator=",">
            (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
        </foreach>
    </insert>
    <delete id="deleteBatchById">
        delete from t_car where
        <foreach collection="ids" separator="or" item="id">
            id = #{id}
        </foreach>
    </delete>
</mapper>
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface CarMapper
{
    int deleteBatchById(@Param("ids")Long[] ids);
    //批量插入,一次插入多条car信息
    int insertBatch(@Param("cars") List<Car> cars);
    //使用foreach标签批量删除
    int deleteByIds(@Param("ids") Long[] ids);
    List<Car> selectWith(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);
    int updateBySet(Car car);
    int updateById(Car car);
    List<Car> selectByMultiConditionWithTrim(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);
    //使用where标签让SQL子句更加智能
    List<Car> selectByMultiConditionWithWhere(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);
    //多条件查询,根据汽车品牌指导价和汽车类型实现查询
    List<Car> selectByMultiCondition(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);

}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface CarMapper
{
    int deleteBatchById(@Param("ids")Long[] ids);
    //批量插入,一次插入多条car信息
    int insertBatch(@Param("cars") List<Car> cars);
    //使用foreach标签批量删除
    int deleteByIds(@Param("ids") Long[] ids);
    List<Car> selectWith(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);
    int updateBySet(Car car);
    int updateById(Car car);
    List<Car> selectByMultiConditionWithTrim(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);
    //使用where标签让SQL子句更加智能
    List<Car> selectByMultiConditionWithWhere(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);
    //多条件查询,根据汽车品牌指导价和汽车类型实现查询
    List<Car> selectByMultiCondition(@Param("brand") String brand,@Param("guidePrice") Double guidePrice,@Param("carType") String carType);

}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class TestCarMapper
{
    private static final Logger logger = LoggerFactory.getLogger(TestCarMapper.class);
    @Test
    public void TestSelectByMultiCondition()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiCondition("比亚迪",3.0,"燃油车");
        carList.forEach(car -> {
            logger.info(car.toString());
        });
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestNull()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiCondition(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestNum()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiCondition("长安",null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestSelectByMultiConditionWithWhere()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithWhere("比亚迪",3.0,"燃油车");
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestNullFor()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithWhere(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestNullOne()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithWhere(null,3.0,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestSelectByMultiConditionWithTrim()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithTrim("比亚迪",3.0,"燃油车");
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestSelectByMultiNull()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithTrim(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestupdateById()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(5L,null,null,null,null,"新能源");
        int count = mapper.updateById(car);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestupdateBySet()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(49L,null,null,null,null,"新能源");
        int count = mapper.updateBySet(car);
        logger.info("" + count);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestselectWith()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectWith(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestdeleteByIds()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Long[] ids = {48L,16L,17L};
        int count = mapper.deleteByIds(ids);
        logger.info("条数" + count);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestinsertBatch()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> cars = new ArrayList<>();
        cars.add(new Car(null,"1200","长城",15.0,"2020-10-11","燃油车"));
        cars.add(new Car(null,"1200","奇瑞",15.0,"2020-10-11","燃油车"));
        cars.add(new Car(null,"1200","江淮",15.0,"2020-10-11","燃油车"));
        int count = mapper.insertBatch(cars);
        System.out.println(count);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestdeleteBatchById()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Long[] ids = {4L};
        mapper.deleteBatchById(ids);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class TestCarMapper
{
    private static final Logger logger = LoggerFactory.getLogger(TestCarMapper.class);
    @Test
    public void TestSelectByMultiCondition()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiCondition("比亚迪",3.0,"燃油车");
        carList.forEach(car -> {
            logger.info(car.toString());
        });
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestNull()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiCondition(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestNum()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiCondition("长安",null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestSelectByMultiConditionWithWhere()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithWhere("比亚迪",3.0,"燃油车");
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestNullFor()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithWhere(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestNullOne()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithWhere(null,3.0,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestSelectByMultiConditionWithTrim()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithTrim("比亚迪",3.0,"燃油车");
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestSelectByMultiNull()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectByMultiConditionWithTrim(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
    }
    @Test
    public void TestupdateById()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(5L,null,null,null,null,"新能源");
        int count = mapper.updateById(car);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestupdateBySet()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(49L,null,null,null,null,"新能源");
        int count = mapper.updateBySet(car);
        logger.info("" + count);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestselectWith()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectWith(null,null,null);
        carList.forEach(car -> {
            logger.info(car.toString());
        });
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestdeleteByIds()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Long[] ids = {48L,16L,17L};
        int count = mapper.deleteByIds(ids);
        logger.info("条数" + count);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestinsertBatch()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> cars = new ArrayList<>();
        cars.add(new Car(null,"1200","长城",15.0,"2020-10-11","燃油车"));
        cars.add(new Car(null,"1200","奇瑞",15.0,"2020-10-11","燃油车"));
        cars.add(new Car(null,"1200","江淮",15.0,"2020-10-11","燃油车"));
        int count = mapper.insertBatch(cars);
        System.out.println(count);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
    @Test
    public void TestdeleteBatchById()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Long[] ids = {4L};
        mapper.deleteBatchById(ids);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Mybatis-Plus提供了一个方便的批量删除方法,可以通过以下步骤实现: 1. 在Mapper接口中定义批量删除方法方法名可以自定义,例如: ``` int batchDelete(List<Long> ids); ``` 2. 在Mapper.xml文件中实现批量删除方法,使用Mybatis-Plus提供的批量删除语句: ``` <delete id="batchDelete" parameterType="java.util.List"> delete from table_name where id in <foreach collection="list" item="id" open="(" separator="," close=")"> #{id} </foreach> </delete> ``` 3. 在Service层中调用批量删除方法,例如: ``` List<Long> ids = Arrays.asList(1L, 2L, 3L); int count = mapper.batchDelete(ids); ``` 以上就是使用Mybatis-Plus实现批量删除方法。 ### 回答2: mybatis-plus是一款优秀的ORM框架,提供了许多方便的操作方法,其中包括批量删除。在使用mybatis-plus进行批量删除时,需要使用Mapper接口中的deleteBatchIds方法,具体操作步骤如下: 1.编写Mapper接口 首先需要编写Mapper接口,定义批量删除方法,如下所示: ``` public interface UserMapper extends BaseMapper<User> { void deleteBatchIds(List<String> ids); } ``` 其中,User是实体类,ids是待删除数据的id列表。 2.编写Mapper.xml文件 接着需要编写UserMapper.xml文件,实现批量删除SQL语句,如下所示: ``` <delete id="deleteBatchIds" parameterType="java.util.List"> delete from user where id in <foreach collection="list" item="id" open="(" separator="," close=")"> #{id} </foreach> </delete> ``` 其中,#{id}表示id列表中的元素,list表示参数列表,item表示元素,open表示开头,separator表示分隔符,close表示结尾。 3.调用Mapper中的deleteBatchIds方法 在需要执行批量删除的地方,可以通过注入UserMapper对象来调用deleteBatchIds方法,如下所示: ``` @Autowired private UserMapper userMapper; public void deleteBatch(List<String> ids) { userMapper.deleteBatchIds(ids); } ``` 其中,ids是待删除数据的id列表,调用deleteBatchIds方法即可实现批量删除操作。 综上所述,使用mybatis-plus进行批量删除操作相对简单,只需要定义Mapper接口和Mapper.xml文件,然后调用方法即可。在实际应用中,可以结合其他操作方法实现更加高效的数据操作。 ### 回答3: MyBatis-Plus 是基于 MyBatis 的增强工具,在 MyBatis 的基础上提供了丰富的实用功能,包括批量删除数据MyBatis-Plus 批量删除方法是通过使用 MyBatis-Plus 提供的 Wrapper 条件构造器实现的。 批量删除数据方法通常会传入一个包含多个主键值的 List 集合参数,这些主键值对应了要删除数据的主键值。使用 MyBatis-Plus 实现批量删除时,我们可以通过以下步骤来完成: 1. 定义 Wrapper 条件构造器对象:使用 MyBatis-Plus 提供的条件构造器可以创建出符合条件的 WHERE 语句,我们需要定义一个 Wrapper 对象。 2. 将主键值添加到 Wrapper 对象中:使用 Wrapper 对象的 in 方法将主键值添加到 Wrapper 对象中,实现批量删除的条件。 3. 调用 BaseMapper 的 delete 方法:在 Mapper 接口中定义一个 delete 方法,并使用 @Param 注解指定参数名称。在方法体中调用 BaseMapper 的 delete 方法,传入 Wrapper 对象作为参数,即可完成批量删除操作。 以下是批量删除的示例代码: ```java public interface UserMapper extends BaseMapper<User> { @Delete("<script>" + "DELETE FROM tb_user WHERE id IN " + "<foreach collection='list' item='id' open='(' separator=',' close=')'>" + "#{id}" + "</foreach>" + "</script>") void deleteBatchIds(@Param("list") List<Long> ids); } ``` 以上代码中使用了 MyBatis-Plus 提供的 BaseMapper 接口和 @Delete 注解来实现批量删除操作。该示例中使用了 SQL 语句,删除操作从 tb_user 表中删除 id 在传入的主键集合中的数据。 总的来说,使用 MyBatis-Plus 来实现批量删除操作非常方便,只需要使用 Wrapper 对象添加条件即可。MyBatis-Plus 在这方面做了很多封装和优化,让代码更加简洁、易读。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧约Alatus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值