国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot 中批量執(zhí)行 SQL 腳本的實(shí)踐

這篇具有很好參考價(jià)值的文章主要介紹了Spring Boot 中批量執(zhí)行 SQL 腳本的實(shí)踐。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

在Spring Boot應(yīng)用中,有時(shí)候我們需要批量執(zhí)行存儲(chǔ)在數(shù)據(jù)庫(kù)中的 SQL 腳本。本文將介紹一個(gè)實(shí)際的案例,演示如何通過(guò) Spring Boot、MyBatis 和數(shù)據(jù)庫(kù)來(lái)實(shí)現(xiàn)這一目標(biāo)。

0、數(shù)據(jù)庫(kù)層

CREATE TABLE batchUpdate (

    id INT AUTO_INCREMENT PRIMARY KEY,
    update_type VARCHAR(255) NOT NULL,
    success_flag BOOLEAN NOT NULL,
    failure_count INT NOT NULL,
    execution_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    sql_script TEXT NOT NULL
);

  


-- 第一條數(shù)據(jù)

INSERT INTO `batch_update` (`update_type`, `success_flag`, `failure_count`, `update_count`, `sql_script`)

VALUES

('update_type_1', 0, 0, 0,

'UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_1'';

UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_1'';

UPDATE batch_update SET failure_count = failure_count + 1, update_count = update_count + 1 WHERE update_type = ''update_type_1'';');

  


-- 第二條數(shù)據(jù)

INSERT INTO `batch_update` (`update_type`, `success_flag`, `failure_count`, `update_count`, `sql_script`)

VALUES

('update_type_2', 0, 0, 0,

'UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_2'';

UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_2'';

UPDATE batch_update SET failure_count = failure_count + 1, update_count = update_count + 1 WHERE update_type = ''update_type_2'';');

  


-- 第三條數(shù)據(jù)

INSERT INTO `batch_update` (`update_type`, `success_flag`, `failure_count`, `update_count`, `sql_script`)

VALUES

('update_type_1', 0, 0, 0,

'UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_1'';

UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_1'';

UPDATE batch_update SET failure_count = failure_count + 1, update_count = update_count + 1 WHERE update_type = ''update_type_1'';');

  


-- 第四條數(shù)據(jù)

INSERT INTO `batch_update` (`update_type`, `success_flag`, `failure_count`, `update_count`, `sql_script`)

VALUES

('update_type_2', 0, 0, 0,

'UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_2'';

UPDATE batch_update SET success_flag = 1, update_count = update_count + 1 WHERE update_type = ''update_type_2'';

UPDATE batch_update SET failure_count = failure_count + 1, update_count = update_count + 1 WHERE update_type = ''update_type_2'';');

1. 控制器層(Controller)

@RestController
@RequestMapping("/batchUpdate")
@AllArgsConstructor
public class BatchUpdateController {

    private BatchUpdateService batchUpdateService;

    @PostMapping("/executeScript/{updateType}")
    public String executeScript(@PathVariable String updateType) {
        List<BatchUpdateEntity> batchUpdateEntities = batchUpdateService.findByUpdateType(updateType);

        if (batchUpdateEntities.isEmpty()) {
            return "Update type not found.";
        }

        for (BatchUpdateEntity batchUpdateEntity : batchUpdateEntities) {
            batchUpdateService.executeSqlScriptBatch(batchUpdateEntity);
        }

        return "SQL scripts executed successfully.";
    }
}

2. 服務(wù)層(Service)

javaCopy code
@Service
@AllArgsConstructor
public class BatchUpdateService {

    private BatchUpdateMapper batchUpdateMapper;

    public List<BatchUpdateEntity> findByUpdateType(String updateType) {
        return batchUpdateMapper.findByUpdateType(updateType);
    }

    public void executeSqlScriptBatch(BatchUpdateEntity batchUpdateEntity) {
        String sqlScript = batchUpdateEntity.getSqlScript();
        List<String> sqlScripts = Arrays.stream(sqlScript.split(";"))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .toList();
        System.out.println(sqlScripts.size());
        sqlScripts.forEach(sql -> {
            System.out.println("要執(zhí)行的sql:" + sql);
            batchUpdateMapper.executeSqlScript(sql);
            System.out.println("已執(zhí)行的sql:" + sql);
        });
    }
}

3. 數(shù)據(jù)訪問(wèn)層(Mapper)

@Mapper
public interface BatchUpdateMapper {

    List<BatchUpdateEntity> findByUpdateType(@Param("updateType") String updateType);
    void executeSqlScript(@Param("sql") String sql);
}

4. MyBatis 配置文件(BatchUpdateMapper.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.lfsun.sqlscript.BatchUpdateMapper">

    <resultMap id="BatchUpdateEntityResultMap" type="com.lfsun.sqlscript.BatchUpdateEntity">
        <id property="id" column="id" />
        <result property="updateType" column="update_type" />
        <result property="successFlag" column="success_flag" />
        <result property="failureCount" column="failure_count" />
        <result property="executionTime" column="execution_time" />
        <result property="sqlScript" column="sql_script" />
    </resultMap>

    <select id="findByUpdateType" resultMap="BatchUpdateEntityResultMap">
        SELECT * FROM batch_update WHERE update_type = #{updateType}
    </select>
    <update id="executeSqlScript">
        ${sql}
    </update>
</mapper>

Spring Boot 中批量執(zhí)行 SQL 腳本的實(shí)踐,# Java 框架,# RDBMS,spring boot,sql

Spring Boot 中批量執(zhí)行 SQL 腳本的實(shí)踐,# Java 框架,# RDBMS,spring boot,sql

通過(guò)這個(gè)案例,可以學(xué)到如何在 Spring Boot 中通過(guò) MyBatis 實(shí)現(xiàn)批量執(zhí)行 SQL 腳本的功能。在控制器層中,我們通過(guò) @PostMapping 注解定義了一個(gè)接口,接收 updateType 作為路徑參數(shù),然后調(diào)用服務(wù)層的方法。服務(wù)層中,我們通過(guò) MyBatis 執(zhí)行 SQL 腳本,實(shí)現(xiàn)了對(duì)數(shù)據(jù)庫(kù)的批量操作。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-795766.html

到了這里,關(guān)于Spring Boot 中批量執(zhí)行 SQL 腳本的實(shí)踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java Spring Boot 開(kāi)發(fā)框架

    Spring Boot是一種基于Java編程語(yǔ)言的開(kāi)發(fā)框架,它的目標(biāo)是簡(jiǎn)化Java應(yīng)用程序的開(kāi)發(fā)過(guò)程。Spring Boot提供了一種快速、易于使用的方式來(lái)創(chuàng)建獨(dú)立的、生產(chǎn)級(jí)別的Java應(yīng)用程序。本文將介紹Spring Boot的特性、優(yōu)勢(shì)以及如何使用它來(lái)開(kāi)發(fā)高效、可靠的應(yīng)用程序。 Spring Boot是由Pivotal團(tuán)隊(duì)

    2024年02月08日
    瀏覽(24)
  • 初級(jí) - 若依框架 - Java Spring/Spring Boot 項(xiàng)目理解記錄

    一般情況下,我們創(chuàng)建對(duì)象都是 類(lèi)名 + 類(lèi)引用名 = new 類(lèi)名() 但是如果是不想要 等于號(hào)后面的對(duì)象實(shí)例化操作,那么可以使用 @Autowired 注解,當(dāng)然這是在使用 Spring 時(shí),才能這樣,不然一般情況下,也沒(méi)法用這個(gè)注解。用了這個(gè) @Autowired 注解,會(huì)讓 Spring 自動(dòng)幫你托管這個(gè)對(duì)象

    2024年02月16日
    瀏覽(17)
  • Java 中 Spring Boot 框架下的 Email 開(kāi)發(fā)

    Java 中 Spring Boot 框架下的 Email 開(kāi)發(fā)

    hutool工具包: 這個(gè)類(lèi)更貼近我們的常見(jiàn)信息 ,用這個(gè)去構(gòu)造郵箱框架的指定郵件類(lèi)的構(gòu)造。 郵箱格式檢查: 以 yeah.net 郵箱為例(其他的郵箱也會(huì)有,舉一反三) 根據(jù)指導(dǎo)即可,獲得授權(quán)密碼! 以smtp為例: 這個(gè)就是host,可以查一下,port是465(其他的服務(wù)器 port是啥,一查

    2024年04月08日
    瀏覽(23)
  • shell腳本-批量主機(jī)執(zhí)行命令(expect)

    上次連接多臺(tái)服務(wù)器使用ssh-keygen,24機(jī)器去連接22、25,所以存在.ssh/authorized_keys 1.如果有.ssh/authorized_keys該文件則先刪除 1.expect命令含義 expect是一種腳本語(yǔ)言,它能夠代替人工實(shí)現(xiàn)與終端的交互,主要應(yīng)用于執(zhí)行命令和程序時(shí),系統(tǒng)以交互形式要求輸入指定字符串,實(shí)現(xiàn)交互

    2024年02月13日
    瀏覽(17)
  • shell批量執(zhí)行命令與文件傳輸腳本

    shell批量執(zhí)行命令與文件傳輸腳本

    對(duì)未進(jìn)行主機(jī)信任操作的服務(wù)器進(jìn)行批量操作 由于ssh只能在交互模式中輸入服務(wù)器密碼進(jìn)行登錄登操作,不便于進(jìn)行大批量服務(wù)器進(jìn)行巡檢或日志采集。sshpass恰好又解決了這個(gè)問(wèn)題,使用 ssh -p passwd 可以實(shí)現(xiàn)命令行輸入密碼操作,便于進(jìn)行規(guī)模巡檢 首先需要在腳本執(zhí)行機(jī)器

    2024年02月08日
    瀏覽(25)
  • Java 框架面試題-Spring Boot自定義配置與自動(dòng)配置共存

    Java 框架面試題-Spring Boot自定義配置與自動(dòng)配置共存

    Spring Boot 是一個(gè)快速開(kāi)發(fā)框架,可以簡(jiǎn)化 Spring 應(yīng)用程序的開(kāi)發(fā),其中自定義配置是其中一個(gè)非常重要的特性。 在 Spring Boot 中,自定義配置允許開(kāi)發(fā)者以自己的方式來(lái)配置應(yīng)用程序。自定義配置可以用于覆蓋默認(rèn)配置,也可以用于添加新的配置項(xiàng)。本文將詳細(xì)介紹 java框架面

    2023年04月11日
    瀏覽(24)
  • 探索Java中最常用的框架:Spring、Spring MVC、Spring Boot、MyBatis和Netty

    探索Java中最常用的框架:Spring、Spring MVC、Spring Boot、MyBatis和Netty

    ??歡迎來(lái)到Java面試技巧專(zhuān)欄~探索Java中最常用的框架:Spring、Spring MVC、Spring Boot、MyBatis和Netty ☆* o(≧▽≦)o *☆嗨~我是IT·陳寒?? ?博客主頁(yè):IT·陳寒的博客 ??該系列文章專(zhuān)欄:Java面試技巧 ??其他專(zhuān)欄:Java學(xué)習(xí)路線 Java面試技巧 Java實(shí)戰(zhàn)項(xiàng)目 AIGC人工智能 數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)

    2024年02月08日
    瀏覽(31)
  • 手把手搭建 java spring boot 框架 maven 項(xiàng)目 web 網(wǎng)址訪問(wèn)

    手把手搭建 java spring boot 框架 maven 項(xiàng)目 web 網(wǎng)址訪問(wèn)

    第一步我們?nèi)? spring boot 官網(wǎng)創(chuàng)建項(xiàng)目并下載壓縮包? 創(chuàng)建項(xiàng)目網(wǎng)址: Spring Initializr https://start.spring.io/ 我們添加一個(gè) srping web 的拓展包 接下來(lái)我們點(diǎn)擊 generate?創(chuàng)建 并下載壓縮包即可 接下來(lái)我們將壓縮文件包解壓到項(xiàng)目根目錄使用編輯器打開(kāi)即可,如果編輯器提示?點(diǎn)擊構(gòu)

    2024年04月23日
    瀏覽(22)
  • mybatis配置批量執(zhí)行SQL

    foreach 標(biāo)簽插入數(shù)據(jù)量大時(shí),明顯影響效率,個(gè)人傾向以下配置? applicationcontext 配置文件 Java代碼 mybatis文件沒(méi)什么可說(shuō)的,就是普通單條插入的insert 標(biāo)簽即可

    2024年02月12日
    瀏覽(23)
  • DBeaver SQL腳本執(zhí)行配置

    DBeaver SQL腳本執(zhí)行配置

    對(duì)生產(chǎn)環(huán)境數(shù)據(jù)遷移到測(cè)試環(huán)境時(shí),單條執(zhí)行可能時(shí)間過(guò)長(zhǎng)、或者內(nèi)存裝不下。最快的方法是執(zhí)行SQL腳本。 本地?cái)?shù)據(jù)庫(kù)客戶端:DBeaver 該客戶端是不自帶腳本執(zhí)行客戶端的,如下圖: mysql官網(wǎng)下載workbench,可以連通客戶端執(zhí)行組件一起下載下來(lái):MySQL :: Download MySQL Workbench 之后

    2024年01月17日
    瀏覽(25)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包