java generics related notes for update

1, a generic method, when using a large number of repeat method, the code amount can be simplified packaging.
The following code, instead of returning type, but the format of the generic method, void return type is, of course, can be used as the return type T

@SuppressWarnings("static-access")
private <T> void insert(String tableName, int syncCount, List<TFinanceRoutingServer> serverList, int type,
		List<T> list) {
	JSONObject json = new JSONObject();
	int serverCount = serverList.size();
	DatabaseContextHolder.setDatabaseType(DatabaseType.dataSource2);
	List<String> insertId = new ArrayList<>();
	List<TFinanceSplitInfo> tList = new ArrayList<>();
	for (TFinanceRoutingServer server : serverList) {
		for (T tap : list) {
			saveSplit(server, type, json, json.toJSONString(tap), tList);
		}
	}
	insertSplit(tList, serverCount);
	int deleteCount = 0;
	try {
		DatabaseContextHolder.setDatabaseType(DatabaseType.dataSource1);
		deleteCount = syncPullService.deleteResult(list, tableName, type, syncCount);
	} catch (Exception e) {
		for (TFinanceSplitInfo s : tList) {
			insertId.add(String.valueOf(s.getId()));
		}
		roback(insertId);
		throw new RuntimeException("删除条数不正确,回滚数据!");
	}
	updateSplitStatus(tList, deleteCount);
}

2, generic interface.
The following code, the code package down clear, flexible, for individuals also facilitates control writing code, class objects defining a plurality of repeating (i.e., business case) inherit an abstract class AbsBatchReport, the abstract class interface to again achieve BachReportService injected defined interface BatchReportMapper, at all levels of the code is very clear division of tasks.

当确定一个业务时,最好是先定义一个接口,此处顺序说
1、在controller层注入相应bachReportService
@Autowired
BachReportService<TFinanceStatProjectFee> projectFeeService;

2、定义service
public interface BachReportService<T> {
	List<T> findByCondition(T t);
}
3、service实现时继承一个抽象类,不直接去实现一个接口是,是目前推断以后可能只实现接口部分方法,让抽象类去实现比较好。
注入BatchReportMapper持久层
@Service
public class TFinanceStatPaymentModeImpl extends AbsBatchReport<PaymentMode> {
	@Autowired
	BatchReportMapper<PaymentMode> batchReportMapper;
	@Override
	public List<PaymentMode> findByCondition(PaymentMode t) {
		return batchReportMapper.findByCondition(t.getProjectId(), t.getTjTime(), null);
	}
}
public abstract class AbsBatchReport<T> implements BachReportService<T>{}

4、定义持久层的接口
public interface BatchReportMapper<T> {
	List<T> findByCondition(String projectId, String statDate, String execTime);	
}
5、各持久层接口去继承统一的接口,便于管控。
public interface TFinanceStatPaymentModeMapper extends BatchReportMapper<TFinanceStatPayMode> {
	@Override
	@Select("SELECT ID_ FROM T_FINANCE_STAT_PAYMODE WHERE PROJECT_ID_ = #{projectId} AND STAT_DATE_ = #{statDate}")
	@Results({ @Result(property = "id", column = "ID_") })
	List<TFinanceStatPayMode> findByCondition(@Param("projectId") String projectId,
			@Param("statDate") String statDate, String execTime);
}

Published 10 original articles · won praise 0 · Views 350

Guess you like

Origin blog.csdn.net/weixin_43137113/article/details/104813636