diff --git a/src/main/java/org/apache/ibatis/annotations/Many.java b/src/main/java/org/apache/ibatis/annotations/Many.java index f09aafbd80b..564809a0870 100644 --- a/src/main/java/org/apache/ibatis/annotations/Many.java +++ b/src/main/java/org/apache/ibatis/annotations/Many.java @@ -34,11 +34,12 @@ @Target({}) public @interface Many { /** - * Returns the id that retrieves result map. + * Returns the result map id used to map collection. * - * @return the id that retrieves result map. + * @return the result map id */ String resultMap() default ""; + /** * Returns the statement id that retrieves collection. * diff --git a/src/main/java/org/apache/ibatis/annotations/One.java b/src/main/java/org/apache/ibatis/annotations/One.java index ff6d12b1c4c..e2b27143f93 100644 --- a/src/main/java/org/apache/ibatis/annotations/One.java +++ b/src/main/java/org/apache/ibatis/annotations/One.java @@ -34,11 +34,12 @@ @Target({}) public @interface One { /** - * Returns the id that retrieves result map. + * Returns the result map id used to map single object. * - * @return the id that retrieves result map. + * @return the result map id */ String resultMap() default ""; + /** * Returns the statement id that retrieves single object. * diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql index 060a9035238..c6841bdbc0d 100644 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql @@ -1,3 +1,19 @@ +-- +-- 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 -- ---------------------------- diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/MyBatisTest.java b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/MyBatisTest.java deleted file mode 100644 index 8cd9d3cdd92..00000000000 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/MyBatisTest.java +++ /dev/null @@ -1,77 +0,0 @@ -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.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.io.Reader; -import java.util.List; - -class MyBatisTest { - - 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 testFindAll() { - try (SqlSession sqlSession = sqlSessionFactory.openSession()){ - UserDao mapper = sqlSession.getMapper(UserDao.class); - List users = mapper.findAll(); - Assertions.assertNotNull(users, "users must not be null"); - Assertions.assertEquals(4, users.size(), "should return 4 results"); - Assertions.assertEquals(2, users.get(0).getRoles().size(), "should have 2 roles"); - } - } - - @Test - void testFindAll2() { - try (SqlSession sqlSession = sqlSessionFactory.openSession()){ - UserDao mapper = sqlSession.getMapper(UserDao.class); - List users = mapper.findAll2(); - Assertions.assertNotNull(users, "users must not be null"); - Assertions.assertEquals(4, users.size(), "should return 4 results"); - Assertions.assertEquals(2, users.get(0).getRoles().size(), "should have 2 roles"); - } - } - - @Test - void testFindAll3() { - try (SqlSession sqlSession = sqlSessionFactory.openSession()){ - UserDao mapper = sqlSession.getMapper(UserDao.class); - List users = mapper.findAll3(); - Assertions.assertNotNull(users, "users must not be null"); - Assertions.assertEquals(2, users.size(), "should return 2 results"); - Assertions.assertNotNull(users.get(0).getRole(), "role must not be null"); - Assertions.assertEquals("teacher", users.get(0).getRole().getRoleName(), "roleName should be teacher"); - } - } - - - @Test - void testFindHeadmaster() { - try (SqlSession sqlSession = sqlSessionFactory.openSession()){ - UserDao mapper = sqlSession.getMapper(UserDao.class); - User headmaster = mapper.findHeadmaster(); - Assertions.assertNotNull(headmaster, "users must not be null"); - Assertions.assertEquals(3, headmaster.getTeachers().size(), "should return 3 results"); - Assertions.assertEquals("Doug Lea", headmaster.getTeachers().get(0).getUsername(), "userName should be lisi"); - } - } - -} diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/OneManyResultMapTest.java b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/OneManyResultMapTest.java new file mode 100644 index 00000000000..623f9876d8c --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/OneManyResultMapTest.java @@ -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 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 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 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()); + } + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/Role.java b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/Role.java index 2aec879eb69..8cfd2eb091f 100644 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/Role.java +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/Role.java @@ -1,17 +1,32 @@ +/** + * 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; //int + private Integer id; @Override public String toString() { return "Role{" + - "id=" + id + - ", roleName='" + roleName + '\'' + - '}'; + "id=" + id + + ", roleName='" + roleName + '\'' + + '}'; } - private String roleName;//varchar + private String roleName; public Integer getId() { return id; diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.java b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.java index 01c26355749..dbed8568347 100644 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.java +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.java @@ -1,3 +1,18 @@ +/** + * 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.annotations.Result; @@ -7,21 +22,13 @@ import java.util.List; /** - * @Description: - * @Author: lvyang - * @Created Date: 2019年12月10日 - * @LastModifyDate: - * @LastModifyBy: - * @Version: + * @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") - } - ) + @Results(id = "roleMap1", value = { + @Result(id = true, column = "role_id", property = "id"), + @Result(column = "role_name", property = "roleName") + }) public List findAll(); } diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.xml b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.xml index 1ffe77395d2..3b748d6afa2 100644 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.xml +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/RoleDao.xml @@ -1,10 +1,29 @@ + - - - - + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml index 6f5f46a22c7..277b1872fe8 100644 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml @@ -1,21 +1,41 @@ + - + - + - - - - + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/User.java b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/User.java index f2d4b066cd6..835bca7a612 100644 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/User.java +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/User.java @@ -1,11 +1,25 @@ +/** + * 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 java.util.List; public class User { - private Integer id; //int - private String username; //varchar + private Integer id; + private String username; private List teachers; private Role role; private List roles; @@ -18,14 +32,13 @@ public void setRole(Role role) { this.role = role; } - @Override public String toString() { return "User{" + - "id=" + id + - ", username='" + username + '\'' + - ", roles=" + roles + - '}'; + "id=" + id + + ", username='" + username + '\'' + + ", roles=" + roles + + '}'; } public Integer getId() { @@ -51,6 +64,7 @@ public List getRoles() { public void setRoles(List roles) { this.roles = roles; } + public List getTeachers() { return teachers; } diff --git a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/UserDao.java b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/UserDao.java index af09e96ed4e..af3266771e7 100644 --- a/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/UserDao.java +++ b/src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/UserDao.java @@ -1,3 +1,18 @@ +/** + * 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.annotations.*; @@ -5,73 +20,64 @@ import java.util.List; /** - * @Description: - * @Author: lvyang - * @Created Date: 2019年12月10日 - * @LastModifyDate: - * @LastModifyBy: - * @Version: + * @author lvyang */ public interface UserDao { - @Select("select\n" + - " u.id,u.username,r.id role_id,r.role_name\n" + - " from user u\n" + - " left join user_role ur on u.id =ur.user_id\n" + - " left join role r on ur.role_id = r.id") - @Results({ - @Result(id = true,column="id",property = "id"), - @Result(column="username",property = "username"), - @Result(property = "roles",many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap1")) - }) - public List findAll(); + @Select({ "select", + " u.id, u.username, r.id role_id, r.role_name", + " from user u", + " left join user_role ur on u.id = ur.user_id", + " left join role r on ur.role_id = r.id" }) + @Results({ + @Result(id = true, column = "id", property = "id"), + @Result(column = "username", property = "username"), + @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap1")) + }) + public List findAll(); - @Select("select\n" + - " u.id,u.username,r.id role_id,r.role_name\n" + - " from user u\n" + - " left join user_role ur on u.id =ur.user_id\n" + - " left join role r on ur.role_id = r.id") + @Select({ "select", + " u.id, u.username, r.id role_id, r.role_name", + " from user u", + " left join user_role ur on u.id = ur.user_id", + " left join role r on ur.role_id = r.id" }) @Results({ - @Result(id = true,column="id",property = "id"), - @Result(column="username",property = "username"), - @Result(property = "roles",many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2")) + @Result(id = true, column = "id", property = "id"), + @Result(column = "username", property = "username"), + @Result(property = "roles", many = @Many(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2")) }) public List findAll2(); - @Select("select\n" + - " u.id,u.username,r.id role_id,r.role_name\n" + - " from user u\n" + - " left join user_role ur on u.id =ur.user_id\n" + - " left join role r on ur.role_id = r.id where u.id in (2,3)") - @Results( - { - @Result(id = true,column="id",property = "id"), - @Result(column="username",property = "username"), - @Result(property = "role",one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2")) + @Select({ "select", + " u.id, u.username, r.id role_id, r.role_name", + " from user u", + " left join user_role ur on u.id = ur.user_id", + " left join role r on ur.role_id = r.id where u.id in (2, 3)" }) + @Results({ + @Result(id = true, column = "id", property = "id"), + @Result(column = "username", property = "username"), + @Result(property = "role", one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2")) }) public List findAll3(); - @Select("select id teacher_id,username teacher_name from user") - @Results( - id = "userMap", - value = { - @Result(id = true,column="teacher_id",property = "id"), - @Result(column="teacher_name",property = "username"), + @Select("select id teacher_id, username teacher_name from user") + @Results(id = "userMap", value = { + @Result(id = true, column = "teacher_id", property = "id"), + @Result(column = "teacher_name", property = "username") }) public List justUseResult(); - @Select("select\n" + - "u.id,u.username,r.id role_id,r.role_name,ut.id teacher_id,ut.username teacher_name\n" + - "from user u\n" + - "left join user_role ur on u.id =ur.user_id\n" + - "left join role r on ur.role_id = r.id\n" + - "left join user ut on ut.id != u.id\n" + - "where role_id = 3" - ) + @Select({ "select", + "u.id, u.username, r.id role_id, r.role_name, ut.id teacher_id, ut.username teacher_name", + "from user u", + "left join user_role ur on u.id = ur.user_id", + "left join role r on ur.role_id = r.id", + "left join user ut on ut.id != u.id", + "where role_id = 3" }) @Results({ - @Result(id = true,column="id",property = "id"), - @Result(column="username",property = "username"), - @Result(property = "role",one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2")), - @Result(property = "teachers",many = @Many(resultMap = "userMap")), - }) + @Result(id = true, column = "id", property = "id"), + @Result(column = "username", property = "username"), + @Result(property = "role", one = @One(resultMap = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2")), + @Result(property = "teachers", many = @Many(resultMap = "userMap")) + }) public User findHeadmaster(); }