JAVA使用JCo连接SAP-3

我不知道其他项目用JAVA使用JCo连接SAP的应用场景是什么,我这边的应用场景是通过SAP返回的数据插入到自己的数据库中。

本人也是刚刚接触就要使用的情况下有些东西可能有更好的办法解决,但是我没发现或者不知道,有更优的办法,可以的话,提供给我,或者留言给我思路也可以。

在JAVA使用JCo连接SAP-2中,我们可以前端通过页面调用controller层调用DataBean.getParameters()方法即可获取到SAP的参数值。对于使用mybatis框架的我们来说,这些东西都需要实体类去接收。一个实体类假设100个字段,按照其他的博主的写法,好像所有的字段值都是写死的。我本人比较懒,如果字段少,我也就那么干了。

不过由于字段太多了,决定采用java反射赋值给实体类。不用写太多冗余的代码(主要是偷懒)!

首先我们写一个实体类:SctsLoadunitVO.java

package com.sap.vo;

import lombok.Data;

@Data
/**托盘档案*/
public class SctsLoadunitVO {
	/**SCTS_LOADUNIT 主键 LOAD_UNIT*/
	private String load_unit;//托盘编号
	private String lu_type;//托盘类型
	private String lu_typename;//托盘类型名称
	private String bin_matnr;//箱子材质
	private String bin_matnrname;//箱子材质名称
	private String lu_length;//托盘长度
	private String lu_width;//托盘宽度
	private String full_lu_height;//托盘最大高度
	private String folded_lu_h;//托盘折叠后的高度
	private String unit_dim;//长度/宽度/高度尺寸单位
	private String static_stack;//托盘静态堆垛最高层数
	private String dyn_stack;//托盘动态堆垛最高层数
	private String bin_code;//BIN编号
	private String bin_pl;//托盘每层放的箱子数
	private String layer_p_lu;//托盘可放箱子的最高层数
	private String bin_length;//BIN长度
	private String bin_width;//BIN宽度
	private String bin_height;//BIN高度
	private String bin_t_weight;//BIN包装重量
	private String bin_t_weight_unit;//包装重量单位
	private String comp_cover;//COMP_COVER
	private String qty_cover;//QTY_COVER
	private String tare_weight_cover;//TARE_WEIGHT_COVER
	private String comp1;//COMP1
	private String qty1;//QTY1
	private String tare_weight1;//TARE_WEIGHT1
	private String tare_weight1_unit;//TARE_WEIGHT1_UNIT
	private String comp2;//COMP2
	private String qty2;//QTY2
	private String tare_weight2;//TARE_WEIGHT2
	private String tare_weight2_unit;//TARE_WEIGHT2_UNIT
	private String comp3;//COMP3
	private String qty3;//QTY3
	private String tare_weight3;//TARE_WEIGHT3
	private String tare_weight3_unit;//TARE_WEIGHT3_UNIT
	private String comp_base;//COMP_BASE
	private String qty_base;//QTY_BASE
	private String tare_weight_base;//TARE_WEIGHT_BASE
	private String tare_weight_base_unit;//TARE_WEIGHT_BASE_UNIT
	private String weight_unit;//WEIGHT_UNIT
	private String tare_weight_cover_unit;//TARE_WEIGHT_COVER_UNIT
	//private String is_top_empty_bin;//是否顶置空BIN
	private String create_date;//CREATE_DATE
	private String create_time;//CREATE_TIME
	private String create_user;//CREATE_USER
	private String change_date;//CHANGE_DATE
	private String change_time;//CHANGE_TIME
	private String change_user;//CHANGE_USER
	private String source_from;//来源于 SAP
	private String scts_create_by;
	private String scts_create_date;
	private String scts_update_by;
	private String scts_update_date;
}

然后写一个反射的工具类Property.java

package utils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.sap.vo.SctsSupplierVO;

import java.lang.reflect.Field;

