• 【Mybatis】Mybatis的批量执行SQL语句 与 返回值问题


    【实验用到的数据库】

    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

    【实验用到的Mybatis版本】

           <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.9</version>
            </dependency>

    【使用到的表及原始数据】

    create table test04(
        id number(3),
        name nvarchar2(20),
        age number(3),
        primary key(id)
    )
    
    insert into test04(id,name,age) values('1','andy','22');
    insert into test04(id,name,age) values('2','bill','23');
    insert into test04(id,name,age) values('3','cindy','24');
    insert into test04(id,name,age) values('4','douglas','25');

    【Mapper.xml写法】

        <update id="updateTest2">
            BEGIN
            <choose>
                <when test="list!=null">
                    <foreach collection="list" item="emp">
                        update test04 set name=#{emp.name} where age=#{emp.age};
                    </foreach>
                </when>
            </choose>
            END;
        </update>

    【对应函数】

    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import java.util.List;
    
    @Mapper
    public interface Test03Mapper {
    ......
        int updateTest2(List<Emp> list);
    }

    【调用】

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MybaticUpdateTest2 {
        public static void main(String[] args) throws Exception{
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory =
                    new SqlSessionFactoryBuilder().build(inputStream);
    
            try (SqlSession session = sqlSessionFactory.openSession()) {
                Test03Mapper mapper = session.getMapper(Test03Mapper.class);
    
                List<Emp> list=new ArrayList<>();
                list.add(new Emp("Andy11",44));
                list.add(new Emp("Bill1",33));
    
                int cnt = mapper.updateTest2(list);
                System.out.println(cnt);
    
                session.commit();
            }
        }
    }

    【关于返回值的问题】

    最尴尬的是,批量执行SQL语句后,无论数据库里被改了几条记录,返回值总是-1 !

    而且,加了以下设置也无效:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <setting name="defaultExecutorType" value="SIMPLE"/>
        </settings>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
                    <property name="username" value="luna"/>
                    <property name="password" value="1234"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="Test03Mapper.xml"/>
        </mappers>
    </configuration>

    这和单条执行SQL,更新几条就返回几是不一样的。

    因此,批量更新后,不该拿返回值做判断。

    真要取变更记录数,当在java程序里循环,取单条更新语句的结果进行累计。

    【使用到的程序】

    大家可自行做实验。

     https://files.cnblogs.com/files/heyang78/RestAccessEs_220303AM.rar?t=1646275138

    END

  • 相关阅读:
    从底层来看Promise
    Promise初探
    时间复杂度和空间复杂度
    IDEA maven Run Maven 启动方式
    IDEA 运行maven工程报错:No goals have been specified for this build.....解决办法
    12.外键约束
    11.约束的概念及分类
    10.创建表-备注与创建表的细节说明。
    9.创建表-自增长(AUTO_INCREAMENT)
    8.创建表--主键的概念与应用
  • 原文地址:https://www.cnblogs.com/heyang78/p/15958647.html
Copyright © 2020-2023  润新知