SSM中使用註解方式調用輸入輸出參數存儲過程,輸入參數爲javabean方式,Boolean類型對應TINYINT類型

最近在搗鼓SSM使用註解方式調用有輸入輸出參數的存儲過程,趟了不少坑,網上一堆沒用抄襲的,廢話不多說,直接上代碼。

Controller層:

/**
     * 註冊用戶
     *
     * @param userName
     * @param userPwd
     * @param userSex
     * @param userPhone
     * @return
     * @throws Exception
     */
    @RequestMapping("/regist")
    @ResponseBody
    public ExecuteResult regist(String userName, String userPwd, String userSex, String userPhone) throws Exception {
        ExecuteResult result = new ExecuteResult();
        ProcRegistParam para = new ProcRegistParam(false, null, userName, userPwd, userSex, userPhone);
        iUserService.regist(para);
        result.setResult(para.getResult());
        result.setMsg(para.getMsg());
        return result;
    }

DAO層:

Repository("iUser")
public interface IUser {

    /**
     * 註冊用戶
     *
     * @return
     * @throws Exception
     */
    @Select({"call Proc_Regist(#{para.userName,mode=IN,jdbcType=VARCHAR},",
            "#{para.userPwd,mode=IN,jdbcType=VARCHAR},",
            "#{para.userSex,mode=IN,jdbcType=VARCHAR},",
            "#{para.userPhone,mode=IN,jdbcType=VARCHAR},",
            "#{para.result,mode=OUT,jdbcType=BOOLEAN},",
            "#{para.msg,mode=OUT,jdbcType=VARCHAR});"})
    @Options(statementType = StatementType.CALLABLE)
    void regist(@Param("para") ProcRegistParam procRegistParam) throws Exception;
}

參數實體類:

package com.study.daomain;

import com.study.util.ExecuteResult;

import java.io.Serializable;

public class ProcRegistParam implements Serializable {

    public  ProcRegistParam()
    {

    }
    public ProcRegistParam(Boolean result, String msg, String userName, String userPwd, String userSex, String userPhone) {
        this.result = result;
        this.msg = msg;
        this.userName = userName;
        this.userPwd = userPwd;
        this.userSex = userSex;
        this.userPhone = userPhone;
    }
    private Boolean result;
    private String msg;
    private String userName; //用戶名
    private String userPwd;//用戶密碼
    private String userSex;//用戶性別
    private String userPhone;//用戶手機號

    public Boolean getResult() {
        return result;
    }

    public void setResult(Boolean result) {
        this.result = result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }
}

存儲過程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `Proc_Regist`(
	IN userName VARCHAR ( 50 ),-- 用戶名
	IN userPwd VARCHAR ( 50 ),-- 用戶密碼
	IN userSex VARCHAR ( 50 ),-- 用戶性別
	IN userPhone VARCHAR ( 20 ),-- 用戶手機號
  OUT result TINYINT(2),
	OUT msg VARCHAR ( 4000 ) 
	)
begin_label : BEGIN-- return標籤
	
		-- 定義參數
	DECLARE newUserId int DEFAULT 0; -- 新添加的用戶id
	
	-- 定義異常
	DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
	BEGIN
		 -- 異常信息
	   get diagnostics CONDITION 1 msg=message_text;
		 -- 異常標識
		 SET result=0;
	END;
	

	
	-- 開始事務
	START TRANSACTION;
	-- 初始化狀態
	SET result=1;
	SET msg='操作成功!';
	
	-- 添加用戶
	INSERT INTO s_user(su_name,su_pwd,su_sex,su_phone,create_user,create_time)
	VALUES(userName,userPwd,userSex,userPhone,userName,NOW());
	SET newUserId=(SELECT LAST_INSERT_ID());
	
	-- 添加用戶權限
	INSERT INTO s_user_power(su_id,sp_id,create_user,create_time) VALUES(newUserId,1,userName,NOW());
	INSERT INTO s_user_power(su_id,sp_id,create_user,create_time) VALUES(newUserId,2,userName,NOW());
	INSERT INTO s_user_power(su_id,sp_id,create_user,create_time) VALUES(newUserId,3,userName,NOW());
	
	if(result=0)
	THEN
  ROLLBACK;
	ELSE
	COMMIT;
	END IF;
	
	END

db.properties文件:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false
jdbc.username=root
jdbc.password=admin123

注意:

1.數據庫中存儲過程返回的TINYINT(2)時,要對應上JavaBean的Boolean類型的result屬性,

(1)db.properties文件的url後面加上 tinyInt1isBit=false 

(2)DAO層中的jdbcType爲Boolean類型

(3)參數實體類-ProcRegistParam中必須要存在一個無參數的構造函數(默認隱藏存在),如果有參數的構造函數,會報錯,這個其實在整個SSM項目都要注意!

(4)mode=IN 中的IN或者OUT必需都爲大寫,小寫會報錯


 

 

 

 

發佈了121 篇原創文章 · 獲贊 48 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章