首頁 > 軟體

前端傳引數進行Mybatis呼叫mysql儲存過程執行返回值詳解

2022-08-12 22:01:52

查詢資料庫中的儲存過程:

方法一:

select `name` from mysql.proc where db = 'your_db_name' and `type`; = 'PROCEDURE'

方法二:

 show procedure status;

你要先在資料庫中建一個表,然後建立儲存過程

我建的表a_tmp,儲存過程名稱bill_a_forbusiness

執行語句:  CALL bill_a_forbusiness(44,44,52,47,44,46,52,52,349171)

儲存過程呼叫方式:

CALL Pro_Get_CO2('2018','','','');
CALL Pro_Get_EnergyData('2017');
CALL Pro_Get_Carbon_OrgType('2014');
CALL 儲存過程名(引數);

檢視儲存過程或函數的建立程式碼:

show create procedure proc_name;
show create function func_name;

因為這個沒有返回值所以需要先傳參呼叫執行,再查詢

前端程式碼:

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="引數輸入" prop="a">
        <el-input
          v-model="queryParams.a"
          placeholder="請輸入第一引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="b">
        <el-input
          v-model="queryParams.b"
          placeholder="請輸入第二引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="c">
        <el-input
          v-model="queryParams.c"
          placeholder="請輸入第三引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="d">
        <el-input
          v-model="queryParams.d"
          placeholder="請輸入第四引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="e">
        <el-input
          v-model="queryParams.e"
          placeholder="請輸入第五引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="f">
        <el-input
          v-model="queryParams.f"
          placeholder="請輸入第六引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="g">
        <el-input
          v-model="queryParams.g"
          placeholder="請輸入第七引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="h">
        <el-input
          v-model="queryParams.h"
          placeholder="請輸入第八引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引數輸入" prop="abc">
        <el-input
          v-model="queryParams.abc"
          placeholder="請輸入第九引數"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="錄入人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="請輸入辦理人名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作日期" prop="recordDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.recordDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="選擇操作日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-top-right"
          size="mini"
          @click="handleQuery"
          >傳值/執行</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="returnQuery"
          >返回/查詢</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getProcList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="returnprocList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="序號"
        align="center"
        prop=""
        type="index"
        width="60"
      />
      <el-table-column label="記錄id" align="center" prop="Id" />
      <el-table-column
        label="第一引數趟次"
        align="center"
        prop="a"
        width="200"
      />
      <el-table-column label="第二引數趟次" align="center" prop="b" />
      <el-table-column label="第三引數趟次" align="center" prop="c" />
      <el-table-column label="第四引數趟次" align="center" prop="d" />
 <el-table-column label="第五引數趟次" align="center" prop="e" />
      <el-table-column label="第六引數趟次" align="center" prop="f" />
      <el-table-column label="第七引數趟次" align="center" prop="g" />
      <el-table-column label="第八引數趟次" align="center" prop="h" />
      <el-table-column label="趟次總金額" align="center" prop="abc" />
<!-- 重新整理查詢 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryparameters.pageNum"
      :limit.sync="queryparameters.pageSize"
      @pagination="getProcList"
    />
</template>

 端js程式碼:

<script>
import {
  listProc,
  getProc,
  delProc,
  addProc,
  updateProc,
  exportProc,
  returnProc,
} from "@/api/stock/proc";
 
