mybatis 调用postgerSql 常见问题解决
spring boot+mybatis 调用pg存储过程基础
·
环境
mybatis :1.3.4-mybatis-spring-boot-starter
postgresql: 12.16
jdk:8+
mapper.java
@Mapper
public interface CustomMapper {
@Options(statementType = StatementType.CALLABLE)
@Select("CALL pg_schema.sync_area_code(#{out_result, mode=IN, jdbcType=VARCHAR})")
String syncAreaCode(@Param("out_result") String outResult);
}
此处@Param参数要和#{out_result, 一致
测试方法
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = DemoApplication.class)
public class TestCode {
@Autowired
private CustomMapper customMapper;
@Test
public void testMybatis() {
String outResult = "成功2333!";
customMapper.syncAreaCode(outResult);
System.out.println(outResult);
}
}
存储过程
-- DROP PROCEDURE sync_area_code(inout varchar);
CREATE OR REPLACE PROCEDURE sync_area_code(INOUT out_result character varying)
LANGUAGE plpgsql
AS $procedure$
begin
-- 业务逻辑
out_result := '成功!';
exception
when others then
rollback;
end;
$procedure$
;
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)