MyBatis doesn't implement default methods of parent interface / base mapper











up vote
0
down vote

favorite












I try to implement a base mapper interface :



public interface IDAOBase<T, ID> {

default List<T> findAll() {
throw new RuntimeException("Method not implemented !");
}

default T findById(ID id) {
throw new RuntimeException("Method not implemented !");
}

default ID insert(T t) {
throw new RuntimeException("Method not implemented !");
}

default T update(T t) {
throw new RuntimeException("Method not implemented !");
}

default void delete(ID id) {
throw new RuntimeException("Method not implemented !");
}

default T getRowById(ID id) {
throw new RuntimeException("Method not implemented !");
}

}


This would be a mapper that extends the base interface :



@Mapper
interface UserMapper extends IDAOBase<UserVO, Long> {

UserVO findByUsername(String username);
// UserVO findById(Long id);
}


I invoke the mapper like this :



@Autowired
private UserMapper mapper;

public UserDetails loadUserById(Long id) {
return loadUser(mapper.findById(id));
}


But I get a RuntimeException thrown from the default method :



java.lang.RuntimeException: Method not implemented !
at com.project.services.IDAOBase.findById(IDAOBase.java:8)


in xml I have the "findById" method:



<select id="findById"
resultType="UserVO">
<include refid="baseSelect"/>
where
k.id = #{id}
</select>


Also; when I uncomment this line in mapper interface it works :



This works :



@Mapper
interface UserMapper extends IDAOBase<UserVO, Long> {

UserVO findByUsername(String username);
UserVO findById(Long id);
}


This doesn't work :



@Mapper
interface UserMapper extends IDAOBase<UserVO, Long> {

UserVO findByUsername(String username);
// UserVO findById(Long id);
}


