Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add columnPrefix to @One and @Many #1829

Merged
merged 5 commits into from Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/Many.java
Expand Up @@ -33,6 +33,13 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Many {
/**
* Returns the columnPrefix.
*
* @return the columnPrefix.
*/
String columnPrefix() default "";

/**
* Returns the result map id used to map collection.
*
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/One.java
Expand Up @@ -33,6 +33,13 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface One {
/**
* Returns the columnPrefix.
*
* @return the columnPrefix.
*/
String columnPrefix() default "";

/**
* Returns the result map id used to map single object.
*
Expand Down
Expand Up @@ -545,16 +545,17 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
@SuppressWarnings("unchecked")
Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>)
((result.typeHandler() == UnknownTypeHandler.class) ? null : result.typeHandler());
boolean hasNestedResultMap = hasNestedResultMap(result);
ResultMapping resultMapping = assistant.buildResultMapping(
resultType,
nullOrEmpty(result.property()),
nullOrEmpty(result.column()),
result.javaType() == void.class ? null : result.javaType(),
result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(),
hasNestedSelect(result) ? nestedSelectId(result) : null,
hasNestedResultMap(result) ? nestedResultMapId(result) : null,
null,
hasNestedResultMap ? nestedResultMapId(result) : null,
null,
hasNestedResultMap ? findColumnPrefix(result) : null,
typeHandler,
flags,
null,
Expand All @@ -564,6 +565,14 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
}
}

private String findColumnPrefix(Result result) {
String columnPrefix = result.one().columnPrefix();
if (columnPrefix.length() < 1) {
columnPrefix = result.many().columnPrefix();
}
return columnPrefix;
}

private String nestedResultMapId(Result result) {
String resultMapId = result.one().resultMap();
if (resultMapId.length() < 1) {
Expand Down
@@ -0,0 +1,77 @@
--
-- 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
-- ----------------------------
CREATE TABLE role (
id int,
role_name varchar(10)
);

-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO role (id,role_name)
VALUES ('1', 'teacher');
INSERT INTO role (id,role_name)
VALUES ('2', 'student');
INSERT INTO role (id,role_name)
VALUES ('3', 'Headmaster');
INSERT INTO role (id,role_name)
VALUES ('4', 'Learning-commissary');

CREATE TABLE user (
id int,
username varchar(32),
);

-- ----------------------------
-- 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)
VALUES ('4', 'Juergen Hoeller');

-- ----------------------------
-- 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');
@@ -0,0 +1,66 @@
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 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_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().getRoleName());
}
}
}
@@ -0,0 +1,31 @@
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='" + roleName + '\'' +
'}';
}

private String roleName;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getRoleName() {
return roleName;
}

public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
@@ -0,0 +1,20 @@
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 org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.Role;

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 = "roleName")
})
public List<Role> findAll();
}
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020 itheima.com All Rights Reserved.FileName: RoleDao.xml@author: lvyang@date: 20-2-6 上午8:38
-->
harawata marked this conversation as resolved.
Show resolved Hide resolved
<!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="roleName" />
</resultMap>
</mapper>
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright (c) 2020 itheima.com All Rights Reserved.FileName: SqlMapConfig.xml@author: lvyang@date: 20-2-6 上午8:37
-->
harawata marked this conversation as resolved.
Show resolved Hide resolved
<!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" />
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url"
value="jdbc:hsqldb:mem:annotion_many_one_add_columnprefix" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper
class="org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.RoleDao" />
<mapper
class="org.apache.ibatis.submitted.annotion_many_one_add_columnprefix.UserDao" />
</mappers>
</configuration>
@@ -0,0 +1,60 @@
package org.apache.ibatis.submitted.annotion_many_one_add_columnprefix;

import java.util.List;

public class User {
private Integer id;
private String username;
private List<User> teachers;
private Role role;
private List<Role> roles;

public Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", roles=" + roles +
'}';
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public List<Role> getRoles() {
return roles;
}

public void setRoles(List<Role> roles) {
this.roles = roles;
}

public List<User> getTeachers() {
return teachers;
}

public void setTeachers(List<User> teachers) {
this.teachers = teachers;
}
}