存儲(chǔ)過(guò)程創(chuàng)建及springboot代碼調(diào)用存儲(chǔ)過(guò)程方式
存儲(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)文章
詳解Java利用實(shí)現(xiàn)對(duì)稱加密(DES、3DES、AES)
本篇文章主要介紹了Java利用實(shí)現(xiàn)對(duì)稱加密(DES、3DES、AES),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Java實(shí)戰(zhàn)項(xiàng)目 圖書管理系統(tǒng)
這篇文章主要介紹了使用java SSM jsp mysql maven設(shè)計(jì)實(shí)現(xiàn)的精品圖書管理系統(tǒng),是一個(gè)很好的實(shí)例,對(duì)大家的學(xué)習(xí)和工作具有借鑒意義,建議收藏一下2021-09-09Java整合mybatis實(shí)現(xiàn)過(guò)濾數(shù)據(jù)
這篇文章主要介紹了Java整合mybatis實(shí)現(xiàn)過(guò)濾數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01javascript與jsp發(fā)送請(qǐng)求到servlet的幾種方式實(shí)例
本文分別給出了javascript發(fā)送請(qǐng)求到servlet的5種方式實(shí)例與 jsp發(fā)送請(qǐng)求到servlet的6種方式實(shí)例2018-03-03Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對(duì)象接收
這篇文章主要介紹了Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對(duì)象接收,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Spring @ExceptionHandler注解統(tǒng)一異常處理和獲取方法名
這篇文章主要介紹了Spring注解之@ExceptionHandler 統(tǒng)一異常處理和獲取方法名,在實(shí)際項(xiàng)目中,合理使用@ExceptionHandler能夠提高代碼的可維護(hù)性和用戶體驗(yàn),通過(guò)本文的解析和實(shí)踐,讀者可以更好地理解和掌握@ExceptionHandler的用法和原理2023-09-09