相关接口
@NoRepositoryBean
public interface MongoRepository extends PagingAndSortingRepository, QueryByExampleExecutor {
List saveAll(Iterable var1);
S insert(S var1);
List insert(Iterable var1);
List findAll();
List findAll(Sort var1);
List findAll(Example var1);
List findAll(Example var1, Sort var2);
}
*************************************************
@NoRepositoryBean
public interface PagingAndSortingRepository extends CrudRepository {
Iterable findAll(Sort var1);
Page findAll(Pageable var1);
}
**************************************************
@NoRepositoryBean
public interface CrudRepository extends Repository {
S save(S var1);
Iterable saveAll(Iterable var1);
Optional findById(ID var1);
boolean existsById(ID var1);
Iterable findAll();
Iterable findAllById(Iterable var1);
long count();
void deleteById(ID var1);
void delete(T var1);
void deleteAll(Iterable extends T> var1);
void deleteAll();
}
**********************************************
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
*********************************************
public interface QueryByExampleExecutor {
Optional findOne(Example var1);
Iterable findAll(Example var1);
Iterable findAll(Example var1, Sort var2);
Page findAll(Example var1, Pageable var2);
long count(Example var1);
boolean exists(Example var1);
}
***********************************************
动态查询时使用
public interface Example {
static Example of(T probe) {
return new TypedExample(probe, ExampleMatcher.matching());
}
static Example of(T probe, ExampleMatcher matcher) {
return new TypedExample(probe, matcher);
}
T getProbe();
ExampleMatcher getMatcher();
default Class getProbeType() {
return ProxyUtils.getUserClass(this.getProbe().getClass());
}
}
*************************************************
public interface ExampleMatcher {
static ExampleMatcher matching() {
return matchingAll();
}
static ExampleMatcher matchingAny() {
return (new TypedExampleMatcher()).withMode(ExampleMatcher.MatchMode.ANY);
}
static ExampleMatcher matchingAll() {
return (new TypedExampleMatcher()).withMode(ExampleMatcher.MatchMode.ALL);
}
ExampleMatcher withIgnorePaths(String... var1);
ExampleMatcher withStringMatcher(ExampleMatcher.StringMatcher var1);
default ExampleMatcher withIgnoreCase() {
return this.withIgnoreCase(true);
}
ExampleMatcher withIgnoreCase(boolean var1);
。。。。
}
相关注解
文档
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Document {
@AliasFor("collection")
String value() default "";
@AliasFor("value")
String collection() default "";
String language() default "";
}
主键
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Id {
}
字段属性
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Field {
String value() default "";
int order() default 2147483647;
}
索引
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Indexed {
boolean unique() default false;
IndexDirection direction() default IndexDirection.ASCENDING;
boolean sparse() default false;
String name() default "";
boolean useGeneratedName() default false;
boolean background() default false;
int expireAfterSeconds() default -1;
}
复合索引
@Target({ElementType.TYPE})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CompoundIndex {
String def() default "";
boolean unique() default false;
boolean sparse() default false;
String name() default "";
boolean useGeneratedName() default false;
boolean background() default false;
}
******************************************
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CompoundIndexes {
CompoundIndex[] value();
}
使用示例
**********************************
dao 层
public interface PersonRepository extends MongoRepository {
Person getFirstByName(String name);
List getAllByAgeGreaterThanEqual(Integer age);
List getByNameLike(String name);
}
****************************************
service 层
public interface PersonService {
Person getByName(String name);
List getByAge(Integer age);
}
*******************************************
serviceImpl 层
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonRepository personRepository;
@Override
public Person getByName(String name) {
return personRepository.getFirstByName(name);
}
@Override
public List getByAge(Integer age) {
return personRepository.getAllByAgeGreaterThanEqual(age);
}
}
*******************************************
controller 层
@RestController
public class PersonController {
@Autowired
private PersonRepository personRepository;
@Autowired
private PersonService personService;
@RequestMapping("/save")
public String save(){
for(int i=0;i<100;i++){
Person person=new Person();
person.setId(i);
person.setName("瓜田李下"+i);
person.setAge(i%10+15);
personRepository.save(person);
}
return "success";
}
@RequestMapping("/get")
public Person get(){
return personService.getByName("瓜田李下2");
}
@RequestMapping("/get2")
public List get2(){
return personService.getByAge(18);
}
********************************
动态查询
@RequestMapping("/get3")
public List get3(String name,Integer age){
Person person=new Person();
if(name!=null){
person.setName(name);
}
if(age!=null){
person.setAge(age);
}
Example example=Example.of(person);
return personRepository.findAll(example);
}
**************************
排序
@RequestMapping("/get4")
public List get4(){
Sort sort=Sort.by(Sort.Direction.DESC,"age");
return personRepository.findAll(sort);
}
****************************
分页
@RequestMapping("/get5")
public List get5(int pageNum,int pageSize){
PageRequest pageRequest=PageRequest.of(pageNum,pageSize);
return personRepository.findAll(pageRequest).getContent();
}
*****************************
排序分页
@RequestMapping("/get6")
public List get6(){
Sort sort=Sort.by(Sort.Direction.DESC,"age");
PageRequest pageRequest=PageRequest.of(2,5,sort);
Page page=personRepository.findAll(pageRequest);
System.out.println("总页数为:"+page.getTotalPages());
System.out.println("总条数为:"+page.getTotalElements());
return page.getContent();
}
******************************
模糊查询
@RequestMapping("/get8")
public List get8(){
return personRepository.getByNameLike("瓜田李下");
}
}