-
Notifications
You must be signed in to change notification settings - Fork 13k
Allow referencing result map from @One and @Many #1771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
harawata
merged 6 commits into
mybatis:master
from
moonService:dev/add_resultmapId_to_annotion_many_one
Feb 5, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
77 changes: 77 additions & 0 deletions
77
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql
This file contains hidden or 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,77 @@ | ||
| -- | ||
| -- Copyright 2009-2019 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 | ||
| -- | ||
| -- http://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. | ||
| -- | ||
|
|
||
| -- ---------------------------- | ||
| -- Table structure for role | ||
| -- ---------------------------- | ||
| CREATE TABLE role ( | ||
| id int, | ||
| role_name varchar(10) | ||
| ); | ||
|
|
||
| -- ---------------------------- | ||
| -- Records of role | ||
| -- ---------------------------- | ||
| INSERT INTO role (id,role_name) | ||
| VALUES ('1', 'teacher'); | ||
| INSERT INTO role (id,role_name) | ||
| VALUES ('2', 'student'); | ||
| INSERT INTO role (id,role_name) | ||
| VALUES ('3', 'Headmaster'); | ||
| INSERT INTO role (id,role_name) | ||
| VALUES ('4', 'Learning-commissary'); | ||
|
|
||
| CREATE TABLE user ( | ||
| id int, | ||
| username varchar(32), | ||
| ); | ||
|
|
||
| -- ---------------------------- | ||
| -- Records of user | ||
| -- ---------------------------- | ||
| INSERT INTO user (id,username) | ||
| VALUES ('1', 'James Gosling'); | ||
| INSERT INTO user (id,username) | ||
| VALUES ('2', 'Doug Lea'); | ||
| INSERT INTO user (id,username) | ||
| VALUES ('3', 'Rod johnson'); | ||
| INSERT INTO user (id,username) | ||
| VALUES ('4', 'Juergen Hoeller'); | ||
|
|
||
| -- ---------------------------- | ||
| -- Table structure for `user_role` | ||
| -- ---------------------------- | ||
| CREATE TABLE user_role ( | ||
| id int, | ||
| role_id int, | ||
| user_id int | ||
| ); | ||
|
|
||
| -- ---------------------------- | ||
| -- Records of user_role | ||
| -- ---------------------------- | ||
| INSERT INTO user_role (id,role_id,user_id) | ||
| VALUES ('1', '2', '4'); | ||
| INSERT INTO user_role (id,role_id,user_id) | ||
| VALUES ('2', '3', '1'); | ||
| INSERT INTO user_role (id,role_id,user_id) | ||
| VALUES ('3', '1', '2'); | ||
| INSERT INTO user_role (id,role_id,user_id) | ||
| VALUES ('4', '2', '3'); | ||
| INSERT INTO user_role (id,role_id,user_id) | ||
| VALUES ('5', '4', '4'); | ||
| INSERT INTO user_role (id,role_id,user_id) | ||
| VALUES ('6', '1', '1'); |
93 changes: 93 additions & 0 deletions
93
...a/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/OneManyResultMapTest.java
This file contains hidden or 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,93 @@ | ||
| /** | ||
| * Copyright 2009-2019 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 | ||
| * | ||
| * http://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.annotion_many_one_add_resultmapid; | ||
|
|
||
| import org.apache.ibatis.BaseDataTest; | ||
| 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.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.io.Reader; | ||
| import java.util.List; | ||
|
|
||
| class OneManyResultMapTest { | ||
|
|
||
| private static SqlSessionFactory sqlSessionFactory; | ||
|
|
||
| @BeforeAll | ||
| static void setUp() throws Exception { | ||
| // create an SqlSessionFactory | ||
| try (Reader reader = Resources | ||
| .getResourceAsReader("org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml")) { | ||
| sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
| } | ||
|
|
||
| // populate in-memory database | ||
| BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), | ||
| "org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldUseResultMapWithMany() { | ||
| try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
| UserDao mapper = sqlSession.getMapper(UserDao.class); | ||
| List<User> users = mapper.findAll(); | ||
| assertNotNull(users); | ||
| assertEquals(4, users.size()); | ||
| assertEquals(2, users.get(0).getRoles().size()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldUseResultMapInXmlWithMany() { | ||
| try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
| UserDao mapper = sqlSession.getMapper(UserDao.class); | ||
| List<User> users = mapper.findAll2(); | ||
| assertNotNull(users); | ||
| assertEquals(4, users.size()); | ||
| assertEquals(2, users.get(0).getRoles().size()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldUseResultMapWithOne() { | ||
| try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
| UserDao mapper = sqlSession.getMapper(UserDao.class); | ||
| List<User> users = mapper.findAll3(); | ||
| assertNotNull(users); | ||
| assertEquals(2, users.size()); | ||
| assertNotNull(users.get(0).getRole()); | ||
| assertEquals("teacher", users.get(0).getRole().getRoleName()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldResolveResultMapInTheSameNamespace() { | ||
| try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
| UserDao mapper = sqlSession.getMapper(UserDao.class); | ||
| User headmaster = mapper.findHeadmaster(); | ||
| assertNotNull(headmaster); | ||
| assertEquals(3, headmaster.getTeachers().size()); | ||
| assertEquals("Doug Lea", headmaster.getTeachers().get(0).getUsername()); | ||
| } | ||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/Role.java
This file contains hidden or 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,46 @@ | ||
| /** | ||
| * Copyright 2009-2019 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 | ||
| * | ||
| * http://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.annotion_many_one_add_resultmapid; | ||
|
|
||
| public class Role { | ||
| private Integer id; | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Role{" + | ||
| "id=" + id + | ||
| ", roleName='" + roleName + '\'' + | ||
| '}'; | ||
| } | ||
|
|
||
| private String roleName; | ||
|
|
||
| public Integer getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Integer id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getRoleName() { | ||
| return roleName; | ||
| } | ||
|
|
||
| public void setRoleName(String roleName) { | ||
| this.roleName = roleName; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.java
This file contains hidden or 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,34 @@ | ||
| /** | ||
| * Copyright 2009-2019 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
moonService marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid; | ||
|
|
||
| import org.apache.ibatis.annotations.Result; | ||
| import org.apache.ibatis.annotations.Results; | ||
| import org.apache.ibatis.annotations.Select; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * @author lvyang | ||
| */ | ||
| public interface RoleDao { | ||
| @Select("select * from role") | ||
| @Results(id = "roleMap1", value = { | ||
| @Result(id = true, column = "role_id", property = "id"), | ||
| @Result(column = "role_name", property = "roleName") | ||
| }) | ||
| public List<Role> findAll(); | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.xml
This file contains hidden or 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,29 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
|
|
||
| Copyright 2009-2019 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 | ||
|
|
||
| http://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" | ||
| "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
| <mapper | ||
| namespace="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao"> | ||
| <resultMap id="roleMap2" | ||
| type="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.Role"> | ||
| <id column="role_id" property="id" /> | ||
| <result column="role_name" property="roleName" /> | ||
| </resultMap> | ||
| </mapper> |
41 changes: 41 additions & 0 deletions
41
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml
This file contains hidden or 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-2019 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 | ||
|
|
||
| http://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 configuration | ||
| PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
| "http://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
| <configuration> | ||
| <environments default="development"> | ||
| <environment id="development"> | ||
| <transactionManager type="JDBC" /> | ||
| <dataSource type="UNPOOLED"> | ||
| <property name="driver" value="org.hsqldb.jdbcDriver" /> | ||
| <property name="url" | ||
| value="jdbc:hsqldb:mem:annotion_many_one_add_resultmapid" /> | ||
| <property name="username" value="sa" /> | ||
| </dataSource> | ||
| </environment> | ||
| </environments> | ||
|
|
||
| <mappers> | ||
| <mapper | ||
| class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao" /> | ||
| <mapper | ||
| class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.UserDao" /> | ||
| </mappers> | ||
| </configuration> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.