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?
mybatis ibatis spring-mybatis
add a comment |
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?
mybatis ibatis spring-mybatis
add a comment |
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?
mybatis ibatis spring-mybatis
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
mybatis ibatis spring-mybatis
asked Nov 19 at 16:07
Wolf359
575622
575622
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 19 at 17:33
answered Nov 19 at 16:57
Roman Konoval
7,46312031
7,46312031
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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