這是我參與更文挑戰的第29天,活動詳情查看: 更文挑戰java
mongo
中,查詢是經過MongoTemplate
來實現數據的查詢已驗收
、未驗收
實際存儲的是一、2@Data
public class DangersBook implements Serializable {
@TimeSequence
@Ignore
@Id
@Query
private Long id;
@DangersNo
@Ignore
@Query
@LikeQuery
private String dangersNo;
@Ids
private String instance_id;
@DangersType
@Ids
private String app_id;
@Ids
@Query
@DangersEntId
@OrQuery
private String entId;
private String created_by;
@Query
@Ids
private String zuoyeleixing;
@Date
@Query
private String pizhunkaishi;
@Date
@Query
private String pizhunjieshu;
@UpdateItem(value = "已驗收")
@Query
@OrQuery
private String yanshouStatus;
@Query
@Date
@UpdateItem(value = "")
private String finishTime;
private String jianweixiuxukezheng;
private String zuoyedengji;
@Query
@LikeQuery
private String zuoyeneirong;
/** * @Description 用於判斷是否應該生成編號 */
@Ignore
@Query
private String createDangerNo;
private String laowubanzu;
@Query
private String test;
@Query
private String interrupt;
@Query
@OrQuery
private String year;
}
複製代碼
DangersBook
中的全部屬性!這是咱們對反射的第一處運用!實際上在項目中凡是須要寫活屬性的獲取的都是經過反射進行獲取結合註解進行屬性過濾!TimeSequence annotation = declaredField.getAnnotation(TimeSequence.class);
if (annotation!=null) {
//雪花算法
long generatorId = DistributedIDUtils.getInstance().getGeneratorId(1l, 2l, 3l);
declaredField.setAccessible(true);
declaredField.set(object, generatorId);
}
複製代碼
TimeSequence
以後咱們就能夠經過雪花算法生成id而後經過屬性將生成的id賦值進去!Ignore
就是這個做用Ignore ignore = declaredField.getAnnotation(Ignore.class);
if (ignore != null) {
continue;
}
複製代碼
Query
基礎上進行擴展的com.ay.sdksm.custom.components.expose.annation.Query queryField = declaredField.getAnnotation(com.ay.sdksm.custom.components.expose.annation.Query.class);
if (queryField != null) {
//須要添加查詢條件
try {
declaredField.setAccessible(true);
Object o = declaredField.get(dangersBook);
if (null != o&&!"".equals(o.toString())) {
String svalue = o.toString();
Date dateAnnotation = declaredField.getAnnotation(Date.class);
LikeQuery likeQuery = declaredField.getAnnotation(LikeQuery.class);
if (dateAnnotation != null) {
String dateValue = svalue;
if (StringUtils.isEmpty(dateValue)) {
continue;
}
String[] dateSplit = dateValue.split(",");
String start = dateSplit[0];
String end = start;
if (dateSplit.length >= 2) {
end = dateSplit[1];
}
criteria.and(declaredField.getName()).gte(start).lte(end);
} else if(likeQuery != null){
Pattern pattern = Pattern.compile(String.format(".*%s.*", svalue), Pattern.CASE_INSENSITIVE);
criteria.and(declaredField.getName()).regex(pattern);
}else {
OrQuery orQuery = declaredField.getAnnotation(OrQuery.class);
if (null != orQuery) {
String[] split = svalue.split(",");
String start = split[0];
String end = split[0];
if (split.length > 1) {
end = split[1];
}
criteria.and(declaredField.getName()).in(start, end);
} else {
if (svalue.contains("$ne")&& JsonUtils.getInstance().isJson(svalue)) {
JSONObject jsonObject = JSONObject.parseObject(svalue);
String $ne = jsonObject.getString("$ne");
criteria.and(declaredField.getName()).ne($ne);
}else if(svalue.contains("$like")&& JsonUtils.getInstance().isJson(svalue)){
//模糊查詢
JSONObject jsonObject = JSONObject.parseObject(svalue);
String $like = jsonObject.getString("$like");
criteria.and(declaredField.getName()).is(String.format("/%s/",$like));
} else {
Class<?> type = declaredField.getType();
if (type == Long.class) {
criteria.and(declaredField.getName()).is(Long.valueOf(svalue));
} else {
criteria.and(declaredField.getName()).is(svalue);
}
}
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
複製代碼
Query
註解表示須要對字段進行條件組裝。當遇到Date咱們就須要將條件值進行定製處理咱們約定的條件值是已逗號間隔的時間段。這樣咱們就能夠動態的支持時間點、時間段的查詢。LikeQuery
表示是模糊查詢。DangersNo annotation = declaredField.getAnnotation(DangersNo.class);
if (annotation != null) {
//根據業務生成指定格式的數據ID;這裏比較複雜。代碼省略。。。。。。
}
複製代碼
點贊,XDM程序員