Skip to content

Commit dd8d87e

Browse files
committed
Polishing
- Applied formatter. - Renamed tests to clarify intention. - Removed redundant comments. - Added license headers.
1 parent 6945700 commit dd8d87e

File tree

11 files changed

+286
-171
lines changed

11 files changed

+286
-171
lines changed

src/main/java/org/apache/ibatis/annotations/Many.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
@Target({})
3535
public @interface Many {
3636
/**
37-
* Returns the id that retrieves result map.
37+
* Returns the result map id used to map collection.
3838
*
39-
* @return the id that retrieves result map.
39+
* @return the result map id
4040
*/
4141
String resultMap() default "";
42+
4243
/**
4344
* Returns the statement id that retrieves collection.
4445
*

src/main/java/org/apache/ibatis/annotations/One.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
@Target({})
3535
public @interface One {
3636
/**
37-
* Returns the id that retrieves result map.
37+
* Returns the result map id used to map single object.
3838
*
39-
* @return the id that retrieves result map.
39+
* @return the result map id
4040
*/
4141
String resultMap() default "";
42+
4243
/**
4344
* Returns the statement id that retrieves single object.
4445
*

src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
--
2+
-- Copyright 2009-2019 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
117
-- ----------------------------
218
-- Table structure for role
319
-- ----------------------------

src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/MyBatisTest.java

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
17+
18+
import org.apache.ibatis.BaseDataTest;
19+
import org.apache.ibatis.io.Resources;
20+
import org.apache.ibatis.session.SqlSession;
21+
import org.apache.ibatis.session.SqlSessionFactory;
22+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
import java.io.Reader;
29+
import java.util.List;
30+
31+
class OneManyResultMapTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeAll
36+
static void setUp() throws Exception {
37+
// create an SqlSessionFactory
38+
try (Reader reader = Resources
39+
.getResourceAsReader("org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml")) {
40+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41+
}
42+
43+
// populate in-memory database
44+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45+
"org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql");
46+
}
47+
48+
@Test
49+
void shouldUseResultMapWithMany() {
50+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51+
UserDao mapper = sqlSession.getMapper(UserDao.class);
52+
List<User> users = mapper.findAll();
53+
assertNotNull(users);
54+
assertEquals(4, users.size());
55+
assertEquals(2, users.get(0).getRoles().size());
56+
}
57+
}
58+
59+
@Test
60+
void shouldUseResultMapInXmlWithMany() {
61+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62+
UserDao mapper = sqlSession.getMapper(UserDao.class);
63+
List<User> users = mapper.findAll2();
64+
assertNotNull(users);
65+
assertEquals(4, users.size());
66+
assertEquals(2, users.get(0).getRoles().size());
67+
}
68+
}
69+
70+
@Test
71+
void shouldUseResultMapWithOne() {
72+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73+
UserDao mapper = sqlSession.getMapper(UserDao.class);
74+
List<User> users = mapper.findAll3();
75+
assertNotNull(users);
76+
assertEquals(2, users.size());
77+
assertNotNull(users.get(0).getRole());
78+
assertEquals("teacher", users.get(0).getRole().getRoleName());
79+
}
80+
}
81+
82+
@Test
83+
void shouldResolveResultMapInTheSameNamespace() {
84+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85+
UserDao mapper = sqlSession.getMapper(UserDao.class);
86+
User headmaster = mapper.findHeadmaster();
87+
assertNotNull(headmaster);
88+
assertEquals(3, headmaster.getTeachers().size());
89+
assertEquals("Doug Lea", headmaster.getTeachers().get(0).getUsername());
90+
}
91+
}
92+
93+
}

src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/Role.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
217

318
public class Role {
4-
private Integer id; //int
19+
private Integer id;
520

621
@Override
722
public String toString() {
823
return "Role{" +
9-
"id=" + id +
10-
", roleName='" + roleName + '\'' +
11-
'}';
24+
"id=" + id +
25+
", roleName='" + roleName + '\'' +
26+
'}';
1227
}
1328

14-
private String roleName;//varchar
29+
private String roleName;
1530

1631
public Integer getId() {
1732
return id;
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
217

318
import org.apache.ibatis.annotations.Result;
@@ -7,21 +22,13 @@
722
import java.util.List;
823

924
/**
10-
* @Description:
11-
* @Author: lvyang
12-
* @Created Date: 2019年12月10日
13-
* @LastModifyDate:
14-
* @LastModifyBy:
15-
* @Version:
25+
* @author lvyang
1626
*/
1727
public interface RoleDao {
1828
@Select("select * from role")
19-
@Results(
20-
id="roleMap1",
21-
value = {
22-
@Result(id = true,column="role_id",property = "id"),
23-
@Result(column="role_name",property = "roleName")
24-
}
25-
)
29+
@Results(id = "roleMap1", value = {
30+
@Result(id = true, column = "role_id", property = "id"),
31+
@Result(column = "role_name", property = "roleName")
32+
})
2633
public List<Role> findAll();
2734
}
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
219
<!DOCTYPE mapper
320
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
421
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5-
<mapper namespace="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao">
6-
<resultMap id="roleMap2" type="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.Role">
7-
<id column="role_id" property="id"/>
8-
<result column="role_name" property="roleName" />
22+
<mapper
23+
namespace="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao">
24+
<resultMap id="roleMap2"
25+
type="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.Role">
26+
<id column="role_id" property="id" />
27+
<result column="role_name" property="roleName" />
928
</resultMap>
1029
</mapper>
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
219
<!DOCTYPE configuration
320
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
421
"http://mybatis.org/dtd/mybatis-3-config.dtd">
522
<configuration>
623
<environments default="development">
724
<environment id="development">
8-
<transactionManager type="JDBC"/>
25+
<transactionManager type="JDBC" />
926
<dataSource type="UNPOOLED">
1027
<property name="driver" value="org.hsqldb.jdbcDriver" />
11-
<property name="url" value="jdbc:hsqldb:mem:annotion_many_one_add_resultmapid" />
28+
<property name="url"
29+
value="jdbc:hsqldb:mem:annotion_many_one_add_resultmapid" />
1230
<property name="username" value="sa" />
1331
</dataSource>
1432
</environment>
1533
</environments>
1634

17-
<mappers>
18-
<mapper class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao"/>
19-
<mapper class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.UserDao"/>
20-
</mappers>
35+
<mappers>
36+
<mapper
37+
class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao" />
38+
<mapper
39+
class="org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.UserDao" />
40+
</mappers>
2141
</configuration>

0 commit comments

Comments
 (0)