JAVA通过MyBatis调用MySql存储过程和函数讲解.docx

上传人:b****8 文档编号:10659419 上传时间:2023-02-22 格式:DOCX 页数:19 大小:20.24KB
下载 相关 举报
JAVA通过MyBatis调用MySql存储过程和函数讲解.docx_第1页
第1页 / 共19页
JAVA通过MyBatis调用MySql存储过程和函数讲解.docx_第2页
第2页 / 共19页
JAVA通过MyBatis调用MySql存储过程和函数讲解.docx_第3页
第3页 / 共19页
JAVA通过MyBatis调用MySql存储过程和函数讲解.docx_第4页
第4页 / 共19页
JAVA通过MyBatis调用MySql存储过程和函数讲解.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

JAVA通过MyBatis调用MySql存储过程和函数讲解.docx

《JAVA通过MyBatis调用MySql存储过程和函数讲解.docx》由会员分享,可在线阅读,更多相关《JAVA通过MyBatis调用MySql存储过程和函数讲解.docx(19页珍藏版)》请在冰豆网上搜索。

JAVA通过MyBatis调用MySql存储过程和函数讲解.docx

JAVA通过MyBatis调用MySql存储过程和函数讲解

JAVA通过MyBatis调用MySql存储过程和函数

1.引言

无论是采用SPRINGMVC框架开发WEB画面,还是开发需按一定时间间隔执行的批处理,都可能要调用数据库的存储过程或函数。

其间调用参数设置不正会浪费大量调试时间初学者甚至放弃使用存储过程。

本文记录了通过MyBatis调用MySql存储过程和函数的具体参数设置内容,供参考。

2.MySql存储过程例

/*全公司员工下一年度带薪休假一发赋予处理*/

CREATEDEFINER=`DBuser`@`%`PROCEDURE`paid_vacation_compute`(

OUTp_返回值INT(11),

INOUTp_员工号CHAR(3),

p_操作者IDVARCHAR(3))

PROC_START:

BEGIN

/*变量声明*/

DECLAREdoneINT;#异常退出控制变量

DECLAREempNoCHAR(3);#员工号

DECLAREdateHiredate;#分公司就职日

DECLAREworkYearsINT;#集团内工作年数

DECLARElastYearRemainDaysFLOAT;#昨年残日数(允许以小时为单位休假)

DECLAREnowYearleaveDaysFLOAT;#今年休暇日数(允许以小时为单位休假)

DECLAREelapseYearINT;#入集团经过年度数

/*游标声明*/

#上年带薪休假数据

DECLAREstaffPaidVacationDaysCurCURSORFOR

SELECTa.EMP_NO,#员工号

a.DATE_HIRE,#入职日期

a.WORK_YEARS,#工作年限

b.REMAIN_DAYS#上年带薪休假应休但未休残日数

FROMT_EMPLOYEEASa,

T_PAID_VACATIONASb

WHEREa.EMP_NO=b.EMP_NO

/*程序退出规定声明*/

DECLARECONTINUEHANDLERFORNOTFOUNDSETSETdone=1;

DECLAREEXITHANDLERFORSQLWARNINGSETdone=2;

DECLARECONTINUEHANDLERFORSQLEXCEPTION

BEGIN

SETdone=3;

ROLLBACK;#SQL异常回滚

END;

/*1.输入参数妥当性检查*/

IF(p_操作者ISNULLORp_操作者='')THEN

SETp_返回值=9;

LEAVEPROC_START;

ENDIF;

SETdone=0;

STARTTRANSACTION;

loop_label:

LOOP

#读入一条记录,代入到各个变量中

FETCHstaffPaidVacationDaysCur

INTOempNo,#员工号

dateHire,#就职日

workYears,#集团内工作年数

lastYearRemainDays#昨年残日数

;

IFdone=1THEN

LEAVEloop_label;

ENDIF;

/*根据集团内工作年限+分公司就职年限计算下一年度带薪天数*/

SETelapseYear=YEAR(current_timestamp)-Year(dateHire)

+workYears;

IFelapseYear=0THEN

SETnowYearLeaveDays=10;

ELSEIFelapseYear=1THEN

SETnowYearLeaveDays=11;

ELSEIFelapseYear=2THEN

SETnowYearLeaveDays=12;

ELSEIFelapseYear=3THEN

SETnowYearLeaveDays=14;

ELSEIFelapseYear=4THEN

SETnowYearLeaveDays=16;

ELSEIFelapseYear=5THEN

SETnowYearLeaveDays=18;

ELSEIFelapseYear>=6THEN

SETnowYearLeaveDays=20;

ENDIF;

SETdone=0;

SETp_员工号=empNo;

UPDATET_PAID_VACATION

SET

LAST_YEAR_REMAIN_DAYS=lastYearRemainDays,THIS_YEAR_BASE_DAYS=nowYearLeaveDays,

UPDATE_DATETIME=current_timestamp,

