Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd9f082

Browse files
committed
1 parent f0b7f16 commit fd9f082

File tree

9 files changed

+326
-0
lines changed

9 files changed

+326
-0
lines changed

‎gh-1239/pom.xml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.mybatis.issues</groupId>
5+
<artifactId>gh-1239</artifactId>
6+
<packaging>jar</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.apache.logging.log4j</groupId>
17+
<artifactId>log4j-core</artifactId>
18+
<version>2.7</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.12</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.hsqldb</groupId>
28+
<artifactId>hsqldb</artifactId>
29+
<version>2.3.4</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.mybatis</groupId>
34+
<artifactId>mybatis</artifactId>
35+
<version>3.4.6</version>
36+
</dependency>
37+
</dependencies>
38+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright 2009-2015 the original author or authors. Licensed under the Apache License,
3+
* Version 2.0 (the "License"); you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless
5+
* required by applicable law or agreed to in writing, software distributed under the License is
6+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
7+
* or implied. See the License for the specific language governing permissions and limitations
8+
* under the License.
9+
*/
10+
11+
package test;
12+
13+
public interface Mapper {
14+
User getUser(Integer id);
15+
16+
void insertUser(User user);
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package test;
2+
3+
import org.apache.ibatis.cache.CacheKey;
4+
import org.apache.ibatis.executor.Executor;
5+
import org.apache.ibatis.mapping.BoundSql;
6+
import org.apache.ibatis.mapping.MappedStatement;
7+
import org.apache.ibatis.plugin.*;
8+
import org.apache.ibatis.reflection.MetaObject;
9+
import org.apache.ibatis.reflection.SystemMetaObject;
10+
import org.apache.ibatis.session.ResultHandler;
11+
import org.apache.ibatis.session.RowBounds;
12+
13+
import java.util.Properties;
14+
15+
@Intercepts(
16+
@Signature(
17+
type = Executor.class,
18+
method = "query",
19+
//Cannot intercept the query method of 6 parameters now
20+
//This PR can solve this problem
21+
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
22+
//The current version can only intercept the following method
23+
//args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
24+
)
25+
)
26+
public class QueryInterceptor implements Interceptor {
27+
@Override
28+
public Object intercept(Invocation invocation) throws Throwable {
29+
Object[] args = invocation.getArgs();
30+
//It is often necessary to use BoundSql in the interceptor, which is very inconvenient now.
31+
BoundSql boundSql = (BoundSql) args[5];
32+
//This is just a simple example and has no practical meaning.
33+
String newSql = boundSql.getSql().replace("*", "id, concat('test-', name) as name");
34+
MetaObject metaObject = SystemMetaObject.forObject(boundSql);
35+
//replace sql
36+
metaObject.setValue("sql", newSql);
37+
38+
return invocation.proceed();
39+
}
40+
41+
@Override
42+
public Object plugin(Object target) {
43+
return Plugin.wrap(target, this);
44+
}
45+
46+
@Override
47+
public void setProperties(Properties properties) {
48+
49+
}
50+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2009-2015 the original author or authors.
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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 test;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.sql.Connection;
22+
23+
import org.apache.ibatis.io.Resources;
24+
import org.apache.ibatis.jdbc.ScriptRunner;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.Assert;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class SimpleTest {
33+
34+
private static SqlSessionFactory sqlSessionFactory;
35+
36+
@BeforeClass
37+
public static void setUp() throws Exception {
38+
// create an SqlSessionFactory
39+
Reader reader = Resources.getResourceAsReader("test/mybatis-config.xml");
40+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41+
reader.close();
42+
43+
// populate in-memory database
44+
SqlSession session = sqlSessionFactory.openSession();
45+
Connection conn = session.getConnection();
46+
reader = Resources.getResourceAsReader("test/CreateDB.sql");
47+
ScriptRunner runner = new ScriptRunner(conn);
48+
runner.setLogWriter(null);
49+
runner.runScript(reader);
50+
reader.close();
51+
session.close();
52+
}
53+
54+
@Test
55+
public void shouldGetAUser() {
56+
SqlSession sqlSession = sqlSessionFactory.openSession();
57+
try {
58+
Mapper mapper = sqlSession.getMapper(Mapper.class);
59+
User user = mapper.getUser(1);
60+
Assert.assertEquals("test-User1", user.getName());
61+
} finally {
62+
sqlSession.close();
63+
}
64+
}
65+
66+
}

‎gh-1239/src/test/java/test/User.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2009-2015 the original author or authors.
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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 test;
17+
18+
public class User {
19+
private Integer id;
20+
private String name;
21+
22+
public Integer getId() {
23+
return id;
24+
}
25+
26+
public void setId(Integer id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="stdout" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%5p [%t] - %m%n" charset="utf-8" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="trace">
10+
<AppenderRef ref="stdout" />
11+
</Root>
12+
</Loggers>
13+
</Configuration>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Copyright 2009-2016 the original author or authors.
3+
--
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+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
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+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20)
22+
);
23+
24+
insert into users (id, name) values
25+
(1, 'User1'),
26+
(2, 'User2');
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2016 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
23+
<mapper namespace="test.Mapper">
24+
25+
<select id="getUser" resultType="test.User">
26+
select * from users where id = #{id}
27+
</select>
28+
29+
<insert id="insertUser">
30+
insert into users (id, name) values (#{id}, #{name})
31+
</insert>
32+
33+
</mapper>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2016 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<plugins>
26+
<plugin interceptor="test.QueryInterceptor"></plugin>
27+
</plugins>
28+
29+
<environments default="development">
30+
<environment id="development">
31+
<transactionManager type="JDBC">
32+
<property name="" value="" />
33+
</transactionManager>
34+
<dataSource type="UNPOOLED">
35+
<property name="driver" value="org.hsqldb.jdbcDriver" />
36+
<property name="url" value="jdbc:hsqldb:mem:mybatisissues" />
37+
<property name="username" value="sa" />
38+
</dataSource>
39+
</environment>
40+
</environments>
41+
42+
<mappers>
43+
<mapper class="test.Mapper" />
44+
</mappers>
45+
46+
</configuration>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /