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?
mybatis ibatis spring-mybatis
add a comment |
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 |
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 '18 at 16:07
Wolf359
612823
612823
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 17:33
answered Nov 19 '18 at 16:57
Roman Konoval
7,72112131
7,72112131
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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