What is the error here?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I try to implement a base mapper interface :



    public interface IDAOBase<T, ID> {

    default List<T> findAll() {
    throw new RuntimeException("Method not implemented !");
    }

    default T findById(ID id) {
    throw new RuntimeException("Method not implemented !");
    }

    default ID insert(T t) {
    throw new RuntimeException("Method not implemented !");
    }

    default T update(T t) {
    throw new RuntimeException("Method not implemented !");
    }

    default void delete(ID id) {
    throw new RuntimeException("Method not implemented !");
    }

    default T getRowById(ID id) {
    throw new RuntimeException("Method not implemented !");
    }

    }


    This would be a mapper that extends the base interface :



    @Mapper
    interface UserMapper extends IDAOBase<UserVO, Long> {

    UserVO findByUsername(String username);
    // UserVO findById(Long id);
    }


    I invoke the mapper like this :



    @Autowired
    private UserMapper mapper;

    public UserDetails loadUserById(Long id) {
    return loadUser(mapper.findById(id));
    }


    But I get a RuntimeException thrown from the default method :



    java.lang.RuntimeException: Method not implemented !
    at com.project.services.IDAOBase.findById(IDAOBase.java:8)


    in xml I have the "findById" method:



    <select id="findById"
    resultType="UserVO">
    <include refid="baseSelect"/>
    where
    k.id = #{id}
    </select>


    Also; when I uncomment this line in mapper interface it works :



    This works :



    @Mapper
    interface UserMapper extends IDAOBase<UserVO, Long> {

    UserVO findByUsername(String username);
    UserVO findById(Long id);
    }


    This doesn't work :



    @Mapper
    interface UserMapper extends IDAOBase<UserVO, Long> {

    UserVO findByUsername(String username);
    // UserVO findById(Long id);
    }


    What is the error here?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I try to implement a base mapper interface :



      public interface IDAOBase<T, ID> {

      default List<T> findAll() {
      throw new RuntimeException("Method not implemented !");
      }

      default T findById(ID id) {
      throw new RuntimeException("Method not implemented !");
      }

      default ID insert(T t) {
      throw new RuntimeException("Method not implemented !");
      }

      default T update(T t) {
      throw new RuntimeException("Method not implemented !");
      }

      default void delete(ID id) {
      throw new RuntimeException("Method not implemented !");
      }

      default T getRowById(ID id) {
      throw new RuntimeException("Method not implemented !");
      }

      }


      This would be a mapper that extends the base interface :



      @Mapper
      interface UserMapper extends IDAOBase<UserVO, Long> {

      UserVO findByUsername(String username);
      // UserVO findById(Long id);
      }


      I invoke the mapper like this :



      @Autowired
      private UserMapper mapper;

      public UserDetails loadUserById(Long id) {
      return loadUser(mapper.findById(id));
      }


      But I get a RuntimeException thrown from the default method :



      java.lang.RuntimeException: Method not implemented !
      at com.project.services.IDAOBase.findById(IDAOBase.java:8)


      in xml I have the "findById" method:



      <select id="findById"
      resultType="UserVO">
      <include refid="baseSelect"/>
      where
      k.id = #{id}
      </select>


      Also; when I uncomment this line in mapper interface it works :



      This works :



      @Mapper
      interface UserMapper extends IDAOBase<UserVO, Long> {

      UserVO findByUsername(String username);
      UserVO findById(Long id);
      }


      This doesn't work :



      @Mapper
      interface UserMapper extends IDAOBase<UserVO, Long> {

      UserVO findByUsername(String username);
      // UserVO findById(Long id);
      }


      What is the error here?










      share|improve this question













      I try to implement a base mapper interface :



      public interface IDAOBase<T, ID> {

      default List<T> findAll() {
      throw new RuntimeException("Method not implemented !");
      }

      default T findById(ID id) {
      throw new RuntimeException("Method not implemented !");
      }

      default ID insert(T t) {
      throw new RuntimeException("Method not implemented !");
      }

      default T update(T t) {
      throw new RuntimeException("Method not implemented !");
      }

      default void delete(ID id) {
      throw new RuntimeException("Method not implemented !");
      }

      default T getRowById(ID id) {
      throw new RuntimeException("Method not implemented !");
      }

      }


      This would be a mapper that extends the base interface :



      @Mapper
      interface UserMapper extends IDAOBase<UserVO, Long> {

      UserVO findByUsername(String username);
      // UserVO findById(Long id);
      }


      I invoke the mapper like this :



      @Autowired
      private UserMapper mapper;

      public UserDetails loadUserById(Long id) {
      return loadUser(mapper.findById(id));
      }


      But I get a RuntimeException thrown from the default method :



      java.lang.RuntimeException: Method not implemented !
      at com.project.services.IDAOBase.findById(IDAOBase.java:8)


      in xml I have the "findById" method:



      <select id="findById"
      resultType="UserVO">
      <include refid="baseSelect"/>
      where
      k.id = #{id}
      </select>


      Also; when I uncomment this line in mapper interface it works :



      This works :



      @Mapper
      interface UserMapper extends IDAOBase<UserVO, Long> {

      UserVO findByUsername(String username);
      UserVO findById(Long id);
      }


      This doesn't work :



      @Mapper
      interface UserMapper extends IDAOBase<UserVO, Long> {

      UserVO findByUsername(String username);
      // UserVO findById(Long id);
      }


      What is the error here?







      mybatis ibatis spring-mybatis






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 16:07









      Wolf359

      575622




      575622
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          In mybatis default methods of mappers are invoked directly and this is a feature. They should not be mapped in xml mapping and the idea is in reusing some generic mapper methods in more specific methods that return the same data but in different format etc:



          public interface MyMapper {

          List<MyObject> getByIds(List<Long> ids);

          default MyObject getById(Long id) {
          List<MyObject> list = getByIds(Arrays.asList(id));
          // checks
          return list.get(0);
          }


          List<MyObject> getAll();

          default Map<Long, MyObject> getAllAsMap() {
          return getAll().stream().collect(
          Collectors.toMap(MyObject::getId, Functions.identity()));
          }
          }


          In your example, just remove the default implementation from the parent mapper and it will work.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378575%2fmybatis-doesnt-implement-default-methods-of-parent-interface-base-mapper%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            In mybatis default methods of mappers are invoked directly and this is a feature. They should not be mapped in xml mapping and the idea is in reusing some generic mapper methods in more specific methods that return the same data but in different format etc:



            public interface MyMapper {

            List<MyObject> getByIds(List<Long> ids);

            default MyObject getById(Long id) {
            List<MyObject> list = getByIds(Arrays.asList(id));
            // checks
            return list.get(0);
            }


            List<MyObject> getAll();

            default Map<Long, MyObject> getAllAsMap() {
            return getAll().stream().collect(
            Collectors.toMap(MyObject::getId, Functions.identity()));
            }
            }


            In your example, just remove the default implementation from the parent mapper and it will work.






            share|improve this answer



























              up vote
              1
              down vote



              accepted










              In mybatis default methods of mappers are invoked directly and this is a feature. They should not be mapped in xml mapping and the idea is in reusing some generic mapper methods in more specific methods that return the same data but in different format etc:



              public interface MyMapper {

              List<MyObject> getByIds(List<Long> ids);

              default MyObject getById(Long id) {
              List<MyObject> list = getByIds(Arrays.asList(id));
              // checks
              return list.get(0);
              }


              List<MyObject> getAll();

              default Map<Long, MyObject> getAllAsMap() {
              return getAll().stream().collect(
              Collectors.toMap(MyObject::getId, Functions.identity()));
              }
              }


              In your example, just remove the default implementation from the parent mapper and it will work.






              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                In mybatis default methods of mappers are invoked directly and this is a feature. They should not be mapped in xml mapping and the idea is in reusing some generic mapper methods in more specific methods that return the same data but in different format etc:



                public interface MyMapper {

                List<MyObject> getByIds(List<Long> ids);

                default MyObject getById(Long id) {
                List<MyObject> list = getByIds(Arrays.asList(id));
                // checks
                return list.get(0);
                }


                List<MyObject> getAll();

                default Map<Long, MyObject> getAllAsMap() {
                return getAll().stream().collect(
                Collectors.toMap(MyObject::getId, Functions.identity()));
                }
                }


                In your example, just remove the default implementation from the parent mapper and it will work.






                share|improve this answer














                In mybatis default methods of mappers are invoked directly and this is a feature. They should not be mapped in xml mapping and the idea is in reusing some generic mapper methods in more specific methods that return the same data but in different format etc:



                public interface MyMapper {

                List<MyObject> getByIds(List<Long> ids);

                default MyObject getById(Long id) {
                List<MyObject> list = getByIds(Arrays.asList(id));
                // checks
                return list.get(0);
                }


                List<MyObject> getAll();

                default Map<Long, MyObject> getAllAsMap() {
                return getAll().stream().collect(
                Collectors.toMap(MyObject::getId, Functions.identity()));
                }
                }


                In your example, just remove the default implementation from the parent mapper and it will work.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 at 17:33

























                answered Nov 19 at 16:57









                Roman Konoval

                7,46312031




                7,46312031






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378575%2fmybatis-doesnt-implement-default-methods-of-parent-interface-base-mapper%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen