在 MyBatis 中调用存储过程

noo*_*gui 0 java xml sql ibatis mybatis

我使用 SqlServer 和 MyBatis。

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

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

所有这些都是字符串。storage_procedure 然后返回一个 STRING,它应该告诉你你有权享受多少折扣。

这五个属性(FirstName、LastName、DateOfBirth、Age、Gender)存储在名为 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");
Run Code Online (Sandbox Code Playgroud)

这是我的 mapperXML

 <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>  
Run Code Online (Sandbox Code Playgroud)

所以问题是:

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

这在 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}
   )}
Run Code Online (Sandbox Code Playgroud)

而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;           
Run Code Online (Sandbox Code Playgroud)

}

mapperInterface.java
public interface UpstreamXmlMapper {
     public String callStoredProcedure (StoredProcRef storedProcRef);
}
Run Code Online (Sandbox Code Playgroud)

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

更新:

SqlServer 中的存储过程变量:

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

**

**

ave*_*ave 5

我想我明白了。
您的过程从查询中返回 7 列,对吗?

我将使用下面的简单表格和过程进行解释。

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

该过程返回 3 列(idnamenote)。
我将向您展示一种仅返回note列的方法。

mapper 方法如下所示(您可以使用 POJO 代替 Map)。

List<Map<String, ?>> myProc(String arg1);
Run Code Online (Sandbox Code Playgroud)

如果它总是返回一行,它可以是......

Map<String, ?> myProc(String arg1);
Run Code Online (Sandbox Code Playgroud)

这是结果图和语句:

<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>
Run Code Online (Sandbox Code Playgroud)

返回的地图将有一个带键的条目"note"

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

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