Mybatis-批量增加/更新/删除

Mybatis-批量增加/更新/删除

文章目录

  • Mybatis-批量增加/更新/删除
    • 一. 前置准备
      • 1.1 依赖
      • 1.2 数据库准备
      • 1.3 Springboot及Mybatis框架搭建
      • 1.4 logback
      • 1.5 前置知识
    • 二. 批量插入
      • 2.1 sql语句
      • 2.2 代码实现
    • 三. 批量更新
      • 3.1 sql语句
      • 3.2 代码实现
    • 四. 批量删除
      • 4.1 sql语句
      • 4.2 代码实现

一. 前置准备

1.1 依赖

	
	<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.5version>
    parent>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>2.4.5version>
        dependency>
		
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <version>2.4.5version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.3version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.21version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.12version>
            <scope>providedscope>
        dependency>

    dependencies>

1.2 数据库准备

表名:table

设置id主键自增

id name
主键 姓名

1.3 Springboot及Mybatis框架搭建

  • 包结构

Mybatis-批量增加/更新/删除_第1张图片

  • yml

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver #数据源配置
        url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf-seSSL=false
        username: root
        password: root
    mybatis:
      mapper-locations: classpath:mybatis/*.xml
      type-aliases-package: com.zhanghp.entity
    

1.4 logback


<configuration>
    
    <property name="LOG_HOME" value="${catalina.base}/logs/" />
    
    <appender name="Stdout" class="ch.qos.logback.core.ConsoleAppender">
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            pattern>
        layout>
    appender>
    
    <appender name="RollingFile"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <FileNamePattern>${LOG_HOME}/server.%d{yyyy-MM-dd}.logFileNamePattern>
            <MaxHistory>30MaxHistory>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            pattern>
        layout>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MBMaxFileSize>
        triggeringPolicy>
    appender>
    
    <root level="info">
        <appender-ref ref="Stdout" />

    root>
    
    <logger name="com.zhanghp.dao" level="DEBUG">logger>
    
    
configuration>

1.5 前置知识

  • mybatis的动态sql中的foreach参数
  1. collection="" 遍历的集合或者是数组,参数是数组,collection中名字指定为array,参数是List集合,collection中名字指定为list

参数是Map集合,collection名字指定为map

  1. separator="" 多个元素取出的时候 用什么文字分隔
  2. open="" 以什么开头
  3. close="" 以什么结尾
  4. item="" 中间变量名
  5. index="" 集合中元素迭代时的索引

二. 批量插入

2.1 sql语句

insert into person (id, name) values (1,'张三'), (2,'李四')

2.2 代码实现

  • mapper层
@Mapper
public interface PersonMapper {
    int insertBatch(List<Person> list);

    int insertBatch2(List<String> list);

    int insertBatchForMap(@Param("map")Map<Integer, String> map);
}
  • xml


<mapper namespace="com.zhanghp.dao.PersonMapper">
    <insert id="insertBatch">
        insert into person (id, name ) values
        <foreach collection="list" separator="," item="it">
            (#{it.id},#{it.name})
        foreach>
    insert>

    <insert id="insertBatch2">
        insert into person (name) values
        
            
            (#{it})
        foreach>
    insert>

    <insert id="insertBatchForMap">
        insert into person (id,name) values
        
            (#{key},#{val})
        foreach>
    insert>

mapper>
  • junit测试
@SpringBootTest(classes = BatchApplication.class)
public class TestDataBase {
   @Autowired
   private PersonMapper testMapper;
    @Test
    public void test2(){
        List<Person> list = new ArrayList<>();
        list.add(new Person(null,"1"));
        list.add(new Person(null,"2"));
        int i = testMapper.insertBatch(list);
        if (i>0) {
            System.out.println("yeah");
        }
    }


    @Test
    public void test3(){
        List<String> list = new ArrayList<>();
        list.add("test1");
        list.add("test2");
        int i = testMapper.insertBatch2(list);
        if (i>0) {
            System.out.println("yeah");
        }
    }

    @Test
    public void insertMap(){
        Map<Integer, String> map = new HashMap<>();
        map.put(null,"map123");
        map.put(null,"map222");
        int i = testMapper.insertBatchForMap(map);
        if (i>0) {
            System.out.println("yeah");
        }
    }
}

三. 批量更新

3.1 sql语句

insert into person (id,name) values (1,'2'),(2,'3') on duplicate key update name=values(name);

3.2 代码实现

  • mapper
int updateForNew(List<Person> personList);
  • xml
	 <update id="updateForNew">
        insert into person (id,name) values
        <foreach collection="list" separator="," item="it">
            (#{it.id},#{it.name})
        foreach>
        on duplicate key update name=values(name);
    update>
  • junit
	@Test
    public void updateForNew(){
        List<Person> list = new ArrayList<>();
        list.add(new Person(1,"1"));
        list.add(new Person(2,"2"));
        int i = testMapper.updateForNew(list);
        if (i>0) {
            System.out.println("yeah");
        }
    }

四. 批量删除

4.1 sql语句

4.2 代码实现

  • mapper
int deleteBatch(List<Integer> idList);
  • xml
	<delete id="deleteBatch">
        delete from person where id in
        <foreach collection="list" item="it" separator="," open="(" close=")">
            #{it}
        foreach>
    delete>
  • junit
	@Test
    public void deleteBatch(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        int i = testMapper.deleteBatch(list);
        if (i>0) {
            System.out.println("yeah");
        }
    }

你可能感兴趣的:(java,spring,boot,mybatis)