@SuppressWarnings({"rawtypes","unused"})
public class Property {
	/* 该方法用于传入某实例对象以及对象方法名,通过反射调用该对象的某个get方法 */
	public static void getProperty(Object beanObj){
		try {
			Field[] fields = beanObj.getClass().getDeclaredFields();//获得属性
			Class clazz = beanObj.getClass();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				// 此处应该判断beanObj,property不为null
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
				Method getMethod = pd.getReadMethod();
				if (getMethod != null) {
					System.out.println(beanObj+"的字段是:"+field.getName()+",类型是:"+field.getType()+",取到的值是: "+getMethod.invoke(beanObj)); 
				}
			}
		} catch (IllegalAccessException e) {
			 e.printStackTrace();
		} catch (IllegalArgumentException e) {
			 e.printStackTrace();
		} catch (InvocationTargetException e) {
			 e.printStackTrace();
		} catch (IntrospectionException e) {
			 e.printStackTrace();
		}
	}

	/* 该方法用于传入某实例对象以及对象方法名、修改值,通过放射调用该对象的某个set方法设置修改值 */
	public static void setProperty(Object beanObj,Object value){
		try {
			Field[] fields = beanObj.getClass().getDeclaredFields();//获得属性
			Class clazz = beanObj.getClass();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				// 此处应该判断beanObj,property不为null
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
				Method setMethod = pd.getWriteMethod();
				if (setMethod != null) {
					System.out.println(beanObj+"的字段是:"+field.getName()+",参数类型是:"+field.getType()+",set的值是: "+value); 
					//这里注意实体类中set方法中的参数类型,如果不是String类型则进行相对应的转换
					setMethod.invoke(beanObj, value);//invoke是执行set方法
				}
			}
		} catch (SecurityException e) {
			 e.printStackTrace();
		} catch (IllegalAccessException e) {
			 e.printStackTrace();
		} catch (IllegalArgumentException e) {
			 e.printStackTrace();
		} catch (InvocationTargetException e) {
			 e.printStackTrace();
		} catch (IntrospectionException e) {
			 e.printStackTrace();
		}
	}
	
	/**
	 * 该方法用于传入某实例对象以及对象方法名、修改值,和要修改值得字段。通过放射调用该对象的某个set方法设置修改值 
	 * @param beanObj 实体类VO
	 * @param value 要修改的值
	 * @param name 要修改值得字段
	 */
	public static void setProperty(Object beanObj, Object value, Object name){
		try {
			Field[] fields = beanObj.getClass().getDeclaredFields();//获得属性
			Class clazz = beanObj.getClass();
				Field field = fields[0];
				// 此处应该判断beanObj,property不为null
				PropertyDescriptor pd = new PropertyDescriptor((String)name, beanObj.getClass());
				Method setMethod = pd.getWriteMethod();
				if (setMethod != null) {
					//System.out.println(beanObj+"的字段是:"+(String)name+",参数类型是:"+field.getType()+",set的值是: "+value); 
					//这里注意实体类中set方法中的参数类型,如果不是String类型则进行相对应的转换
					setMethod.invoke(beanObj, value);//invoke是执行set方法
				}
		} catch (SecurityException e) {
			 e.printStackTrace();
		} catch (IllegalAccessException e) {
			 e.printStackTrace();
		} catch (IllegalArgumentException e) {
			 e.printStackTrace();
		} catch (InvocationTargetException e) {
			 e.printStackTrace();
		} catch (IntrospectionException e) {
			 e.printStackTrace();
		}
	}
	
	/***
	 * 通过反射获取实体类对象
	 * @param vo new一个实体类
	 * @return List<String> getFieldName 返回实体类对象名称
	 */
	public static List<String> getFieldName(Object vo){
		Field[] field = vo.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组
		List<String> list = new ArrayList<>();
		for(int j=0 ; j<field.length ; j++){ //遍历所有属性
			String name = field[j].getName(); //获取属性的名字
			list.add(name);
		}
		for (String name : list) {
			System.out.println(name);
		}
		return list;
	}
	
	/***
	 * @param VO 实体类
	 * @param voName 字段
	 */
	public static void setVoValue(Object VO,String voName){
		Property.setProperty(VO, "",voName);
	}
	
    /**
     * 功能:获取UUID并去除“-”
     */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("\\-", "");
    }
	
}

在昨天DataBean.java的基础上接收参数,赋值:

	/***
	 * @Description 获取sap接口的SctsLoadunit表数据
	 * @param functionName 程序名
	 * @param tableName 输出参数
	 * @return List<SctsLoadunitVO>
	 */
	public List<SctsLoadunitVO> getSctsLoadunitBySap(String functionName, String tableName){
		List<SctsLoadunitVO> sctsLoadunitList = new ArrayList<>();
		JCO.Client client = JCO.getClient(POOL_NAME);
		try {
			JCO.Function function = getFunction(functionName);
			JCO.ParameterList listParams = function.getImportParameterList();
			client.execute( function );

			JCO.Table tab = function.getTableParameterList().getTable(tableName);

			for(int i = 0 ; i < tab.getNumRows() ;i ++) {
				SctsLoadunitVO sctsLoadunitVO = new SctsLoadunitVO();
				tab.setRow(i);
				for(int j = 0 ; j < tab.getNumColumns() ; j ++) {
					String s = "" ;
					String voName = tab.getName(j).toLowerCase();
					//判断tab.getField(j).getValue()
					if(tab.getField(j).getValue()!=null) {
						if(tab.getField(j).getValue().getClass().getName().equals("java.util.Date")) {
							if(voName.equals("create_date")||voName.equals("change_date")) {
								s = new SimpleDateFormat("yyyyMMdd").format(tab.getField(j).getValue());
							}else if(voName.equals("create_time")||voName.equals("change_time")) {
								s = new SimpleDateFormat("HHmmss").format(tab.getField(j).getValue());
							}else {
								//是否为Date格式,是则转换
								s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tab.getField(j).getValue());
							}
						}else {
							s = tab.getField(j).getValue().toString().trim();
						}
					}
					Property.setProperty(sctsLoadunitVO, s,voName);
				}
				sctsLoadunitList.add(sctsLoadunitVO);
			}
		}
		catch ( JCO.AbapException ex ) {
			if ( ex.getKey().equalsIgnoreCase( "ERROR" ) ) {
				System.out.println( "-----------There is no record in the table ZQC_QA_INTERFACE.-----------" );
			}
			else {
				System.out.println( ex.getMessage() );
			}
		}
		catch ( Exception ex ) {
			System.out.println( "Caught an exception: \n" + ex );
		}
		finally {
			JCO.releaseClient( client );
		}

		return sctsLoadunitList;
	}

如果传送过来的是日期格式的,我就转换一下需要的格式样式。

然后用Property.setProperty(sctsLoadunitVO, s,voName);赋值给实体类。

后面就是利用这个实体类操作数据库了。

猜你喜欢

转载自blog.csdn.net/qq_36026747/article/details/81318318