亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

存儲(chǔ)過(guò)程創(chuàng)建及springboot代碼調(diào)用存儲(chǔ)過(guò)程方式

 更新時(shí)間:2024年11月18日 09:26:29   作者:oNuoyi  
文章介紹了如何在Navicat中創(chuàng)建存儲(chǔ)過(guò)程,并在Spring Boot項(xiàng)目中調(diào)用存儲(chǔ)過(guò)程,存儲(chǔ)過(guò)程創(chuàng)建步驟包括選擇函數(shù)類(lèi)型、自定義函數(shù)名、添加參數(shù)等,在Spring Boot中調(diào)用存儲(chǔ)過(guò)程時(shí),可以通過(guò)JdbcTemplate或MyBatis等工具進(jìn)行

存儲(chǔ)過(guò)程創(chuàng)建及springboot代碼調(diào)用存儲(chǔ)過(guò)程

阿里推薦最好不使用存儲(chǔ)過(guò)程,因?yàn)榇鎯?chǔ)過(guò)程代碼過(guò)長(zhǎng)涉及邏輯太多,導(dǎo)致修改業(yè)務(wù)時(shí)存儲(chǔ)過(guò)程代碼難以下手;于是沒(méi)看過(guò)存儲(chǔ)過(guò)程;

導(dǎo)致要用的時(shí)候不會(huì),但是作為一名開(kāi)發(fā)還是要會(huì)存儲(chǔ)過(guò)程,于是百度學(xué)習(xí)了一波在此記錄;

我是在navacat中創(chuàng)建的存儲(chǔ)過(guò)程

右鍵函數(shù)選擇新建函數(shù)

自定義函數(shù)名,選擇過(guò)程

然后添加輸入輸出參數(shù)點(diǎn)擊完成

我這里是輸出了三個(gè)參數(shù);這樣存儲(chǔ)過(guò)程就創(chuàng)建完成了;

右鍵運(yùn)行存儲(chǔ)過(guò)程函數(shù)也是生效的

接下來(lái)就要考慮在項(xiàng)目中

如何實(shí)現(xiàn)調(diào)用創(chuàng)建好的存儲(chǔ)過(guò)程;

import com.lansi.realtynavi.mapper.pojo.DataInfoPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 存儲(chǔ)過(guò)程
 * @Date 2021/3/18 13:50
 * @Created by nuoyi
 */
@Component
public class ProcedureReturnListExecutor {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private TransactionTemplate template;
    public List<DataInfoPo> get(){
        ProcedureReturnListTransactionCallback callback = new ProcedureReturnListTransactionCallback();
        return template.execute(callback);
    }
    class ProcedureReturnListTransactionCallback implements TransactionCallback<List<DataInfoPo>> {
        @Override
        public List<DataInfoPo> doInTransaction(TransactionStatus transactionStatus) {
            return jdbcTemplate.execute(new CallableStatementCreator() {
                @Override
                public CallableStatement createCallableStatement(Connection con) throws SQLException {
                    String procedure = "{call selectData(?,?,?)}";
                    CallableStatement cs = con.prepareCall(procedure);
                    cs.registerOutParameter(1, Types.INTEGER);
                    cs.registerOutParameter(2, Types.BIGINT);
                    cs.registerOutParameter(3, Types.BIGINT);
                    return cs;
                }
            }, new CallableStatementCallback<List<DataInfoPo>>() {
                @Override
                public List<DataInfoPo> doInCallableStatement(CallableStatement cs)
                        throws SQLException, DataAccessException {
                    ResultSet rs = cs.executeQuery();
                    List<DataInfoPo> list = new ArrayList<>();
                    while (rs.next()) {
                        Integer id = rs.getInt(1);
                        Long dataInfo = rs.getLong(2);
                        Long dataTime = rs.getLong(3);
                        DataInfoPo dataInfoPo = new DataInfoPo();
                        dataInfoPo.setId(id);
                        dataInfoPo.setData_info(dataInfo);
                        dataInfoPo.setData_time(dataTime);
                        list.add(dataInfoPo);
                    }
                    return list;
                }
            });
        }
    }
}

在自己需要用的地方調(diào)用即可

這樣就完成啦。

總結(jié)

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

相關(guān)文章

最新評(píng)論