存储过程创建及springboot代码调用存储过程方式
目录
- 存储过程创建及springboot代码调用存储过程
- 我是在navacat中创建的存储过程
- 右键函数选择新建函数
- 自定义函数名,选择过程
- 然后添加输入输出参数点击完成
- 右键运行存储过程函数也是生效的
- 接下来就要考虑在项目中
- 在自己需要用的地方调用即可
- 总结
存储过程创建及springboot代码调用存储过程
阿里推荐最好不使用存储过程,因为存储过程代码过长涉及逻辑太多,导致修改业务时存储过程代码难以下手;于是没看过存储过程;
导致要用的时候不会,但是作为一名开发还是要会存储过程,于是百度学习了一波在此记录;
我是在navacat中创建的存储过程
右键函数选择新建函数
自定义函数名,选择过程
然后添加输入输出参数点击完成
我这里是输出了三个参数;这样存储过程就创建完成了;
右键运行存储过程函数也是生效的
接下来www.devze.com就要考虑在项目中
如何实现调用创建好的存储过程;
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<pythonDataInfoPo> doInTransaction(Trwww.devze.comansactionStatus 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; } }); } } }
在自己需要用的地方调用即可
这样就完成啦。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论