Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1829 from moonService/dev/@one_@many_add_columnpr…
- Loading branch information
Showing
11 changed files
with
528 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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
78 changes: 78 additions & 0 deletions
78
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_columnprefix/CreateDB.sql
This file contains 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,78 @@ | ||
-- | ||
-- Copyright 2009-2020 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, | ||
name varchar(30) | ||
); | ||
|
||
-- ---------------------------- | ||
-- Records of role | ||
-- ---------------------------- | ||
INSERT INTO role (id,name) | ||
VALUES ('1', 'teacher'); | ||
INSERT INTO role (id,name) | ||
VALUES ('2', 'student'); | ||
INSERT INTO role (id,name) | ||
VALUES ('3', 'Headmaster'); | ||
INSERT INTO role (id,name) | ||
VALUES ('4', 'Learning-commissary'); | ||
|
||
CREATE TABLE user ( | ||
id int, | ||
username varchar(32), | ||
friend_id int | ||
); | ||
|
||
-- ---------------------------- | ||
-- 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, friend_id) | ||
VALUES ('4', 'Juergen Hoeller', 1); | ||
|
||
-- ---------------------------- | ||
-- 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'); |
97 changes: 97 additions & 0 deletions
97
...g/apache/ibatis/submitted/annotion_many_one_add_columnprefix/OneManyColumnPrefixTest.java
This file contains 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,97 @@ | ||
/** | ||
* Copyright 2009-2020 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_columnprefix; | ||
|
||
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 OneManyColumnPrefixTest { | ||
|
||
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_columnprefix/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_columnprefix/CreateDB.sql"); | ||
} | ||
|
||
@Test | ||
void shouldUseColumnPrefixWithMany() { | ||
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 shouldUseColumnPrefixInXmlWithMany() { | ||
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 shouldUseColumnPrefixWithOne() { | ||
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().getName()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldResolveNestedColumnPrefix() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
UserDao mapper = sqlSession.getMapper(UserDao.class); | ||
User user = mapper.findUserWithFriend(4); | ||
assertEquals(4, user.getId()); | ||
assertEquals(2, user.getRoles().size()); | ||
assertEquals("student", user.getRoles().get(0).getName()); | ||
assertEquals("Learning-commissary", user.getRoles().get(1).getName()); | ||
assertEquals(1, user.getFriend().getId()); | ||
assertEquals(2, user.getFriend().getRoles().size()); | ||
assertEquals("teacher", user.getFriend().getRoles().get(0).getName()); | ||
assertEquals("Headmaster", user.getFriend().getRoles().get(1).getName()); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_columnprefix/Role.java
This file contains 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-2020 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_columnprefix; | ||
|
||
public class Role { | ||
private Integer id; | ||
|
||
@Override | ||
public String toString() { | ||
return "Role{" + | ||
"id=" + id + | ||
", roleName='" + name + '\'' + | ||
'}'; | ||
} | ||
|
||
private String name; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_columnprefix/RoleDao.java
This file contains 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-2020 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_columnprefix; | ||
|
||
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 = "id", property = "id"), | ||
@Result(column = "name", property = "name") | ||
}) | ||
public List<Role> findAll(); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_columnprefix/RoleDao.xml
This file contains 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-2020 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_columnprefix.RoleDao"> | ||
<resultMap id="roleMap2" | ||
type="org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.Role"> | ||
<id column="id" property="id" /> | ||
<result column="name" property="name" /> | ||
</resultMap> | ||
</mapper> |
Oops, something went wrong.