在 MyBatis 中调用存储过程

Call a Stored Procedure in MyBatis

我正在使用 SqlServer 和 MyBatis。

一直在网上寻找几个例子,但似乎没有一个适合我的需要。 stored_procedure Im 调用接受 5 个参数和 returns 1 个值-例如

give_discount(名字、姓氏、性别、出生日期、年龄)

都是字符串。 stored_procedure 然后 return 是一个字符串,它应该告诉您您有权享受多少折扣。

这五个属性(名字、姓氏、出生日期、年龄、性别)存储在名为 Person.java

的 POJO 中

假设我创建了一个 Person.java

的实例
Person person1 = new Person();
person1.setFirstName("Joe");
person1.setLastName("Higashi");
person1.setGender("M");
person1.setDateOfBirth("1990-01-01");
person1.setAge("29");

这是我的映射器XML

 <resultMap id = "discountValueParams"  type="com.test.Person">
      <result property = "FirstName"    column = "FirstName"/>
      <result property = "LastName"     column = "LastName"/>
      <result property = "Gender"       column = "Gender"/>
      <result property = "DateOfBirth"  column = "DateOfBirth"/>
      <result property = "Age"          column = "Age"/>
 </resultMap>  

所以问题是:

如何将 person1 传递给我的 stored_procedure 以便它使用其当前分配的值和 return 一个值?

这在 XML 映射器中是否正确?

<select id = "getDiscountValue" resultType = "String" parameterType = "com.test.Person" statementType = "CALLABLE">
  {call dbo.get_discount_value(
       #{FirstName,  jdbcType = NVARCHAR},
       #{LastName,  jdbcType = NVARCHAR},
       #{Gender,  jdbcType = NVARCHAR},
       #{DateOfBirth,  jdbcType = NVARCHAR},
       #{"Age",  jdbcType = NVARCHAR}
   )}

而java方法是:

public String getDiscountMethod( Person person) {

            String discountValue= "";

            Map<String, Object> map = new HashMap<>();
            map.put("FirstName", person.getFirstName  );
            map.put("LastName", person.getLastName());
            map.put("Gender", person.getGender());
            map.put("DateOfBirth", person.getBday());
            map.put("Age", person.getAge());

            SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
            try {
                XmlMapper xmlmapper = session.getMapper(XmlMapper.class);
                discountValue= applicationMapper.getDiscountValue(map) ;
                session.commit();
            } catch (Exception e) {
                LOGGER.error(e);
            } finally {
                session.close();
            }
            return discountValue;           

}

mapperInterface.java
public interface UpstreamXmlMapper {
     public String callStoredProcedure (StoredProcRef storedProcRef);
}

鉴于这些线路,我该如何进行正确的调用?我知道我缺乏并且显然可能错误地实施了一些东西。 需要适当的指导。

已更新:

SqlServer 中的存储过程变量:

ALTER PROCEDURE [dbo].[get_discount_for_customer]
    @formCode nvarchar(50),
    @locationCode nvarchar(10),
    @gender nvarchar(1),
    @dateOfBirth date,
    @labResultFormId bigint
AS
..SP body ..

**

**

我想我明白了。
您的程序 return 有 7 列来自一个查询,对吗?

我将使用以下简单的 table 和一个过程进行解释。

create table users (
  id int,
  name varchar(20),
  note varchar(20)
);
create procedure MY_PROC 
  @ARG1 varchar(10)
as
begin
  select * from users where name = @ARG1;
end

程序 returns 3 列(idnamenote)。
我将向您展示 return 仅 note 列的方法。

映射器方法如下所示(您可以使用 POJO 而不是 Map)。

List<Map<String, ?>> myProc(String arg1);

如果它总是return一行,它可以是...

Map<String, ?> myProc(String arg1);

下面是结果图和语句:

<resultMap type="map" id="userRM" autoMapping="false">
  <id column="id" />
  <result column="note" property="note" />
</resultMap>

<select id="myProc" statementType="CALLABLE" resultMap="userRM">
  {call MY_PROC(#{arg1, mode=IN, jdbcType=VARCHAR})}
</select>

returned 地图将有一个带有键 "note" 的条目。

  • <id /> 可能不是必需的,但最好提供它。
  • autoMapping="false"是为了避免自动映射不必要的列。

一个executable演示项目:
https://github.com/harawata/mybatis-issues/tree/master/so-58802623