最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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

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

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

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

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

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

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

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 存儲過程
 * @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)文章

最新評論

平乐县| 兴隆县| 宜川县| 辉南县| 岳普湖县| 交口县| 威信县| 郴州市| 巴中市| 策勒县| 枣阳市| 绥中县| 吐鲁番市| 桐庐县| 双城市| 溆浦县| 华宁县| 津市市| 卓资县| 蓬溪县| 洪泽县| 安阳市| 方城县| 宽甸| 承德县| 宜川县| 英山县| 格尔木市| 平潭县| 樟树市| 玛曲县| 苏尼特左旗| 噶尔县| 栖霞市| 驻马店市| 罗定市| 白银市| 罗田县| 莒南县| 凌源市| 张家川|