发表于 2015-7-9 11:41:01

mongodb spring anno 查询

public interface LogTaskBuilderRepository extends MongoRepository{
@Query(value="{ 'type': {'$in' : ?0} }",fields="{ 'type' : 1}")
public List findTest(String[] name);
@Query(value="{?0:?1}")
public List findTest2(Object field,Object Value);
//正则测试
@Query(value="{ 'type' :{'$regex' :?0, '$options': 'i'} }")   
public List findTest3(String name);
//日期查询

@Query(value="{ 'executeDate' :{'$gt' : ?0 } }")   
public List findTest4(Date date);
//内部查询 findBy{字段}{条件}
public List findByTypeLike(String name);
public List findByTypeLike(String name, Sort sort);
public List findByTypeLike(String name, Pageable pageable);
}


public class LogTaskBuilder {
@Id
private String name;
private RecordType type;
private Date executeDate;
}
  
  test



System.out.println(logTaskBuilderRepository.findByTypeLike("SoulRecord").size());
System.out.println(logTaskBuilderRepository.findTest3("Record").size());
System.out.println(logTaskBuilderRepository.findByTypeLike("Record",new Sort("type", Order.DESCENDING)).size());
System.out.println(logTaskBuilderRepository.findTest(new String[]{"SoulRecord","ItemRecord"}).size());
System.out.println(logTaskBuilderRepository.findTest2("name","SoulRecord").size());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
System.out.println(logTaskBuilderRepository.findTest4(dateFormat.parse("2012-10-26")).size());
} catch (ParseException e) {
e.printStackTrace();
}
页: [1]
查看完整版本: mongodb spring anno 查询