forked from mybatis/mybatis-3
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrating the issue reported in mybatis#902 .
- Loading branch information
Showing
6 changed files
with
237 additions
and
0 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
34 changes: 34 additions & 0 deletions
34
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/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,34 @@ | ||
-- | ||
-- Copyright 2009-2017 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. | ||
-- | ||
|
||
CREATE SCHEMA mbtest; | ||
|
||
CREATE TABLE mbtest.users ( | ||
user_id serial PRIMARY KEY, | ||
name character varying(30) | ||
); | ||
|
||
INSERT INTO mbtest.users (name) values | ||
('Jimmy'); | ||
|
||
|
||
CREATE TABLE mbtest.sections ( | ||
section_id int PRIMARY KEY, | ||
name character varying(30) | ||
); | ||
|
||
INSERT INTO mbtest.sections (section_id, name) values | ||
(1, 'Section 1'); |
27 changes: 27 additions & 0 deletions
27
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.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,27 @@ | ||
/** | ||
* Copyright 2009-2017 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.usesjava8.postgres_genkeys; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Update; | ||
|
||
public interface Mapper { | ||
@Update("update mbtest.users set name = #{name} where user_id = #{userId}") | ||
int updateUserById(@Param("userId") Integer userId, @Param("name") String name); | ||
|
||
@Update("update mbtest.sections set name = #{name} where section_id = #{sectionId}") | ||
int updateSectionById(@Param("sectionId") Integer sectionId, @Param("name") String name); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/MapperConfig.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,43 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
Copyright 2009-2017 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> | ||
<settings> | ||
<setting name="useGeneratedKeys" value="true" /> | ||
</settings> | ||
<environments default="development"> | ||
<environment id="development"> | ||
<transactionManager type="JDBC"> | ||
<property name="" value="" /> | ||
</transactionManager> | ||
<dataSource type="UNPOOLED"> | ||
<property name="driver" value="org.postgresql.Driver" /> | ||
<property name="url" value="jdbc:postgresql:postgres_genkeys" /> | ||
<property name="username" value="postgres" /> | ||
<property name="password" value="root" /> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
<mappers> | ||
<mapper | ||
class="org.apache.ibatis.submitted.usesjava8.postgres_genkeys.Mapper" /> | ||
</mappers> | ||
</configuration> |
89 changes: 89 additions & 0 deletions
89
...ava/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.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,89 @@ | ||
/** | ||
* Copyright 2009-2017 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.usesjava8.postgres_genkeys; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.Reader; | ||
import java.nio.file.Paths; | ||
import java.sql.Connection; | ||
import java.util.Collections; | ||
|
||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; | ||
|
||
public class PostgresGeneratedKeysTest { | ||
|
||
private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
// Launch PostgreSQL server. Download / unarchive if necessary. | ||
postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get("target/postgres")), "localhost", 5432, "postgres_genkeys", "postgres", "root", Collections.emptyList()); | ||
|
||
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/postgres_genkeys/MapperConfig.xml"); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
reader.close(); | ||
|
||
SqlSession session = sqlSessionFactory.openSession(); | ||
Connection conn = session.getConnection(); | ||
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql"); | ||
ScriptRunner runner = new ScriptRunner(conn); | ||
runner.setLogWriter(null); | ||
runner.runScript(reader); | ||
reader.close(); | ||
session.close(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() { | ||
postgres.stop(); | ||
} | ||
|
||
@Test | ||
public void shouldDefaultKeyPropertyNotCauseException() throws Exception { | ||
SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
int result = mapper.updateUserById(1, "Ethan"); | ||
assertEquals(1, result); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldDefaultKeyPropertyNotCauseException2() throws Exception { | ||
SqlSession sqlSession = sqlSessionFactory.openSession(); | ||
try { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
int result = mapper.updateSectionById(1, "IMF"); | ||
assertEquals(1, result); | ||
} finally { | ||
sqlSession.close(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.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,38 @@ | ||
/** | ||
* Copyright 2009-2017 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.usesjava8.postgres_genkeys; | ||
|
||
public class User { | ||
private Integer id; | ||
|
||
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; | ||
} | ||
} |