1. <i id='N6995'><tr id='N6995'><dt id='N6995'><q id='N6995'><span id='N6995'><b id='N6995'><form id='N6995'><ins id='N6995'></ins><ul id='N6995'></ul><sub id='N6995'></sub></form><legend id='N6995'></legend><bdo id='N6995'><pre id='N6995'><center id='N6995'></center></pre></bdo></b><th id='N6995'></th></span></q></dt></tr></i><div id='N6995'><tfoot id='N6995'></tfoot><dl id='N6995'><fieldset id='N6995'></fieldset></dl></div>
        <tfoot id='N6995'></tfoot>
          <bdo id='N6995'></bdo><ul id='N6995'></ul>

        <small id='N6995'></small><noframes id='N6995'>

        <legend id='N6995'><style id='N6995'><dir id='N6995'><q id='N6995'></q></dir></style></legend>

        &lt;S 扩展 T&gt; 是什么意思保存(S实体);在 Spring 存储库中?

        时间:2024-08-24
        <i id='nj4z6'><tr id='nj4z6'><dt id='nj4z6'><q id='nj4z6'><span id='nj4z6'><b id='nj4z6'><form id='nj4z6'><ins id='nj4z6'></ins><ul id='nj4z6'></ul><sub id='nj4z6'></sub></form><legend id='nj4z6'></legend><bdo id='nj4z6'><pre id='nj4z6'><center id='nj4z6'></center></pre></bdo></b><th id='nj4z6'></th></span></q></dt></tr></i><div id='nj4z6'><tfoot id='nj4z6'></tfoot><dl id='nj4z6'><fieldset id='nj4z6'></fieldset></dl></div>
          <legend id='nj4z6'><style id='nj4z6'><dir id='nj4z6'><q id='nj4z6'></q></dir></style></legend>

            • <tfoot id='nj4z6'></tfoot>
                  <tbody id='nj4z6'></tbody>

                  <bdo id='nj4z6'></bdo><ul id='nj4z6'></ul>

                  <small id='nj4z6'></small><noframes id='nj4z6'>

                  本文介绍了&lt;S 扩展 T&gt; 是什么意思保存(S实体);在 Spring 存储库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Spring Data 项目中,CrudRepository 为正在管理的实体类提供复杂的 CRUD 功能.

                  In Spring Data project the CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed.

                  public interface CrudRepository<T, ID extends Serializable>
                  extends Repository<T, ID> {
                  
                      <S extends T> S save(S entity);
                  
                      T findOne(ID primaryKey);
                  
                      Iterable<T> findAll();
                  
                      Long count();
                  
                      void delete(T entity);
                  
                      boolean exists(ID primaryKey);
                  
                      // … more functionality omitted.
                  }
                  

                  一般来说,我知道S extends T"是什么意思,即save操作的返回类型S必须是T的子类型.为什么要加上这样的约束?我认为这样做会很好:

                  In general, I know what "S extends T" means, i.e. that S, the return type of save operation, must be subtype of T. Why is it necessary to add such as constraint? I think that would be fine doing something like this:

                  T save (T entity);
                  

                  void delete(T entity);
                  

                  我在谷歌上搜索以寻求更多帮助,我发现了关于 stackoverflow 本身的以下问题,但对我来说并不是很清楚:

                  I've googled to find more help and I've figured out following question on stackoverflow itself but it isn't really clear for me:

                  Spring CrudRepository:为什么要发明一个新的泛型S型

                  谢谢.

                  推荐答案

                  如果你想拥有它

                  T save (T entity);
                  

                  那么您可以将结果分配给的唯一变量必须是 T 类型.

                  Then the only variable you could assign the result to would have to be of type T.

                  所以,如果你有一个 CrudRepository<Animal,AnimalID>存储库,你有

                  So, if you have a CrudRepository<Animal,AnimalID> repository, and you have

                  Dog dog = getDog();
                  Dog savedDog = repository.save(dog);
                  

                  你会得到一个编译错误 - 你不能将结果分配给 Dog,因为它必须是 T 类型,在这种情况下,动物.

                  You'd get a compile error - you can't assign the result to Dog, as it has to be of type T, in this case, Animal.

                  您需要检查返回的值是否确实是 Dog 类型,如果是,则将其转换为 Dog 以将其放入 savedDog.

                  You'd need to check if the returned value was indeed of type Dog, and if so, cast it to Dog to put it in savedDog.

                  按照原样声明,这意味着您可以将其分配给与原始参数相同类型的变量,因为类型解析将允许这样做.

                  With the declaration as it is, it means that you can assign it to a variable of the same type as the original argument, as type resolution will allow that.

                  声明本身并没有具体说明如何保存狗的非动物部分(如果有的话).它所做的只是允许将结果分配回 Dog 如果它最初是 Dog.

                  The declaration itself doesn't specify how the non-animal parts of the dog are saved if at all. All it does is allow assigning the result back to a Dog if it was originally a Dog.

                  这篇关于&lt;S 扩展 T&gt; 是什么意思保存(S实体);在 Spring 存储库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  • <legend id='YkWJ5'><style id='YkWJ5'><dir id='YkWJ5'><q id='YkWJ5'></q></dir></style></legend>

                        <i id='YkWJ5'><tr id='YkWJ5'><dt id='YkWJ5'><q id='YkWJ5'><span id='YkWJ5'><b id='YkWJ5'><form id='YkWJ5'><ins id='YkWJ5'></ins><ul id='YkWJ5'></ul><sub id='YkWJ5'></sub></form><legend id='YkWJ5'></legend><bdo id='YkWJ5'><pre id='YkWJ5'><center id='YkWJ5'></center></pre></bdo></b><th id='YkWJ5'></th></span></q></dt></tr></i><div id='YkWJ5'><tfoot id='YkWJ5'></tfoot><dl id='YkWJ5'><fieldset id='YkWJ5'></fieldset></dl></div>
                      1. <small id='YkWJ5'></small><noframes id='YkWJ5'>

                          <tbody id='YkWJ5'></tbody>
                          <bdo id='YkWJ5'></bdo><ul id='YkWJ5'></ul>
                          <tfoot id='YkWJ5'></tfoot>