查询一条数据:(多用于保存时判断db中是否已有当前数据,这里 is 精确匹配,模糊匹配 使用 regex…)
public PageUrl getByUrl(String url) {
return findOne(new Query(Criteria.where("url").is(url)),PageUrl.class);
}
查询多条数据:linkUrl.id 属于分级查询
public List getPageUrlsByUrl(int begin, int end,String linkUrlid) {
Query query = new Query();
query.addCriteria(Criteria.where("linkUrl.id").is(linkUrlid));
return find(query.limit(end - begin).skip(begin), PageUrl.class);
}
模糊查询:
public long getProcessLandLogsCount(List conditions)
{
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).regex(".*?\\" +condition.getValue().toString()+ ".*"));
}
}
return count(query, ProcessLandLog.class);
}
gte: 大于等于,lte小于等于…注意查询的时候各个字段的类型要和mongodb中数据类型一致
public List getProcessLandLogs(int begin,int end,List conditions,String orderField,Direction direction)
{
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
if(condition.getKey().equals("time")){
query.addCriteria(Criteria.where("time").gte(condition.getValue())); //gte: 大于等于
}else if(condition.getKey().equals("insertTime")){
query.addCriteria(Criteria.where("insertTime").gte(condition.getValue()));
}else{
query.addCriteria(Criteria.where(condition.getKey()).is(condition.getValue()));
}
}
}
return find(query.limit(end - begin).skip(begin).with(new Sort(new Sort.Order(direction, orderField))), ProcessLandLog.class);
}
public List getDpsLandsByTime(int begin, int end, Date beginDate,Date endDate) {
return find(new Query(Criteria.where("updateTime").gte(beginDate).lte(endDate)).limit(end - begin).skip(begin),
DpsLand.class);
}
查询字段不存在的数据
public List getGoodsDetails2(int begin, int end) {
Query query = new Query();
query.addCriteria(Criteria.where("goodsSummary").not());
return find(query.limit(end - begin).skip(begin),GoodsDetail.class);
}
查询字段不为空的数据
Criteria.where("key1").ne("").ne(null)
查询或语句:a || b
Criteria criteria = new Criteria();
criteria.orOperator(Criteria.where("key1").is("0"),Criteria.where("key1").is(null));
查询且语句:a && b
Criteria criteria = new Criteria();
criteria.and("key1").is(false);
criteria.and("key2").is(type);
Query query = new Query(criteria);
long totalCount = this.mongoTemplate.count(query, Xxx.class);
查询一个属性的子属性,例如:查下面数据的key2.keyA的语句
var s = {
key1: value1,
key2: {
keyA: valueA,
keyB: valueB
}
};
@Query("{'key2.keyA':?0}")
List findAllBykeyA(String keyA);
查询数量:
public long getPageInfosCount(List conditions) {
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).is(condition.getValue()));
}
}
return count(query, PageInfo.class);
}
更新一条数据的一个字段:
public WriteResult updateTime(PageUrl pageUrl) {
String id = pageUrl.getId();
return updateFirst(new Query(Criteria.where("id").is(id)),Update.update("updateTime", pageUrl.getUpdateTime()), PageUrl.class);
}
更新一条数据的多个字段:
//调用更新
private void updateProcessLandLog(ProcessLandLog processLandLog,
int crawlResult) {
List fields = new ArrayList();
List
//更新
public void updateProcessLandLog(ProcessLandLog land, List fields,List values) {
Update update = new Update();
int size = fields.size();
for(int i = 0 ; i < size; i++){
String field = fields.get(i);
Object value = values.get(i);
update.set(field, value);
}
updateFirst(new Query(Criteria.where("id").is(land.getId())), update,ProcessLandLog.class);
}
删除数据:
public void deleteObject(Class clazz,String id) {
remove(new Query(Criteria.where("id").is(id)),clazz);
}
9.保存数据:
//插入一条数据
public void saveObject(Object obj) {
insert(obj);
}
//插入多条数据
public void saveObjects(List objects) {
for(T t:objects){
insert(t);
}
}
使用的例子:
public Map getAppPortDetailByPage(int pageNo, int pageSize, String order, String sortBy, String appPortType, String appPortSeacherName) {
Criteria criteria = new Criteria();
if (!appPortType.equals("")) {
if (!appPortType.equals("all")) {
//DB表里的字段----appmanageType
//下同 port protocol 也是DB表的字段
criteria.and("appmanageType").is(appPortType);
}
}
if (!appPortSeacherName.equals("")) {
try {
criteria.orOperator(Criteria.where("port").is(Integer.parseInt(appPortSeacherName)),
Criteria.where("protocol").regex(".*?" + appPortSeacherName + ".*"));
}catch (Exception e){
criteria.orOperator(Criteria.where("protocol").regex(".*?" + appPortSeacherName + ".*"));
}
}
Map result = Maps.newHashMap();
Query query = new Query(criteria);
query.skip((pageNo - 1) * pageSize);
query.limit(pageSize);
if(order != null && sortBy != null){
query.with(new Sort(new Sort.Order(order.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sortBy)));
}else {
query.with(new Sort(new Sort.Order(Sort.Direction.ASC, "port")));
}
List list = this.mongoTemplate.find(query, Appportmanage.class);
long count = this.mongoTemplate.count(query, Appportmanage.class);
result.put("datas", list);
result.put("size", count);
return result;
}
原题链接:#137 Single Number II
要求:
给定一个整型数组,其中除了一个元素之外,每个元素都出现三次。找出这个元素
注意:算法的时间复杂度应为O(n),最好不使用额外的内存空间
难度:中等
分析:
与#136类似,都是考察位运算。不过出现两次的可以使用异或运算的特性 n XOR n = 0, n XOR 0 = n,即某一
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, det