反射注解配合使用存储数据库

 调用

Executors.execute(new CarInfoToData(mapDetail,mapOther,bufferBrand));
package com.aa.gecco.common.exec;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Executors {

	private static ThreadPoolExecutor pool;
	static{
		int corePoolSize = 1;
		int maximumPoolSize = 64;
		long keepAliveTime = 1;
		TimeUnit unit = TimeUnit.MINUTES;
		new Executors(corePoolSize, maximumPoolSize, keepAliveTime, unit);
		}

	public Executors(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit) {
		BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
		RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();
		String prefix="";
		if(pool==null){
			ThreadFactory threadFactory = new MyThreadFactory(prefix);
			pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory,
					handler);
		}

	}
	public static void execute(Runnable action) {
		pool.execute(action);
	}
}

 

package com.aa.gecco.crawler.autohome.task;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.geccocrawler.gecco.annotation.ParamName;
import com.ktm.gecco.common.utils.SpringContextUtil;
import com.ktm.gecco.crawler.autohome.bean.VehiclesDetail;
import com.ktm.gecco.crawler.autohome.mapper.AutoHomeDataMapper;


public  class CarInfoToData implements Runnable{
	AutoHomeDataMapper autoHomeDataMapper=SpringContextUtil.getApplicationContext().getBean(AutoHomeDataMapper.class);
	private StringBuffer  buffer;
	private Map<String, Object> detailMap;
	private Map<String, Object> otherMap;
	public CarInfoToData(Map<String, Object>  detailMap,Map<String, Object>  otherMap,StringBuffer buffer){
		this.buffer=buffer;
		this.detailMap=detailMap;
		this.otherMap=otherMap;
	}
	public CarInfoToData(){
	}
	@Override
	public void  run() {
	    	 List<String> list=(List<String>) detailMap.get("positionInfo");
			 List<VehiclesDetail> autoHomeDataList=new ArrayList<VehiclesDetail>();
			 String[] buffers=buffer.toString().split("\t");
			 Map<String, String> map=(Map<String, String>) detailMap.get("saveDetailData");
			 Map<String, String> othermap=(Map<String, String>) otherMap.get("saveOtherData");
			 for (String string : list) {
				 VehiclesDetail detail =new VehiclesDetail();
				 detail.setBrandId(buffers[0]);
				 detail.setBrandName(buffers[1]);
				 detail.setCarFirmsId(buffers[2]);
				 detail.setCarFirmsName(buffers[3]);
				 detail.setAudiId(buffers[4]);
				 detail.setAudiName(buffers[5]);;
				 // 此处要用反射将字段中的注解解析出来
				 detail=getdata(map, detail, string);
				 detail=getdata(othermap, detail, string);
				 autoHomeDataList.add(detail);
					        	}
			 for (int i = 0; i < autoHomeDataList.size();) {
				 if(i==0){
					 if(autoHomeDataList.size()>1000){
				 autoHomeDataMapper.batchSaveAutoHome(autoHomeDataList.subList(i, 1000));
				 i=1000;
					 }else{
						 autoHomeDataMapper.batchSaveAutoHome(autoHomeDataList.subList(i, autoHomeDataList.size()));
						 i=autoHomeDataList.size();
					 }
				 }else if(autoHomeDataList.subList(i, autoHomeDataList.size()).size()<1000){
					 autoHomeDataMapper.batchSaveAutoHome(autoHomeDataList.subList(i, autoHomeDataList.size()));
					 i=autoHomeDataList.size();
				 }else{
					 autoHomeDataMapper.batchSaveAutoHome(autoHomeDataList.subList(i, i+1000));
					 i=i+1000;
				 }
			}
		 }

public VehiclesDetail getdata(Map<String , String> map,VehiclesDetail detail,String string){
	 Class<VehiclesDetail> clz = VehiclesDetail.class;
    // 获取属性上的注解
	Field[] allFields = clz.getDeclaredFields();// 得到所有定义字段;
	for (Field field : allFields) {
		if(field.isAnnotationPresent(ParamName.class)){
		ParamName fieldAnno = field.getAnnotation(ParamName.class);
		String name=fieldAnno.name().toString();
		String names=fieldAnno.names().toString();
		String fileName=field.getName();
		  try {
			  fileName = fileName.substring(0, 1).toUpperCase() + fileName.substring(1); // 将属性的首字符大写
					 String setMethodName = "set"+fileName;
						//得到get方法的Method对象,带参数
						Method setMethod = clz.getDeclaredMethod(setMethodName,field.getType());
						setMethod.setAccessible(true);
						if(!"".equals(names)){
//							System.out.println("hahahhahahahahha99"+map.get(names+"_"+string));
							if(null!=map.get(names+"_"+string)){
							setMethod.invoke(detail, map.get(names+"_"+string));
							}
						}else if(null!=map.get(name+"_"+string)){
								setMethod.invoke(detail, map.get(name+"_"+string));
//							System.out.println("hahahhahahahahha66"+map.get(name+"_"+string));
							}

		  }catch (Exception e) {
					e.printStackTrace();
				}

            }
	}
return detail;
}
}

 

package com.geccocrawler.gecco.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 表示该字段是一个链接类型的元素,jsoup会默认获取元素的href属性值。
 *
 * @author wangyanan
 *
 */
@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ParamName {

	/**
	 * 默认获取href属性值,可以多选,按顺序查找
	 *
	 * @return 属性名
	 */
	String name() default "";
	/**
	 * 默认获取href属性值,可以多选,按顺序查找
	 *
	 * @return 属性名
	 */
	String names() default "";


}

 

package com.aa.gecco.crawler.autohome.bean;
import com.geccocrawler.gecco.annotation.ParamName;

public class VehiclesDetail {

	@ParamName(name = "567")
	private String modelname; // 车型名称
	@ParamName(name = "219")
	private String manufacturergui; // 厂商指导价(元)
	@ParamName(name = "306",names="实测")
	private String measurementground; // 实测
	
	
	public String getModelname() {
		return modelname;
	}
	public void setModelname(String modelname) {
		this.modelname = modelname;
	}
	public String getManufacturergui() {
		return manufacturergui;
	}
	public void setManufacturergui(String manufacturergui) {
		this.manufacturergui = manufacturergui;
	}
	public String getMeasurementground() {
		return measurementground;
	}
	public void setMeasurementground(String measurementground) {
		this.measurementground = measurementground;
	}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值