我正在使用 SqlServer 和 MyBatis。
一直在网上寻找几个例子,但似乎没有一个能满足我的需要。 store_procedure Im 调用接受 5 个参数并返回 1 个值 - 例如
give_discount(名字、姓氏、性别、出生日期、年龄)
都是字符串。然后,stored_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");
这是我的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>
所以问题是:
如何将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}
)}
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 ..
**
**
最佳答案
我想我明白了。
您的过程从查询中返回 7 列,对吧?
我将使用下面的简单表格和过程进行解释。
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
该过程返回 3 列( id
、 name
和 note
)。
我将向您展示一种仅返回note
的方法专栏。
映射器方法如下所示(您可以使用 POJO 而不是 Map)。
List<Map<String, ?>> myProc(String arg1);
如果它总是返回一行,则可能是...
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>
返回的 map 将有一个带有键 "note"
的条目.
-
<id />
可能不是必需的,但提供它是一个很好的做法。 -
autoMapping="false"
是为了避免自动映射不必要的列。
一个可执行的演示项目:
https://github.com/harawata/mybatis-issues/tree/master/so-58802623
关于java - 在 MyBatis 中调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802623/