1.先配置
1.下载sapjco3.dll插件置放在JDK或Tomcat的bin文件夹下
2.在项目中,新建一个jar目录;把sapjco3.jar和sapjco.jar复制进去,并添加上依赖
3.pom文件配置:
<!--添加的本地依赖-->
<dependency>
<groupId>com.sapjco3</groupId>
<artifactId>sapjco3-lib</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/sapjco3.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sapjco</groupId>
<artifactId>sapjco-lib</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/sapjco.jar</systemPath>
</dependency>
4.开始连接:
import com.alibaba.fastjson.JSON;
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @description: sap连接
* @author: Administrator
* @date: 2019-02-21 13:18
*/
@Component
public class MySapUtils {
public static Log log = LogFactory.getLog(MySapUtils.class);
//JCO配置名称;这里是生成的配置信息,一个配置信息对应一个文件,名称也就变了;我一开始用ABAP_WITHOUT_POOL不行 ,后来用 //ABAP5_WITHOUT_POOL这个名字就好了
static String ABAP_T = "ABAP5_WITHOUT_POOL";
public static JCoTable getTableList(String functionStr, String tablename, Map<String, Object> param){
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,
"127.0.0.1");//IP
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");系统编号
connectProperties
.setProperty(DestinationDataProvider.JCO_CLIENT, "10");客户端编号
connectProperties.setProperty(DestinationDataProvider.JCO_USER,
"username");//用户名
// 注:密码是区分大小写的,要注意大小写
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,
"password");//密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh");//语言
File cfg = new File(ABAP_T + "." + "jcoDestination");
System.err.println(cfg.getPath());
if (!cfg.exists()) {
try {
FileOutputStream fos = new FileOutputStream(cfg, false);
connectProperties.store(fos, "for tests only !");
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
JCoFunction function = null;
JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(ABAP_T);
function = destination.getRepository().getFunction(functionStr);
JCoParameterList input = function.getImportParameterList();
// 遍历map中的键
for (String key : param.keySet()) {
input.setValue(key,param.get(key));
}
function.execute(destination);
JCoTable tb = function.getTableParameterList().getTable(tablename);
return tb;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
//请求sap,发送消息
getMessage();
}
public static List<Message> getMessage(){
List<Message> messageList=new ArrayList<>();
Map<String, Object> param = new HashMap<>();
//Z_WX_GET_PO_RCAB是sap提供的方法名,T_ABC是sap那边提供的表名
JCoTable tb = getTableList("Z_WX_GET_PO_RCAB", "T_ABC", param);
System.err.println(tb);
}
}
5.测试返回的信息:
6.项目打包jar放到服务器上
我的是windowsserver
6.1先把sapjco3.dll文件复制到服务器的jdk的bin目录下,pom文件中:
然后打包jar放到服务器运行就可以了,不过打包war好像有问题(我没测试)
问题:在服务器运行报配置文件不存在:
复制本地已有的文件:
到服务器的jar包同级目录下: