Custom annotation strategy + mode, writing to different tables!

Case:

1, the use of open-source database synchronization tool Ali -canal to resolve the different database tables binlog log, parsed the data, we have to put in storage different databases, different tables.
2, each table corresponding to a Mapper class, insert different tables, we need to select a different Mapper to perform the same function: insertSelective
. 3, the ordinary mode is completed, we need to adopt "if" condition is determined depending on the table, select different Mapper, compare this code is redundant.

How to use custom annotations to achieve?

1, define an interface: IProcessor

package com.jane.binlog.dao;

public interface IProcessor<T> {
    int insertSelective(T record);

    int updateSelective(T record);
}

2, the definition of an annotation: ProcessorMapper

package com.jane.binlog.dao;

import java.lang.annotation.*;

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ProcessorMapper {

    String value() default "";
}

3, each Mapper increase annotation: @ProcessorMapper

1, annotated value is the name of the table.
2, Mapper implement the interface IProcessor, the operation target of the incoming type

import java.util.Date;

@Repository
@ProcessorMapper("pos_sale")
public interface PosSaleMapper extends IProcessor<PosSale> {
   int insert(PosSale record);
}

4, call the file

package com.jane.binlog.service;

import com.alibaba.fastjson.JSONObject;
import com.jane.binlog.dao.IProcessor;
import com.jane.binlog.dao.ProcessorMapper;
import com.jane.binlog.entity.PosSale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Service
public class PosSaleService {

    private static Logger LOG = LoggerFactory.getLogger(PosSaleService.class);

    public static Map<String, Class> map = new HashMap<>();

    //定义表和对象的关系
    static {
        map.put("pos_sale", PosSale.class);
    }

    private Map<String, IProcessor> mapperMap = new HashMap<String, IProcessor>();

    @Autowired
    private ApplicationContext applicationContext;

    //利用spring上下文,注入带有自定义注解的所有mapper到一个hashmap中。
    @PostConstruct
    public void init() {
        String[] classNames = applicationContext.getBeanNamesForAnnotation(ProcessorMapper.class);
        for (String name: classNames) {
            Class<?> type = applicationContext.getType(name);
            boolean posSaleMapper = type.isAnnotationPresent(ProcessorMapper.class);
            if (posSaleMapper) {
                String value = type.getAnnotation(ProcessorMapper.class).value();
                mapperMap.put(value, (IProcessor) applicationContext.getBean(name));
            }
        }
    }

    /**
     * 数据写入操作
     * @param table
     * @param data
     * @param op
     */
    public void binlogInsert(String table, Map<String, Object> data, String op) {
        try {
                    //获取表对应的对象类
            Class clazz = map.get(table);
                        //组装对象数据
            Object obj = this.assumePosSale(data, clazz);
                        //实现写操作
            IProcessor processor = mapperMap.get(table);
            processor.insertSelective(obj);
        } catch(Exception e) {
            throw e;
        }
    }

    /**
     * 把数据map转化为对应表对象
     * @param data
     * @param clazz
     * @return
     */
    private Object assumePosSale(Map<String, Object> data, Class clazz) {
        return JSONObject.parseObject(JSONObject.toJSONString(data), clazz);
    }

}

Guess you like

Origin blog.51cto.com/janephp/2440586