UPDATE_USER_ID='SYS',UPDATE_TERMINAL_ID='MANUAL'

WHEREEMP_NO=CONVERT(empNoUSINGbinary);

IFdone=3THEN

SETp_返回值=6;

LEAVEPROC_START;

ENDIF;

ENDLOOP;

COMMIT;

END

3.MySql函数例

CREATEDEFINER=`DBuser`@`%`FUNCTION`paid_vacation_compute`(

p_员工号CHAR(3),

p_操作者IDVARCHAR(3))

)RETURNSint(11)

BEGIN

/*变量声明*/

DECLAREdoneINT;#异常退出控制变量

DECLAREempNoCHAR(3);#员工号

DECLAREdateHiredate;#分公司就职日

DECLAREworkYearsINT;#集团内工作年数

DECLARElastYearRemainDaysFLOAT;#昨年残日数(允许以小时为单位休假)

DECLAREnowYearleaveDaysFLOAT;#今年休暇日数(允许以小时为单位休假)

DECLAREelapseYearINT;#入集团经过年度数

/*游标声明*/

#上年带薪休假数据

DECLAREstaffPaidVacationDaysCurCURSORFOR

SELECTa.EMP_NO,#员工号

a.DATE_HIRE,#入职日期

a.WORK_YEARS,#工作年限

b.REMAIN_DAYS#上年带薪休假应休但未休残日数

FROMT_EMPLOYEEASa,

T_PAID_VACATIONASb

WHEREa.EMP_NO=b.EMP_NO

/*程序退出规定声明*/

DECLARECONTINUEHANDLERFORNOTFOUNDSETSETdone=1;

DECLAREEXITHANDLERFORSQLWARNINGSETdone=2;

DECLARECONTINUEHANDLERFORSQLEXCEPTION

BEGIN

SETdone=3;

ROLLBACK;#SQL异常回滚

END;

/*1.输入参数妥当性检查*/

IF(p_操作者ISNULLORp_操作者='')THEN

RETURN9;

ENDIF;

SETdone=0;

STARTTRANSACTION;

loop_label:

LOOP

#读入一条记录,代入到各个变量中

FETCHstaffPaidVacationDaysCur

INTOempNo,#员工号

dateHire,#就职日

workYears,#集团内工作年数

lastYearRemainDays#昨年残日数

;

IFdone=1THEN

LEAVEloop_label;

ENDIF;

/*根据集团内工作年限+分公司就职年限计算下一年度带薪天数*/

SETelapseYear=YEAR(current_timestamp)-Year(dateHire)

+workYears;

IFelapseYear=0THEN

SETnowYearLeaveDays=10;

ELSEIFelapseYear=1THEN

SETnowYearLeaveDays=11;

ELSEIFelapseYear=2THEN

SETnowYearLeaveDays=12;

ELSEIFelapseYear=3THEN

SETnowYearLeaveDays=14;

ELSEIFelapseYear=4THEN

SETnowYearLeaveDays=16;

ELSEIFelapseYear=5THEN

SETnowYearLeaveDays=18;

ELSEIFelapseYear>=6THEN

SETnowYearLeaveDays=20;

ENDIF;

SETdone=0;

SETp_员工号=empNo;

UPDATET_PAID_VACATION

SET

LAST_YEAR_REMAIN_DAYS=lastYearRemainDays,THIS_YEAR_BASE_DAYS=nowYearLeaveDays,

UPDATE_DATETIME=current_timestamp,

UPDATE_USER_ID='SYS',UPDATE_TERMINAL_ID='MANUAL'

WHEREEMP_NO=CONVERT(empNoUSINGbinary);

IFdone=3THEN

RETURN6;

ENDIF;

ENDLOOP;

COMMIT;

END

4.MySql存储过程调用时的iBatis用Mapper例

BaseInfoEditMapper.xml

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEmapper

PUBLIC"-//ibatis.apache.org//DTDMapper3.0//EN"

"http:

//ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

