Skip to content

Commit 93c84f4

Browse files
committed
1 parent 0a66341 commit 93c84f4

File tree

8 files changed

+332
-0
lines changed

8 files changed

+332
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.apache.ibatis.submitted.parent_childs;
2+
3+
public class Child {
4+
5+
private int id;
6+
private String name;
7+
private String surName;
8+
private int age;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
public void setId(int id) {
14+
this.id = id;
15+
}
16+
public String getName() {
17+
return name;
18+
}
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
public String getSurName() {
23+
return surName;
24+
}
25+
public void setSurName(String surName) {
26+
this.surName = surName;
27+
}
28+
public int getAge() {
29+
return age;
30+
}
31+
public void setAge(int age) {
32+
this.age = age;
33+
}
34+
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--
2+
-- Copyright 2009-2012 The MyBatis Team
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+
17+
drop table users if exists;
18+
19+
create table Parent (
20+
id int,
21+
Name varchar(20),
22+
SurName varchar(20)
23+
);
24+
25+
create table Child (
26+
id int,
27+
Name varchar(20),
28+
SurName varchar(20),
29+
Age int,
30+
ParentId varchar(20)
31+
);
32+
33+
insert into Parent values (1, 'Jose', 'Garcia');
34+
insert into Parent values (2, 'Juan', 'Perez');
35+
36+
insert into Child values (1, 'Ana', 'Garcia', 1, 1);
37+
insert into Child values (2, 'Rosa', 'Garcia', 4, 1);
38+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2009-2012 The MyBatis Team
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.parent_childs;
17+
18+
import java.util.List;
19+
20+
public interface Mapper {
21+
22+
List<Parent> getParents();
23+
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2013 The MyBatis Team
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE mapper
18+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
21+
<mapper namespace="org.apache.ibatis.submitted.parent_childs.Mapper">
22+
23+
<resultMap id="ParentMap" type="org.apache.ibatis.submitted.parent_childs.Parent">
24+
<id column="p_Id" property="id" />
25+
<id column="p_Name" property="name" />
26+
<id column="p_SurName" property="surName" />
27+
<collection property="childs" resultMap="ChildMap" />
28+
</resultMap>
29+
30+
<resultMap id="ChildMap" type="org.apache.ibatis.submitted.parent_childs.Child">
31+
<id column="c_id" property="id" />
32+
<id column="c_Name" property="name" />
33+
<id column="c_SurName" property="surName" />
34+
<id column="c_Age" property="age" />
35+
</resultMap>
36+
37+
<select id="getParents" resultMap="ParentMap">
38+
select
39+
p.Id as p_Id,
40+
p.Name as p_Name,
41+
p.SurName as p_SurName,
42+
c.Id as c_Id,
43+
c.Name as c_Name,
44+
c.SurName as c_SurName,
45+
c.Age as c_Age
46+
from Parent p
47+
left outer join Child c on p.Id = c.ParentId
48+
</select>
49+
</mapper>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.apache.ibatis.submitted.parent_childs;
2+
3+
import java.util.List;
4+
5+
public class Parent {
6+
7+
private int id;
8+
private String name;
9+
private String surName;
10+
private List<Child> childs;
11+
12+
public int getId() {
13+
return id;
14+
}
15+
public void setId(int id) {
16+
this.id = id;
17+
}
18+
public String getName() {
19+
return name;
20+
}
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
public String getSurName() {
25+
return surName;
26+
}
27+
public void setSurName(String surName) {
28+
this.surName = surName;
29+
}
30+
public List<Child> getChilds() {
31+
return childs;
32+
}
33+
public void setChilds(List<Child> childs) {
34+
this.childs = childs;
35+
}
36+
37+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2009-2012 The MyBatis Team
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.parent_childs;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
import java.util.List;
21+
22+
import org.apache.ibatis.io.Resources;
23+
import org.apache.ibatis.jdbc.ScriptRunner;
24+
import org.apache.ibatis.session.SqlSession;
25+
import org.apache.ibatis.session.SqlSessionFactory;
26+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.junit.Assert;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class ParentChildTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeClass
36+
public static void setUp() throws Exception {
37+
// create a SqlSessionFactory
38+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/parent_childs/mybatis-config.xml");
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
reader.close();
41+
42+
// populate in-memory database
43+
SqlSession session = sqlSessionFactory.openSession();
44+
Connection conn = session.getConnection();
45+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/parent_childs/CreateDB.sql");
46+
ScriptRunner runner = new ScriptRunner(conn);
47+
runner.setLogWriter(null);
48+
runner.runScript(reader);
49+
reader.close();
50+
session.close();
51+
}
52+
53+
@Test
54+
public void shouldGetAUser() {
55+
SqlSession sqlSession = sqlSessionFactory.openSession();
56+
try {
57+
Mapper mapper = sqlSession.getMapper(Mapper.class);
58+
List<Parent> parents = mapper.getParents();
59+
Assert.assertEquals(2, parents.size());
60+
Parent fistParent = parents.get(0);
61+
Assert.assertEquals("Jose", fistParent.getName());
62+
Assert.assertEquals(2, fistParent.getChilds().size());
63+
Parent secondParent = parents.get(1);
64+
Assert.assertEquals("Juan", secondParent.getName());
65+
Assert.assertEquals(0, secondParent.getChilds().size());
66+
} finally {
67+
sqlSession.close();
68+
}
69+
}
70+
71+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2012 The MyBatis Team
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.parent_childs;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2012 The MyBatis Team
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC">
26+
<property name="" value="" />
27+
</transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:parent_childs" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper resource="org/apache/ibatis/submitted/parent_childs/Mapper.xml" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)