mybatis批量操作数据

pom引入依赖

  <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
</dependency>

<dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.4</version>
</dependency>
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

启动类添加mapper扫描

@MapperScan("com.example.demo.mapper")

mapper

@Repository
public interface vodMapper {

    int inserlist(List<vod>lists);

    int deleteList(int[] arrs);

    int updateList(List<vod> lists);
}

数据库连接配置要加上allowMultiQueries=true,不然不能批量修改

spring.datasource.url=jdbc:mysql://localhost:3306/itv_basic_v022?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

 

mappers.xml

 <!-- 批量添加 -->
<insert id="inserlist" parameterType="java.util.List" useGeneratedKeys="false" >
    insert into con_vod ( cp_id, con_name,
    first_letter, nike_name, clazz_flag,
    update_time, opt_staff_id, statuz,
    rtsp_bd, rtsp_hd, size,
    busi_type, time_long)
    values
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.cpId},
      #{item.conName},
      #{item.firstLetter},
      #{item.nikeName},
      #{item.clazzFlag},
      #{item.updateTime},
      #{item.optStaffId},
      #{item.statuz},
      #{item.rtspBd},
      #{item.rtspHd},
      #{item.size},
      #{item.busiType},
      #{item.timeLong}
      )
    </foreach>
  </insert>
 <!-- 批量删除 -->
  <delete id="deleteList" parameterType="int[]">
    delete from con_vod where con_id in
    <foreach collection="array" item="arr" index="no" open="("
             separator="," close=")">
      #{arr}
    </foreach>
  </delete>
  <!-- 批量更新 -->
  <update id="updateList" parameterType="java.util.List">
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
      update con_vod
      <set>
        con_name=#{item.conName}
      </set>
      where con_id = #{item.conId}
    </foreach>
  </update>

service

@Service
public interface vodService {
    public int addlist(List<vod> list);

  int deleteList(int[] arrs);

    int updateList(List<vod> lists);

}

serviceImpl 

@Component
public class vodServiceImpl implements vodService{
@Autowired
    vodMapper vodMapper;

    public int addlist(List<vod> list) {
  return   vodMapper.inserlist(list);
    }

  public  int deleteList(int[] arrs){
        return vodMapper.deleteList(arrs);
    }

    public int updateList(List<vod> lists){

        return  vodMapper.updateList(lists);
    }
}

controller

@Controller
public class vodController {
    @Autowired
  vodService vodService;

    @GetMapping("/addvod")
    @ResponseBody
    public void addvod(){
        List<vod> list=new ArrayList<>();
        vod vod=new vod(22l,"woshi","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748");
        vod vod1=new vod(22l,"woshi","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748");
        list.add(vod);
        list.add(vod1);
        System.out.println(vodService.addlist(list));
    }


//批量删除
    @GetMapping("/deletevod")
    @ResponseBody
    public void deleteList()
    {int[] arrs={11124,11125};
    System.out.println( vodService.deleteList(arrs));
    }

    //批量更新
    @GetMapping("/updatevod")
    @ResponseBody
    public void updateList()
    {  List<vod> list=new ArrayList<>();
        vod vod=new vod(11126l,22l,"abcd","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748","232");
        vod vod1=new vod(11127l,22l,"abcdes","saf","23","22","199723-2",22l,"1","http12382034","httwaaskds",1232,"13823748","232");
        list.add(vod);
        list.add(vod1);
        System.out.println( vodService.updateList(list));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

今朝花落悲颜色

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

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

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

打赏作者

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

抵扣说明:

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

余额充值