public interface JpaRepository<T.ID> extends PagingAndSortingRepository<T.ID>, QueryByExampleExecutor<T> {
List<T> findAll(a);
List<T> findAll(Sort sort);
List<T> findAllById(Iterable<ID> ids);
<S extends T> List<S> saveAll(Iterable<S> entities);
void flush(a);
<S extends T> S saveAndFlush(S entity);
void deleteInBatch(Iterable<T> entities);
void deleteAllInBatch(a);
T getOne(ID id);
@Override
<S extends T> List<S> findAll(Example<S> example);
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
}
Copy the code
// Pass in the field names that require group by and sum
public cacheMap(List<String> groupByKeys, List<String> sumValues) {
this.groupByKeys = groupByKeys;
this.sumValues = sumValues;
}
private void excute(T e) {
// Fetch the list of fields that require group by from the POJO
List<Object> key = buildPrimaryKey(e);
// primaryMap is the Map where results are stored
T value = primaryMap.get(key);
// If there is a corresponding record from the storage result
if(value ! =null) {
for (String elem : sumValues) {
// reflect the corresponding field, do the accumulation process
Field field = getDeclaredField(elem, e);
if (field.get(e) instanceof Integer) {
field.set(value, (Integer) field.get(e) + (Integer) field.get(value));
} else if (field.get(e) instanceof Long) {
field.set(value, (Long) field.get(e) + (Long) field.get(value));
} else {
throw new RuntimeException("Type exception, please handle exception");
}
}
// Process time records
Field field = getDeclaredField("updated", value);
if (null! = field) {
field.set(value, DateTimeUtils.getCurrentTime());
}
} else {
// the group by field is entered for the first time
try {
primaryMap.put(key, Tclone(e));
createdMap.put(key, DateTimeUtils.getCurrentTime());
}catch (Exception ex) {
log.info("first put value error {}" , e);
}
}
}
Copy the code
The article is explained from the perspective of pure interview, so there are many details that are not foreshadowed.
Reflection and generics fundamentals, for example, are all covered in Java3y basic tutorials and even ebooks that I won’t go into.
Welcome to follow my wechat official account [Interview Building Rocket] to talk about Java interview