存储过程创建及springboot代码调用存储过程

存储过程创建及springboot代码调用存储过程


阿里推荐最好不使用存储过程,因为存储过程代码过长涉及逻辑太多,导致修改业务时存储过程代码难以下手;于是没看过存储过程;
导致要用的时候不会,但是作为一名开发还是要会存储过程,于是百度学习了一波在此记录;
我是在navacat中创建的存储过程

在这里插入图片描述
右键函数选择新建函数
在这里插入图片描述
自定义函数名,选择过程;
在这里插入图片描述
然后添加输入输出参数点击完成;
我这里是输出了三个参数;这样存储过程就创建完成了;在这里插入图片描述
右键运行存储过程函数也是生效的
在这里插入图片描述
接下来就要考虑在项目中怎么实现调用创建好的存储过程;


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;
                }
            });
        }
    }
}

在这里插入图片描述
在自己需要用的地方调用即可
在这里插入图片描述
这样就完成啦。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oNuoyi

你的鼓励将是我创作的做大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值