{callpaid_vacation_compute(

#{ReturnValue,javaType=INTEGER,jdbcType=INTEGER,mode=OUT},

#{StaffNumber,javaType=String,jdbcType=CHAR,mode=INOUT},

#{HireDate,javaType=String,jdbcType=VARCHAR,mode=IN},

#{OperateID,javaType=String,jdbcType=VARCHAR,mode=IN})}

 

5.MySql函数调用时的iBatis用Mapper例

BaseInfoEditMapper.xml

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEmapper

PUBLIC"-//ibatis.apache.org//DTDMapper3.0//EN"

"http:

//ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

{#{ReturnValue,javaType=INTEGER,jdbcType=INTEGER,mode=OUT}=

callpaid_vacation_compute(

#{StaffNumber,javaType=String,jdbcType=CHAR,mode=IN},

#{HireDate,javaType=String,jdbcType=VARCHAR,mode=IN},

#{OperateID,javaType=String,jdbcType=VARCHAR,mode=IN})}

6.JAVA调用例1(MySql存储过程和函数相同)

packagecom.XXX.impl;

importjava.util.ArrayList;

importjava.util.List;

importcom.XXX.web.bean.VacationCreateBean;

importcom.XXX.web.dao.BaseInfoEditDAO;

importcom.XXX.web.util.BasicSqlSupport;

 

publicclassBaseInfoEditDAOImplextendsBasicSqlSupportimplementsBaseInfoEditDAO{

publicbooleanaddBaseInfo(BaseInfoEditBeanobjUserInfo)throwsException{

booleanblnFlag=false;//成功FLAG

;

//全公司员工下一年度带薪休假一发赋予处理

VacationCreateBeanobjVacationCreateBean=newVacationCreateBean();

objVacationCreateBean.setStaffNumber(objUserInfo.getSTAFF_NUMBER());

objVacationCreateBean.setHireDate(objUserInfo.getDATE_HIRE().toString());

objVacationCreateBean.setOperateID(objUserInfo.getCREATE_USER_ID());

objVacationCreateBean.setDhcWorkYearsShinKi(objUserInfo.getDHC_WORK_YEARS());

StringreturnValue=(String)this.session.selectOne("com.XXX.web.mapper.base_info_edit.VacationProcedure",objVacationCreateBean);

//System.out.println("staffNumber="+objVacationCreateBean.getStaffNumber());

//System.out.println("result="+objVacationCreateBean.getReturnValue());

//System.out.println("returnValue="+returnValue);

//追加結果の判断

blnFlag=true;

returnblnFlag;

}

}

7.処理DAO接口

packagecom.XXX.web.dao;

importjava.util.List;

importcom.XXX.web.bean.BaseInfoEditBean;

/**

*员工基本信息画面の処理DAO

*/

publicinterfaceBaseInfoEditDAO{

publicListselectAuthoriyList()throwsException;

publicStringselectStaffId()throwsException;

publicintselectOpetateTimeNum(StringstrStaffNumber)throwsException;

publicbooleanaddBaseInfo(BaseInfoEditBeanobjUserInfo)throwsException;

publicbooleanupdateBaseInfo(BaseInfoEditBeanobjUserInfo)throwsException;

publicBaseInfoEditBeansearchBaseInfo(StringstrStaffNumber)throwsException;

}

8.共同処理

packagecom.XXX.web.util;

importorg.apache.ibatis.session.SqlSession;

publicclassBasicSqlSupport{

protectedSqlSessionsession;

publicSqlSessiongetSession(){

returnsession;

}

publicvoidsetSession(SqlSessionsession){

this.session=session;

}

}

 

9.DAO与MAP间的交互BEAN

packagecom.XXX.web.bean;

importjava.io.Serializable;

publicclassVacationCreateBeanimplementsSerializable{

privateintReturnValue;

privateStringStaffNumber;

privateStringHireDate;

privateStringOperateID;

privateintWorkYearsShinKi;

publicintgetReturnValue(){

returnReturnValue;

}

publicvoidsetReturnValue(intreturnValue){

ReturnValue=returnValue;

}

publicStringgetStaffNumber(){

returnStaffNumber;

}

publicvoidsetStaffNumber(StringstaffNumber){

StaffNumber=staffNumber;

}

publicStringgetHireDate(){

returnHireDate;

}

publicvoidsetHireDate(StringhireDate){

HireDate=hireDate;

}

publicStringgetOperateID(){

returnOperateID;

}

publicvoidsetOperateID(StringoperateID){

OperateID=operateID;

}

publicintgetDhcWorkYearsShinKi(){

returndhcWorkYearsShinKi;

}

publicvoidsetDhcWorkYearsShinKi(intdhcWorkYearsShinKi){

this.dhcWorkYearsShinKi=dhcWorkYearsShinKi;

}

}

 

10.通过MAIN函数进行调用

packagecom.ohc.pms.batch;

importjava.io.FileInputStream;

importjava.io.IOException;

importjava.sql.CallableStatement;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.SQLException;

importjava.util.Properties;

importorg.apache.log4j.Logger;

importorg.apache.log4j.PropertyConfigurator;

publicclassVacationCreate{

/**JDBC驱动名*/

staticStringjdbcDriver="com.mysql.jdbc.Driver";

/**DBURL*/

staticStringdbURL="jdbc:

mysql:

//172.999.999.35:

3306/empdb";

/**DB用户名*/

staticStringuser="empuser";

/**DB密码*/

staticStringpass="empuser123";

/**日志输出*/

staticprotectedLoggerlog=Logger.getLogger(VacationCreate.class);

publicstaticvoidmain(String[]args){

Connectionconn=null;

CallableStatementstmt=null;

try{

PropertyConfigurator.configure(System.getProperty("user.dir")+"\\"+"log4j.properties");

log.info("実行路径:

"+System.getProperty("use

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教学研究 > 教学反思汇报

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1