JAVA实现SAP接口
环境spring-boot+maven
1.maven依赖
<dependency><groupId>com.github.virtualcry</groupId><artifactId>sapjco-spring-boot-starter</artifactId><version>3.1.4</version></dependency>
2.配置文件 application.yml
jco:clients:main-client:# 服务端地址ashost: 127.0.0.1# 系统编号sysnr: 00# 系统环境client: 800# 用户名user: userName# 密码password: userPwd# 最大连接数pool-capacity: 20# 最大连接线程数peak-limit: 50# 语言language: ZH
3.在项目resources目录下创建lib文件夹并放置如下文件
4.测试方法(以下代码放main方法里执行一样)
@Testpublic void testSap(){//获取连接工厂JCoConnectionFactory connectionFactory = JCoConnectionFactoryProvider.getSingleton().getIfAvailable();//获取连接client (main-client(同名yml配置文件中配置名))JCoClient client = connectionFactory.getClient("main-client");try {// 1.获取调用 RFC 函数对象(FUNCTION_NAME:方法名)JCoFunction func = client.getFunction("FUNCTION_NAME");// 2.1 配置传入普通参数JCoParameterList importParameterList = func.getImportParameterList();importParameterList.setValue("NAME", "XIAOMING");importParameterList.setValue("AGE", "18");// 2.2 设置传入内表参数(IN_MATNR:内表名称)JCoTable inTable = func.getTableParameterList().getTable("IN_MATNR");inTable.appendRow();inTable.setValue("USERNO","001");inTable.setValue("USERNAME","张三");inTable.setValue("USERAGE",18);inTable.appendRow();inTable.setValue("USERNO","002");inTable.setValue("USERNAME","李四");inTable.setValue("USERAGE",20);// 3.调用并获取返回值func.execute(client.getDestination());// 4.1 获取普通参数JCoParameterList exportParameterList = func.getExportParameterList();//返回代码、返回消息String returncode = exportParameterList.getString("RETURNCODE");String returnms = exportParameterList.getString("RETURNMS");System.out.println("获取返回普通参数:");System.out.println(returncode);System.out.println(returnms);// 4.2获取 返回内表 (OUTDATA:返回表名称)JCoTable maraTable = func.getTableParameterList().getTable("OUTDATA");// 循环输出 Table 数据for (int i = 0; i < maraTable.getNumRows(); i++) {System.out.println("获取返回内表数据:");maraTable.setRow(i);String code = maraTable.getString("CODE");String msg = maraTable.getString("MSG");System.out.println("获取内表code:"+code);System.out.println("获取内表msg:"+msg);}} catch (Exception e) {e.printStackTrace();}}
以上方法名、参数、内表参数、返回参数、返回内表参数
皆为甲方文档中定义,我们只需要传值和取值即可
5.正式环境运行时需要再jdk中放置以下文件