Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
- Applied formatter.
- Renamed tests to clarify intention.
- Removed redundant comments.
- Added license headers.
  • Loading branch information
harawata committed Dec 27, 2019
1 parent 6945700 commit dd8d87e
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 171 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/apache/ibatis/annotations/Many.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/apache/ibatis/annotations/One.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
-- ----------------------------
Expand Down

This file was deleted.

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());
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Role> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,10 +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" />
<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>
Original file line number Diff line number Diff line change
@@ -1,21 +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"/>
<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="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>
<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>
Loading

0 comments on commit dd8d87e

Please sign in to comment.