英文:
Creating a generic mongo repository in spring boot
问题
我想创建一个通用的Mongo存储库,只需传递文档并使用它,而不是为每个文档创建多个存储库。
到目前为止,这是我考虑的内容:
@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 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.
答案1
得分: 1
尝试这样[*(点击此处查看示例)*](https://www.browxy.com#USER_306296)…
@NoRepositoryBean
public interface GenericRepository<T, ID> extends MongoRepository<T, ID> {
@Override
public T findById(ID id);
@Override
public List<T> findAll();
...
}
…然后给定某个实体…
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; }
...
}
…具体的实现可以是这样的…
public class SomeEntityRepository implements GenericRepository<SomeEntity, String> {
...
@Override
public SomeEntity findById(String id) { /* 在这里添加您的实现 */ }
@Override
public List<SomeEntity> findAll() { /* 在这里添加您的实现 */ }
...
}
[*这里展示了如何使用此方法*](https://www.browxy.com#USER_306296)…
/* 给定 */
SomeEntity anNtt = new SomeEntity("foo", "bar");
MongoRepository<SomeEntity, String> repo = new SomeEntityRepository();
repo.save(anNtt);
out.printf("已保存实体:%1$35s%n", anNtt);
/* 手动(即非Spring)依赖注入 */
Controller ctrlR = new Controller(repo);
/* 当 */
List<SomeEntity> allNtts = ctrlR.all();
SomeEntity oneNtt = ctrlR.one(anNtt.getId());
/* 那么 */
out.printf("是否找到正好1个?%1$20s%n", allNtts.size() == 1);
out.printf("找到正好%1$20s%n", allNtts.size());
out.printf("找到的实体.equals( 已保存的实体 )?%1$20s%n", allNtts.get(0).equals(anNtt) && oneNtt.equals(anNtt));
out.printf("找到的实体:%1$35s%n", oneNtt);
}
点击上方演示的绿色***开始***按钮来运行它…
已保存实体:SomeEntity [id=foo, someField=bar]
是否找到正好1个? true
找到正好 1
找到的实体.equals( 已保存的实体 )? true
找到的实体: SomeEntity [id=foo, someField=bar]
英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论