MyBatis批量查询、插入、更新、删除如何实现

mybatis批量查询、插入、更新、删除如何实现

本文讲解"mybatis批量查询、插入、更新、删除怎么实现",希望能够解决相关问题。

1.批量查询

提供两种方式。

方式一,返回值: list。

方式二,返回值: list        select city_id as "cityid",     city_name as "cityname",     land_area as "landarea",     population as "population",     gross as "gross",     city_describe as "citydescribe",     data_year as "datayear",     update_time as "updatetime"     from t_city     where city_id in              #{cityid}                   select city_id as "cityid",     city_name as "cityname",     land_area as "landarea",     population as "population",     gross as "gross",     city_describe as "citydescribe",     data_year as "datayear",     update_time as "updatetime"     from t_city     where city_id in              #{cityid}        

2.批量插入

2.1在citybatchmapper中接口代码

@repository
public interface citybatchmapper {
  // 1.2批量插入
  int insertcity1_2(list citylist);
  // 2.2批量插入
  int insertcity2_2(list
       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
       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
       delete from t_city_01 where 1=1
                    
相关文章