Search code examples
mybatisibatisspring-mybatis

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


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?


Solution

  • 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.