在Spring Boot中创建一个通用的Mongo仓库。

huangapple go评论89阅读模式
英文:

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&lt;T, String&gt; extends MongoRepository&lt;T,String&gt; {
 T findById(ObjectId id);
}


@RestController
@RequestMapping(&quot;/endpoint&quot;)
public class Controller {

    @Autowired
    public GenericRepository&lt;DocumentClass, String&gt; repository;

    @GetMapping(&quot;/&quot;)
    public List&lt;DocumentClass&gt; 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&hellip;

@NoRepositoryBean
public interface GenericRepository&lt; T,  ID &gt; extends MongoRepository&lt; T, ID &gt;{ 
@Override
public T findById( ID id );
@Override    
public List&lt; T &gt; findAll();
...
}

&hellip;Then given some entity&hellip;

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; }
...   
}

&hellip;A concrete implementation could be like&hellip;

public class SomeEntityRepository implements GenericRepository&lt; SomeEntity, String &gt;{ 
...
@Override
public SomeEntity findById( String id ){ /* Your implementation goes here */  }
@Override    
public List&lt; SomeEntity &gt; findAll(){ /* Your implementation goes here */ }
...
}

Here's a demonstration of how that would be used&hellip;

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

Click the green Start button at the top of the demo to run it&hellip;

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]

huangapple
  • 本文由 发表于 2020年8月8日 22:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定