Skip to content

Commit bb2c6d3

Browse files
committed
test
1 parent 608e840 commit bb2c6d3

3 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.selectkey;
17+
18+
import org.apache.ibatis.annotations.*;
19+
20+
import java.util.Map;
21+
22+
public interface AnnotatedMapper {
23+
24+
@Insert("insert into table2 (name) values(#{name})")
25+
@SelectKey(statement = "call identity()", keyProperty = "nameId", before = false, resultType = int.class)
26+
int insertTable2(Name name);
27+
28+
@Insert("insert into table2 (name) values(#{name})")
29+
@Options(useGeneratedKeys = true, keyProperty = "nameId,generatedName", keyColumn = "ID,NAME_FRED")
30+
int insertTable2WithGeneratedKey(Name name);
31+
32+
int insertTable2WithGeneratedKeyXml(Name name);
33+
34+
@Insert("insert into table2 (name) values(#{name})")
35+
@SelectKey(statement = "select id, name_fred from table2 where id = identity()", keyProperty = "nameId,generatedName", keyColumn = "ID,NAME_FRED", before = false, resultType = Map.class)
36+
int insertTable2WithSelectKeyWithKeyMap(Name name);
37+
38+
int insertTable2WithSelectKeyWithKeyMapXml(Name name);
39+
40+
@Insert("insert into table2 (name) values(#{name})")
41+
@SelectKey(statement = "select id as nameId, name_fred as generatedName from table2 where id = identity()", keyProperty = "nameId,generatedName", before = false, resultType = Name.class)
42+
int insertTable2WithSelectKeyWithKeyObject(Name name);
43+
44+
int insertTable2WithSelectKeyWithKeyObjectXml(Name name);
45+
46+
@Insert("insert into table3 (id, name) values(#{nameId}, #{name})")
47+
@SelectKey(statement = "call next value for TestSequence", keyProperty = "nameId", before = true, resultType = int.class)
48+
int insertTable3(Name name);
49+
50+
@InsertProvider(type = SqlProvider.class, method = "insertTable3_2")
51+
@SelectKey(statement = "call next value for TestSequence", keyProperty = "nameId", before = true, resultType = int.class)
52+
int insertTable3_2(Name name);
53+
54+
@Update("update table2 set name = #{name} where id = #{nameId}")
55+
@Options(useGeneratedKeys = true, keyProperty = "generatedName")
56+
int updateTable2WithGeneratedKey(Name name);
57+
58+
int updateTable2WithGeneratedKeyXml(Name name);
59+
60+
@Update("update table2 set name = #{name} where id = #{nameId}")
61+
@SelectKey(statement = "select name_fred from table2 where id = #{nameId}", keyProperty = "generatedName", keyColumn = "NAME_FRED", before = false, resultType = String.class)
62+
int updateTable2WithSelectKeyWithKeyMap(Name name);
63+
64+
int updateTable2WithSelectKeyWithKeyMapXml(Name name);
65+
66+
@Update("update table2 set name = #{name} where id = #{nameId}")
67+
@SelectKey(statement = "select name_fred as generatedName from table2 where id = #{nameId}", keyProperty = "generatedName", before = false, resultType = Name.class)
68+
int updateTable2WithSelectKeyWithKeyObject(Name name);
69+
70+
int updateTable2WithSelectKeyWithKeyObjectXml(Name name);
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.custom_collection_handling;
17+
18+
import org.apache.ibatis.reflection.ReflectionException;
19+
import org.apache.ibatis.reflection.factory.ObjectFactory;
20+
21+
import java.lang.reflect.Array;
22+
import java.lang.reflect.Constructor;
23+
import java.util.*;
24+
25+
public class CustomObjectFactory implements ObjectFactory {
26+
27+
@Override
28+
public <T> T create(Class<T> type) {
29+
return create(type, null, null);
30+
}
31+
32+
@Override
33+
public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
34+
Class<?> classToCreate = resolveInterface(type);
35+
@SuppressWarnings("unchecked") // we know types are assignable
36+
T created = (T) instantiateClass(classToCreate, constructorArgTypes, constructorArgs);
37+
return created;
38+
}
39+
40+
private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
41+
try {
42+
Constructor<T> constructor;
43+
if (constructorArgTypes == null || constructorArgs == null) {
44+
constructor = type.getDeclaredConstructor();
45+
if (!constructor.isAccessible()) {
46+
constructor.setAccessible(true);
47+
}
48+
return constructor.newInstance();
49+
}
50+
constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
51+
if (!constructor.isAccessible()) {
52+
constructor.setAccessible(true);
53+
}
54+
return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
55+
} catch (Exception e) {
56+
StringBuilder argTypes = new StringBuilder();
57+
if (constructorArgTypes != null) {
58+
for (Class<?> argType : constructorArgTypes) {
59+
argTypes.append(argType.getSimpleName());
60+
argTypes.append(",");
61+
}
62+
}
63+
StringBuilder argValues = new StringBuilder();
64+
if (constructorArgs != null) {
65+
for (Object argValue : constructorArgs) {
66+
argValues.append(argValue);
67+
argValues.append(",");
68+
}
69+
}
70+
throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
71+
}
72+
}
73+
74+
private Class<?> resolveInterface(Class<?> type) {
75+
Class<?> classToCreate;
76+
if (type == List.class || type == Collection.class) {
77+
classToCreate = LinkedList.class;
78+
} else if (type == Map.class) {
79+
classToCreate = LinkedHashMap.class;
80+
} else if (type == SortedSet.class) { // issue #510 Collections Support
81+
classToCreate = TreeSet.class;
82+
} else if (type == Set.class) {
83+
classToCreate = HashSet.class;
84+
} else {
85+
classToCreate = type;
86+
}
87+
return classToCreate;
88+
}
89+
90+
@Override
91+
public <T> boolean isCollection(Class<T> type) {
92+
return CustomCollection.class.isAssignableFrom(type);
93+
}
94+
95+
@SuppressWarnings("unchecked")
96+
public <T> T[] createArray(Class<T> type, int size) {
97+
return (T[]) Array.newInstance(type, size);
98+
}
99+
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2009-2015 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.cacheorder;
17+
18+
import java.io.Serializable;
19+
20+
public class User implements Serializable {
21+
22+
private static final long serialVersionUID = 2636291819488700444L;
23+
private Integer id;
24+
private String name;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
}

0 commit comments

Comments
 (0)