欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis批量新增的三種實(shí)現(xiàn)方式

 更新時(shí)間:2024年12月13日 14:15:51   作者:摩爾多0  
文章講述了在Java中進(jìn)行批量操作的最佳實(shí)踐,包括使用MyBatis的ExecutorType.BATCH模式,作者建議根據(jù)實(shí)際需求選擇是否進(jìn)行批量操作,因?yàn)榕坎僮鞑⒉豢偸悄芴嵘阅?且有副作用,如無法獲取自增ID等

導(dǎo)入依賴

 <!-- 數(shù)據(jù)庫驅(qū)動(dòng) -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <!--        web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

Dao

@Repository // 代表持久層
public interface DeptMapper  {

    int addDept(Dept dept);

    int foreachAdd(List<Dept> list);
}

記得在啟動(dòng)類上加

@MapperScan(“com.example.demo.dao”)

DeptMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.dao.DeptMapper">


<!--普通新增-->
    <insert id="addDept" parameterType="com.example.demo.pojo.Dept">
        INSERT INTO dept (dname, db_source) VALUES (#{dname},#{dbSource})
    </insert>


    <!--foreachMySql寫法-->
    <insert id="foreachAdd"  parameterType="java.util.List">
        insert into dept (
        dname,
        db_source

        )
        values
        <foreach collection="list" item="dept" index="index" separator="," >
            (
            #{dept.dname,jdbcType=VARCHAR},
            #{dept.dbSource,jdbcType=VARCHAR}

            )
        </foreach>
    </insert>


</mapper>

實(shí)體類

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
  private long deptno;
  private String dname;
  private String dbSource;
}

配置文件

# mysql 5 驅(qū)動(dòng)不同 com.mysql.jdbc.Driver
# mysql 8 驅(qū)動(dòng)不同com.mysql.cj.jdbc.Driver、需要增加時(shí)區(qū)的配置
serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/db01?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis-plus.mapper-locations=classpath:mapping/*Mapper.xml

核心測(cè)試類

@SpringBootTest
class DemoApplicationTests {


    @Autowired
    private DeptMapper deptMapper;


    //測(cè)試數(shù)據(jù)
    public List<Dept> textData(){
        ArrayList<Dept> deptList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            Dept dept = new Dept();
            dept.setDname("foreach");
            dept.setDbSource("db02");
            deptList.add(dept);
        }
        return deptList;
    }

//    循環(huán)插入
    @Test
    public void add(){
        List<Dept> textData = textData();//測(cè)試數(shù)據(jù)
        long start = System.currentTimeMillis();//開始時(shí)間
        for(Dept dept : textData){
            deptMapper.addDept(dept);
        }
        System.out.println(System.currentTimeMillis() - start);//統(tǒng)計(jì)時(shí)間
    }

//    foreach標(biāo)簽
    @Test
    public void foreach(){
        List<Dept> textData = textData();//測(cè)試數(shù)據(jù)
        long start = System.currentTimeMillis();//開始時(shí)間
        deptMapper.foreachAdd(textData);
        System.out.println(System.currentTimeMillis() - start);//統(tǒng)計(jì)時(shí)間
    }

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Test
    public void testInsertBatch(){

        List<Dept> textData = textData();//測(cè)試數(shù)據(jù)
        long start = System.currentTimeMillis();//開始時(shí)間
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
        DeptMapper studentMapperNew = sqlSession.getMapper(DeptMapper.class);
        textData.stream().forEach(student -> studentMapperNew.addDept(student));
        sqlSession.commit();
        sqlSession.clearCache();
        System.out.println(System.currentTimeMillis() - start);//統(tǒng)計(jì)時(shí)間
    }
}

總結(jié)

其實(shí)實(shí)際意義上來說,包括在程序里面for循環(huán)還是在sql里面for循環(huán)都不算是批量操作。

只有將ExecutorType設(shè)置為BATCH模式才是真正意義上的批量操作。

并且事實(shí)證明在sql循環(huán)時(shí)設(shè)置batch與否其實(shí)執(zhí)行時(shí)間差別不是很大,幾乎可以忽略不計(jì)。

所以其實(shí)如果不是特別要求性能??梢灾苯釉趕ql中使用for循環(huán)即可。

謹(jǐn)慎使用batch,如果需要使用batch,請(qǐng)?jiān)谛枰暮瘮?shù)上面設(shè)置batch,不要全局使用。

因?yàn)閎atch也是有副作用的。比如在Insert操作時(shí),在事務(wù)沒有提交之前,是沒有辦法獲取到自增的id,此外,對(duì)于update、delete無法返回更新、插入條數(shù)。這在某型情形下是不符合業(yè)務(wù)要求的。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論