使用MyBatis批量查询(select)、批量插入(insert)、批量更新(update)、批量删除(delete)

记录:412

场景:使用MyBatis批量查询(select)、批量插入(insert)、批量更新(update)、批量删除(delete)操作MySQL数据库。

版本:JDK 1.8,Spring Boot 2.6.3,mybatis-3.5.9,MySQL-5.7.33。

1.批量查询

提供两种方式。

方式一,返回值: List

方式二,返回值: List>。

1.1在CityBatchMapper中接口代码

@Repository
public interface CityBatchMapper {
  // 1.1批量查询
  List queryCity1_1(List paraList);
  // 2.1批量查询
}

1.2 在CityBatchMapper.xml中SQL代码




  
  
  
  

2.批量插入

2.1在CityBatchMapper中接口代码

@Repository
public interface CityBatchMapper {
  // 1.2批量插入
  int insertCity1_2(List cityList);
  // 2.2批量插入
  int insertCity2_2(List> cityList);
}

2.2 在CityBatchMapper.xml中SQL代码




  
  
    insert into t_city_01 (CITY_ID,
    CITY_NAME,
    LAND_AREA,
    POPULATION,
    GROSS,
    CITY_DESCRIBE,
    DATA_YEAR,
    UPDATE_TIME)
    values
    
        (#{cityPO.cityId},
        #{cityPO.cityName},
        #{cityPO.landArea},
        #{cityPO.population},
        #{cityPO.gross},
        #{cityPO.cityDescribe},
        #{cityPO.dataYear},
        #{cityPO.updateTime})
    
  
  
  
    insert into t_city_01 (CITY_ID,
    CITY_NAME,
    LAND_AREA,
    POPULATION,
    GROSS,
    CITY_DESCRIBE,
    DATA_YEAR,
    UPDATE_TIME)
    values
    
        (#{cityPO.cityId},
        #{cityPO.cityName},
        #{cityPO.landArea},
        #{cityPO.population},
        #{cityPO.gross},
        #{cityPO.cityDescribe},
        #{cityPO.dataYear},
        #{cityPO.updateTime})
    
  

3.批量更新

示例使用批量更新时,数据源请求URL需添加配置:allowMultiQueries=true。

3.1在CityBatchMapper中接口代码

@Repository
public interface CityBatchMapper {
  // 1.3批量更新
  int updateCity1_3(List cityList);
  // 2.3批量更新
  int updateCity2_3(List> cityList);
}

3.2 在CityBatchMapper.xml中SQL代码




  
  
    update
    t_city_01
    set CITY_DESCRIBE='杭州是一个发达城市'
    WHERE CITY_ID IN
    
        #{cityPO.cityId}
    
  
  
  
    
        update
        t_city_01
        set CITY_DESCRIBE = #{cityPO.cityDescribe}
        where CITY_ID=#{cityPO.cityId}
    
  

4.批量删除

4.1在CityBatchMapper中接口代码

@Repository
public interface CityBatchMapper {
  // 1.4批量删除
  int deleteCity1_4(List cityList);
  // 2.4批量删除
  int deleteCity2_4(List> cityList);
}

4.2 在CityBatchMapper.xml中SQL代码




  
  
    delete from t_city_01 where 1=1
    
        AND CITY_ID IN
        
            #{cityPO.cityId}
        
    
  
  
  
    delete from t_city_01 where 1=1
    
        AND CITY_ID IN
        
            #{cityPO.cityId}
        
    
  

5.测试代码

5.1代码

@Slf4j
@RestController
@RequestMapping("/hub/example/cityBatch")
public class CityBatchController {
  @Autowired
  private CityBatchMapper cityBatchMapper;
  @GetMapping("/load01")
  public Object load01() {
    log.info("测试开始...");
    List paraList =  Arrays.asList(1L,2L,3L);
    // 1.批量查询结果集: List
    List list01 = cityBatchMapper.queryCity1_1(paraList);
    // 2.批量插入参数集: List
    cityBatchMapper.insertCity1_2(list01);
    // 3.批量更新参数集: List
    cityBatchMapper.updateCity1_3(list01);
    // 4.批量删除: List
cityBatchMapper.deleteCity1_4(list01);
log.info("测试结束...");
    return "执行成功";
  }
  @GetMapping("/load02")
  public Object load02() {
    log.info("测试开始...");
    List paraList =  Arrays.asList(1L,2L);
    // 1.批量查询结果集: List>
    List> list02=cityBatchMapper.queryCity2_1(paraList);
    // 2.批量插入参数集: List>
    cityBatchMapper.insertCity2_2(list02);
    AtomicInteger add= new AtomicInteger();
    list02.forEach((map -> {
        map.put("cityDescribe","杭州是一个互联网城市"+"_"+add.get());
        add.getAndIncrement();
    }));
    // 3.批量更新参数集: List>
    cityBatchMapper.updateCity2_3(list02);
    // 4.批量删除: List>
    cityBatchMapper.deleteCity2_4(list02);
    log.info("测试结束...");
    return "执行成功";
  }
}

5.2测试请求

URL01: http://127.0.0.1:18080/hub-example/hub/example/cityBatch/load01

URL02: http://127.0.0.1:18080/hub-example/hub/example/cityBatch/load02

6.基础支撑

6.1实体对象

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CityPO implements Serializable {
  private Long cityId;
  private String cityName;
  private Double landArea;
  private Long population;
  private Double gross;
  private String cityDescribe;
  private String dataYear;
  private Date updateTime;
}

6.2建表语句

CREATE TABLE t_city (
  CITY_ID BIGINT(16) NOT NULL COMMENT '唯一标识',
  CITY_NAME VARCHAR(64) COLLATE utf8_bin NOT NULL COMMENT '城市名',
  LAND_AREA DOUBLE DEFAULT NULL COMMENT '城市面积',
  POPULATION BIGINT(16) DEFAULT NULL COMMENT '城市人口',
  GROSS DOUBLE DEFAULT NULL COMMENT '生产总值',
  CITY_DESCRIBE VARCHAR(512) COLLATE utf8_bin DEFAULT NULL COMMENT '城市描述',
  DATA_YEAR VARCHAR(16) COLLATE utf8_bin DEFAULT NULL COMMENT '数据年份',
  UPDATE_TIME DATETIME DEFAULT NULL COMMENT '更新时间'
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='城市信息表';

6.3引入MyBatis依赖

使用mybatis-spring-boot-starter方式引入mybatis,对应mybatis-3.5.9和mybatis-spring-2.0.7核心依赖。


  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  2.2.2

6.4application.yml配置

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/hub_a_db?allowMultiQueries=true
    username: hub_a
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  jackson:
    time-zone: GMT+8
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

以上,感谢。

2023年4月17日

你可能感兴趣的:(L02-SpringBoot,使用MyBatis,批量查询select,批量插入insert,批量更新update,批量删除delete)