简体   繁体   中英

Creating a generic mongo repository in spring boot

I want to create a generic mongo repository and just pass the document and use it, instead of creating multiple repositories for each document.

So far this is what I thought about:

@NoRepositoryBean
public interface GenericRepository<T, String> extends MongoRepository<T,String> {
 T findById(ObjectId id);
}


@RestController
@RequestMapping("/endpoint")
public class Controller {

    @Autowired
    public GenericRepository<DocumentClass, String> repository;

    @GetMapping("/")
    public List<DocumentClass> all(){
        return repository.findAll();
    }

}

I'm not sure this is the correct way of doing so because I'm getting a lot of errors.

Try it like this

@NoRepositoryBean
public interface GenericRepository< T,  ID > extends MongoRepository< T, ID >{ 
    
    @Override
    public T findById( ID id );

    @Override    
    public List< T > findAll();
    
    ...
}

…Then given some entity…

public class SomeEntity{ 
    
    private final String id;
    
    private final String someField;
    
    public SomeEntity( String id, String someField ){ 
        this.id = id;
        this.someField = someField;
    }
    
    public String getId( ){ return this.id; }

    ...   
}

…A concrete implementation could be like…

public class SomeEntityRepository implements GenericRepository< SomeEntity, String >{ 
    ...

    @Override
    public SomeEntity findById( String id ){ /* Your implementation goes here */  }

    @Override    
    public List< SomeEntity > findAll(){ /* Your implementation goes here */ }

    ...
}

Here's a demonstration of how that would be used

    /* Given */
    SomeEntity anNtt = new SomeEntity( "foo", "bar" );
    
    MongoRepository< SomeEntity, String > repo = new SomeEntityRepository( ) ;
    
    repo.save( anNtt );
    
    out.printf( "Saved entity:%1$35s%n", anNtt );
    
    /* Manual (i.e., non-Spring) dependency injection */
    Controller ctrlR = new Controller( repo );
    
    /* When */
    List< SomeEntity > allNtts = ctrlR.all( );
    SomeEntity oneNtt = ctrlR.one( anNtt.getId( ) );
    
    /* Then */
    out.printf( "Found exactly 1? %1$20s%n", allNtts.size( ) == 1 );
    out.printf( "Found exactly %1$20s%n", allNtts.size( ) );
    out.printf( "Found entity.equals( Saved entity )? %1$20s%n", allNtts.get( 0 ).equals( anNtt ) && oneNtt.equals( anNtt ) );
    out.printf( "Found entity: %1$35s%n", oneNtt );
    
}

Click the green Start button at the top of the demo to run it…

Saved entity: SomeEntity [id=foo, someField=bar]
Found exactly 1?                 true
Found exactly                    1
Found entity.equals( Saved entity )?                 true
Found entity:  SomeEntity [id=foo, someField=bar]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM