使用可选参数调用存储过程

时间:2017-02-28 09:01:12

标签: java mysql spring-mvc jdbc

基本上我有一个存储过程接受8个参数,并且基于所有这些参数只是它正在执行查询并给我一个输出,但现在我有一种情况,我可以在任何地方传递单个值或多个值在1到8个参数之间,它应该完美地工作,它应该根据已经传递的参数返回一些值。 我的StoredProcedure在MySQL中,我在Java代码中通过CallableStatement调用它。 下面是我的Storedprocedure的片段。

DELIMITER $$


CREATE PROCEDURE get_details(IN cus_name varchar(255),u_id int, ent_type varchar(255)
,branchId varchar(255), from_date datetime,to_date datetime, val varchar(255),rep_status varchar(255)
)
BEGIN
SELECT
r.id,r.parent_request_id,r.customer_master_id,
r.user_master_id,r.request_status,

cm.customer_name,

u.unique_id,

c.branch_id,c.created_date,c.entity_name,
c.entity_status,c.entity_type,c.no_of_documents,
c.no_of_parties,c.request_id,c.validity,c.itr_validity

FROM request as r
left join user_master as u on r.user_master_id = u.id
left join customer_master as cm  on u.customer_master_id = cm.id
left join customer_entity as c  on r.id= c.request_id

where 
customer_name = cus_name and 
unique_id = u_id and
entity_type = ent_type and
branch_id = branchId and
created_date >= from_date and
created_date <= to_date and
validity = val and 
request_status = rep_status and
entity_status='parent';

END$$
DELIMITER ;

以下是我在Java中调用此StoredProcedure的代码

String query = "{ call get_details(?,?,?,?,?,?,?,?)}";
            ResultSet resultSet = null;
            List<SuperAdmin> admins = new ArrayList<SuperAdmin>();
            try(
                    Connection connection = getconnection();
                    CallableStatement callableStatement = connection.prepareCall(query)){
                if(ReportDTO.getCustomerName() != null){
                callableStatement.setString(1, ReportDTO.getCustomerName());
                }
                if(ReportDTO.getUniqueId() != null){
                callableStatement.setString(2, ReportDTO.getUniqueId());
                }
                if(ReportDTO.getEntity_type() != null){
                callableStatement.setString(3, ReportDTO.getEntity_type());
                }
                if(ReportDTO.getBranchId() != null){
                callableStatement.setString(4, ReportDTO.getBranchId().toString());
                }
                if(ReportDTO.getFilter_from() != null){
                    Date newFilterDate_from =  dateFormat.parse(ReportDTO.getFilter_from().toString());
                    String newFormattedDate = dateFormat2.format(newFilterDate_from);
                    callableStatement.setString(5, newFormattedDate);
                }
                if(ReportDTO.getFilter_to() != null){
                    Date newFilterDate_to = (Date) dateFormat.parse(ReportDTO.getFilter_to().toString());
                    String newFormattedDateTo = dateFormat2.format(newFilterDate_to);
                    callableStatement.setString(6,newFormattedDateTo);
                }
                if(ReportDTO.getValidity() != null){
                callableStatement.setString(7, ReportDTO.getValidity());
                }
                if(ReportDTO.getReport_status() != null){
                callableStatement.setString(8, ReportDTO.getReport_status());
                }
                resultSet = callableStatement.executeQuery();
                while(resultSet.next()){
                    SuperAdmin sAdmin = new SuperAdmin();
                    sAdmin.setRequest_status(resultSet.getString("request_status"));
                    sAdmin.setCustomer_entity_type(resultSet.getString("entity_type"));
                    sAdmin.setCustomer_entity_name(resultSet.getString("entity_name"));
                    sAdmin.setRequest_id(resultSet.getInt("id"));
                    sAdmin.setCustomer_entity_created_date(resultSet.getDate("created_date"));
                    sAdmin.setCustomer_entity_status(resultSet.getString("entity_status"));
                    sAdmin.setCustomer_entity_no_of_documents(resultSet.getString("no_of_documents"));
                    sAdmin.setCustomer_entity_no_of_parties(resultSet.getString("no_of_parties"));
                    sAdmin.setCustomer_entity_validity(resultSet.getString("validity"));
                    sAdmin.setCustomer_entity_itr_validity(resultSet.getString("itr_validity"));
                    sAdmin.setCustomer_entity_branch_id(resultSet.getLong("branch_id"));
                    admins.add(sAdmin);
                }

            }

任何帮助都会非常值得赞赏。谢谢你。

1 个答案:

答案 0 :(得分:0)

对于存储过程中的可选参数,您可以这样写

CREATE PROCEDURE get_details(IN cus_name varchar(255)='',u_id int=0, ent_type varchar(255)='',
branchId varchar(255)='', from_date datetime=null,to_date datetime=null, val    varchar(255)='',rep_status varchar(255)=''
 )
BEGIN
you query syntex