forked from tuguangquan/mybatis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mybatis/mybatis-3.git
- Loading branch information
Showing
8 changed files
with
347 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
language: java | ||
jdk: | ||
- oraclejdk8 | ||
- oraclejdk7 | ||
- openjdk7 | ||
- openjdk6 | ||
|
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
61 changes: 61 additions & 0 deletions
61
src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/Account.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,61 @@ | ||
/*- | ||
* Copyright 2009-2014 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.nestedresulthandler_association; | ||
|
||
import java.util.Date; | ||
|
||
public class Account { | ||
private String accountUuid; | ||
|
||
private String accountName; | ||
|
||
private Date birthDate; | ||
|
||
private AccountAddress address; | ||
|
||
public String getAccountUuid() { | ||
return accountUuid; | ||
} | ||
|
||
public void setAccountUuid(String accountUuid) { | ||
this.accountUuid = accountUuid; | ||
} | ||
|
||
public String getAccountName() { | ||
return accountName; | ||
} | ||
|
||
public void setAccountName(String accountName) { | ||
this.accountName = accountName; | ||
} | ||
|
||
public Date getBirthDate() { | ||
return birthDate; | ||
} | ||
|
||
public void setBirthDate(Date birthDate) { | ||
this.birthDate = birthDate; | ||
} | ||
|
||
public AccountAddress getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(AccountAddress address) { | ||
this.address = address; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountAddress.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,49 @@ | ||
/*- | ||
* Copyright 2009-2014 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.nestedresulthandler_association; | ||
|
||
public class AccountAddress { | ||
private String accountUuid; | ||
|
||
private String zipCode; | ||
|
||
private String address; | ||
|
||
public String getAccountUuid() { | ||
return accountUuid; | ||
} | ||
|
||
public void setAccountUuid(String accountUuid) { | ||
this.accountUuid = accountUuid; | ||
} | ||
|
||
public String getZipCode() { | ||
return zipCode; | ||
} | ||
|
||
public void setZipCode(String zipCode) { | ||
this.zipCode = zipCode; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...st/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountRepository.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,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2009-2014 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.nestedresulthandler_association.AccountRepository"> | ||
<resultMap id="resultMapAccount" type="org.apache.ibatis.submitted.nestedresulthandler_association.Account"> | ||
<id property="accountUuid" column="account_uuid" /> | ||
<result property="accountName" column="account_name" /> | ||
<result property="birthDate" column="birth_date" /> | ||
<association property="address" javaType="org.apache.ibatis.submitted.nestedresulthandler_association.AccountAddress"> | ||
<id property="accountUuid" column="account_uuid" /> | ||
<result property="zipCode" column="zip_code" /> | ||
<result property="address" column="address" /> | ||
</association> | ||
</resultMap> | ||
<select id="collectPageByBirthMonth" resultMap="resultMapAccount" resultOrdered="true"> | ||
<![CDATA[ | ||
SELECT | ||
a.account_uuid | ||
,a.account_name | ||
,a.birth_date | ||
,aa.zip_code | ||
,aa.address | ||
FROM | ||
t_account a | ||
LEFT OUTER JOIN | ||
t_account_address aa | ||
ON aa.account_uuid = a.account_uuid | ||
WHERE | ||
a.birth_date >= #{birthMonth} AND a.birth_date < ADD_MONTHS(#{birthMonth}, 1) | ||
ORDER BY | ||
a.account_uuid | ||
]]> | ||
</select> | ||
</mapper> |
37 changes: 37 additions & 0 deletions
37
src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/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,37 @@ | ||
-- | ||
-- Copyright 2009-2014 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. | ||
-- | ||
|
||
DROP TABLE t_account_address if exists; | ||
DROP TABLE t_account if exists; | ||
CREATE TABLE t_account( | ||
account_uuid CHAR(36) PRIMARY KEY | ||
,account_name VARCHAR(256) | ||
,birth_date DATETIME | ||
); | ||
CREATE TABLE t_account_address( | ||
account_uuid CHAR(36) PRIMARY KEY | ||
,zip_code CHAR(7) | ||
,address VARCHAR(256) | ||
); | ||
|
||
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc01', 'Bob1', '2014-01-01 00:00:00.000'); | ||
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc02', 'Bob2', '2014-01-02 00:00:00.000'); | ||
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc03', 'Bob3', '2014-01-03 00:00:00.000'); | ||
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc04', 'Bob4', '2014-01-04 00:00:00.000'); | ||
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc05', 'Bob5', '2014-01-05 00:00:00.000'); | ||
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc11', 'Mark1', '2014-02-01 00:00:00.000'); | ||
|
||
INSERT INTO t_account_address (account_uuid,zip_code,address) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc02', '1710051', 'Tokyo Toshimaku Nagasaki'); |
106 changes: 106 additions & 0 deletions
106
.../ibatis/submitted/nestedresulthandler_association/NestedResultHandlerAssociationTest.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,106 @@ | ||
/* | ||
* Copyright 2009-2014 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.nestedresulthandler_association; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.Reader; | ||
import java.sql.Connection; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.session.ResultContext; | ||
import org.apache.ibatis.session.ResultHandler; | ||
import org.apache.ibatis.session.RowBounds; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class NestedResultHandlerAssociationTest { | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
// create an SqlSessionFactory | ||
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml"); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
reader.close(); | ||
|
||
// populate in-memory database | ||
SqlSession session = sqlSessionFactory.openSession(); | ||
Connection conn = session.getConnection(); | ||
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql"); | ||
ScriptRunner runner = new ScriptRunner(conn); | ||
runner.setLogWriter(null); | ||
runner.runScript(reader); | ||
reader.close(); | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void shouldHandleRowBounds() throws Exception { | ||
SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); | ||
Date targetMonth = fmt.parse("2014-01-01"); | ||
final List<Account> accounts = new ArrayList<Account>(); | ||
try { | ||
sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), new ResultHandler() { | ||
@Override | ||
public void handleResult(ResultContext context) { | ||
Account account = (Account) context.getResultObject(); | ||
accounts.add(account); | ||
} | ||
}); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
assertEquals(2, accounts.size()); | ||
assertEquals("Bob2", accounts.get(0).getAccountName()); | ||
assertEquals("Bob3", accounts.get(1).getAccountName()); | ||
} | ||
|
||
@Test | ||
public void shouldHandleStop() throws Exception { | ||
SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); | ||
final List<Account> accounts = new ArrayList<Account>(); | ||
try { | ||
Date targetMonth = fmt.parse("2014-01-01"); | ||
sqlSession.select("collectPageByBirthMonth", targetMonth, new ResultHandler() { | ||
@Override | ||
public void handleResult(ResultContext context) { | ||
Account account = (Account) context.getResultObject(); | ||
accounts.add(account); | ||
if (accounts.size() > 1) | ||
context.stop(); | ||
} | ||
}); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
assertEquals(2, accounts.size()); | ||
assertEquals("Bob1", accounts.get(0).getAccountName()); | ||
assertEquals("Bob2", accounts.get(1).getAccountName()); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.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,40 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
Copyright 2009-2014 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"> | ||
<property name="" value="" /> | ||
</transactionManager> | ||
<dataSource type="UNPOOLED"> | ||
<property name="driver" value="org.hsqldb.jdbcDriver" /> | ||
<property name="url" value="jdbc:hsqldb:mem:nestedresulthandler_association" /> | ||
<property name="username" value="sa" /> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
|
||
<mappers> | ||
<mapper resource="org/apache/ibatis/submitted/nestedresulthandler_association/AccountRepository.xml" /> | ||
</mappers> | ||
|
||
</configuration> |