-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2767 from FlyInWind1/no-result-type
Resolve resultType by namespace and id when not provide resultType and resultMap
- Loading branch information
Showing
10 changed files
with
316 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/java/org/apache/ibatis/submitted/no_result_type_map/Mapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2009-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.no_result_type_map; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface Mapper extends ParentMapper<User>{ | ||
|
||
User getUser(@Param("id") Integer id); | ||
|
||
List<User> getAllUsers(); | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
src/test/java/org/apache/ibatis/submitted/no_result_type_map/NoResultTypeMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2009-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.no_result_type_map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.Reader; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.BaseDataTest; | ||
import org.apache.ibatis.exceptions.PersistenceException; | ||
import org.apache.ibatis.executor.ExecutorException; | ||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class NoResultTypeMapTest { | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeAll | ||
static void setUp() throws Exception { | ||
// create a SqlSessionFactory | ||
try (Reader reader = Resources | ||
.getResourceAsReader("org/apache/ibatis/submitted/no_result_type_map/mybatis-config.xml")) { | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
sqlSessionFactory.getConfiguration().addMapper(Mapper.class); | ||
} | ||
|
||
// populate in-memory database | ||
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), | ||
"org/apache/ibatis/submitted/no_result_type_map/CreateDB.sql"); | ||
} | ||
|
||
@Test | ||
void shouldGetAUser() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
User user = mapper.getUser(1); | ||
Assertions.assertEquals("User1", user.getName()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetAllUsers() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
List<User> users = mapper.getAllUsers(); | ||
Assertions.assertEquals(3, users.size()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldResolveInheritedReturnType() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
List<User> users = mapper.getAllUsersInParent(); | ||
Assertions.assertEquals(3, users.size()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldFailIfNoMatchingMethod() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
PersistenceException ex = assertThrows(PersistenceException.class, | ||
() -> sqlSession.selectList("org.apache.ibatis.submitted.no_result_type_map.Mapper.noMatchingMethod")); | ||
ExecutorException cause = (ExecutorException) ex.getCause(); | ||
assertEquals("A query was run and no Result Maps were found for the Mapped Statement " | ||
+ "'org.apache.ibatis.submitted.no_result_type_map.Mapper.noMatchingMethod'. " | ||
+ "'resultType' or 'resultMap' must be specified when there is no corresponding method.", | ||
cause.getMessage()); | ||
} | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/org/apache/ibatis/submitted/no_result_type_map/ParentMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2009-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.no_result_type_map; | ||
|
||
import java.util.List; | ||
|
||
public interface ParentMapper<T> { | ||
|
||
List<T> getAllUsersInParent(); | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/org/apache/ibatis/submitted/no_result_type_map/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2009-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.no_result_type_map; | ||
|
||
public class User { | ||
|
||
private Integer id; | ||
private String name; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/resources/org/apache/ibatis/submitted/no_result_type_map/CreateDB.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-- | ||
-- Copyright 2009-2023 the original author or authors. | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- https://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
|
||
drop table users if exists; | ||
|
||
create table users ( | ||
id int, | ||
name varchar(20) | ||
); | ||
|
||
insert into users (id, name) values(1, 'User1'); | ||
insert into users (id, name) values(2, 'User2'); | ||
insert into users (id, name) values(3, 'User3'); |
41 changes: 41 additions & 0 deletions
41
src/test/resources/org/apache/ibatis/submitted/no_result_type_map/Mapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2009-2023 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"https://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="org.apache.ibatis.submitted.no_result_type_map.Mapper"> | ||
|
||
<select id="getUser"> | ||
select * from users where id = #{id} | ||
</select> | ||
|
||
<select id="getAllUsers"> | ||
select * from users | ||
</select> | ||
|
||
<select id="getAllUsersInParent"> | ||
select * from users | ||
</select> | ||
|
||
<select id="noMatchingMethod"> | ||
select * from users | ||
</select> | ||
|
||
</mapper> |
Oops, something went wrong.