export default {
  name: "Proc",
  dicts: ["record_type"],
  data() {
    return {
      // 遮罩層
      loading: true,
      // 顯示搜尋條件
      showSearch: true,
      // 總條數
      total: 0,
      // 儲存過程表格資料
      procList: [],
      returnprocList: [],

      // 查詢引數
      queryParams: {

        a: null,
        b: null,
        c: null,
        d: null,
        e: null,
        f: null,
        g: null,
        h: null,
        abc: null,
        //C: null,
      },
      queryparameters:{
        pageNum: 1,
        pageSize: 10,
        recordType: 1,

      },

    };
  },
  created() {
    this.getList();
    this.getProcList();

  },
  methods: {
    /** 查詢執行資料 */
    getList() {
      this.loading = true;
      listProc(this.queryParams).then((response) => {
        this.procList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     /** 查詢返回列表 */
    getProcList() {
      this.loading = true;
      returnProc(this.queryparameters).then((response) => {
        this.returnprocList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


    // 表單重置
    reset() {
      this.form = {
        Id: null,
        recordType: null,
        a: null,
        b: null,
        c: null,
        d: null,
        e: null,
        f: null,
        g: null,
        h: null,
        abc: null,
        t: null,
        tc: null,
        min1: null,

      };

    },
    /** 搜尋按鈕操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
     /** 返回重新整理按鈕操作 */
     returnQuery() {
      this.queryparameters.pageNum = 1;
      this.getProcList();
    },

    /** 重置按鈕操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

};
</script>

 介面程式碼:

import request from '@/utils/request'

// 查詢列表
export function listProc(query) {
    return request({
      url: '/stock/proc/list',
      method: 'get',
      params: query
    })
  }

// 查詢
export function returnProc(query) {
  return request({
    url: '/stock/proc/query',
    method: 'get',
    parameters: query
  })
}

Java程式碼:

controller:

@RestController
@RequestMapping("/stock/proc")
public class StockProcController extends BaseController
{
    @Autowired
    private IStockProcService stockProcService;

    /**
     * 查詢列表
     */
    //@PreAuthorize("@ss.hasPermi('stock:proc:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockProc stockProc)
    {
startPage();
            List<StockProc> paramlist = stockProcService.selectStockProcParamList(stockProc);
            //return getDataTable(paramlist);
        return null;

    }
/**
     * 獲取外出申請詳細資訊
     */
   @PreAuthorize("@ss.hasPermi('stock:Proc:query')")
    @GetMapping("/query")
    public TableDataInfo getInfo(StockProc stockProc)
    {
        startPage();
        List<StockProc> list = stockProcService.selectStockProcList(stockProc);

        return getDataTable(list);
    }
}

實體層:

dao/dto

package com.ruoyi.stock.domain;
 
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
 * 儲存過程頁面
 *
 */
public class StockProc extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @Excel(name = "序號")
//    @NotBlank(message = "該欄位不能為空")
    private int id;

    /** 第一編號 */
    @Excel(name = "第一引數趟次")
    private int a;

    /** 第一編號 */
    @Excel(name = "第二引數趟次")
    private int b ;
    /** 第一編號 */
    @Excel(name = "第三引數趟次")
    private int c;
    /** 第一編號 */
    @Excel(name = "第四引數趟次")
    private int d;
    /** 第一編號 */
    @Excel(name = "第五引數趟次")
    private int e;
    /** 第一編號 */
    @Excel(name = "第六引數趟次")
    private int f;
    /** 第一編號 */
    @Excel(name = "第七引數趟次")
    private int g;
    /** 第一編號 */
    @Excel(name = "第八引數趟次")
    private int h;
    /** 第一編號 */
    @Excel(name = "趟次總金額")
    private int abc;
    /** 第一編號 */
    @Excel(name = "趟")
    private int t;
    /** 第一編號 */
    @Excel(name = "趟次")
    private int tc;
    /** 第一編號 */
    @Excel(name = "小計")
    private int min1;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }
    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }
 
    public void setC(int c) {
        this.c = c;
    }
 
    public int getD() {
        return d;
    }
 
    public void setD(int d) {
        this.d = d;
    }
 
    public int getE() {
        return e;
    }
    public void setE(int e) {
        this.e = e;
    }
    public int getF() {
        return f;
    }
    public void setF(int f) {
        this.f = f;
    }
    public int getG() {
        return g;
    }
    public void setG(int g) {
        this.g = g;
    }
    public int getH() {
        return h;
    }
    public void setH(int h) {
        this.h = h;
    }
    public int getAbc() {
        return abc;
    }
    public void setAbc(int abc) {
        this.abc = abc;
    }
    public int getT() {
        return t;
    }
    public void setT(int t) {
        this.t = t;
    }
    public int getTc() {
        return tc;
    }
    public void setTc(int tc) {
        this.tc = tc;
    }
    public int getMin1() {
        return min1;
    }
    public void setMin1(int min1) {
        this.min1 = min1;
    }
    @Override
    public String toString() {
        return "StockProc{" +
                "id=" + id +
                ", a=" + a +
                ", b=" + b +
                ", c=" + c +
                ", d=" + d +
                ", e=" + e +
                ", f=" + f +
                ", g=" + g +
                ", h=" + h +
                ", abc=" + abc +
                ", t=" + t +
                ", tc=" + tc +
                ", min1=" + min1 +
                '}';
    }
}

server層:

public interface IStockProcService
{
    /**
     * 查詢列表
     * @return 記錄集合
     */
    public List<StockProc> selectStockProcList(StockProc stockProc);
    public List<StockProc> selectStockProcParamList(StockProc stockProc);
}

Impl程式碼:

@Service
public class StockProcImpl implements IStockProcService {
    @Autowired
    private StockProcMapper stockProcMapper;
    /**
     *
     * @param 列表記錄
     * @return
     */
    @Override
    public List<StockProc> selectStockProcList(StockProc stockProc) {
        //return stockProcMapper.selectStockProcList(stockProc);
        return stockProcMapper.selectStockProcList(stockProc);
    }
    @Override
    public List<StockProc> selectStockProcParamList(StockProc stockProc) {
        return stockProcMapper.selectStockProcParamList(stockProc);
        //return null;
    }
}

mapper程式碼:

public interface StockProcMapper
{
    /**
     * 查詢列表
     * 
     * @param stockProc 記錄
     * @return 集合
     */
    public List<StockProc> selectStockProcList(StockProc stockProc);

    public List<StockProc> selectStockProcParamList(StockProc stockProc);
}

mybatis的xml檔案:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.stock.mapper.StockProcMapper">
    <resultMap type="StockProc" id="StockProcResult">
        <result property="Id"    column="id"/>
        <result property="a"    column="a"/>
        <result property="b"    column="b"/>
        <result property="c"    column="c"/>
        <result property="d"    column="d"/>
        <result property="e"    column="e"/>
        <result property="f"    column="f"/>
        <result property="g"    column="g"/>
        <result property="h"    column="h"/>
        <result property="abc"    column="abc"/>
        <result property="t"    column="t"/>
        <result property="tc"    column="tc"/>
       <result property="min1"    column=" min1"/>
    </resultMap>
    <sql id="selectStockProcVo">
        SELECT
           a,b,c,d,e,f,g,h,abc,t,tc,min1
        FROM
            a_tmp
    </sql>
<!--使用資料庫儲存過程查詢-->
    <select id="selectStockProcParamList" parameterType="StockProc" resultMap="StockProcResult" statementType="CALLABLE">
        call bill_a_forbusiness(#{a},#{b},#{c},#{d},#{e},#{f},#{g},#{h},#{abc})
    </select>
    <!--無引數查詢-->
    <select id="selectStockProcList" parameterType="StockProc" resultMap="StockProcResult">
        <include refid="selectStockProcVo"/>
    </select>
</mapper>

最後便可以通過頁面輸入框的引數進行呼叫儲存過程執行,然後點選查詢返回結果列表。

到此這篇關於前端傳引數進行Mybatis呼叫mysql儲存過程執行返回值詳解的文章就介紹到這了,更多相關Mybatis呼叫mysql內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com