-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3128 from epochcoder/feat/custom-sql-session-usin…
…g-defaults feat: allow `DefaultSqlSessionFactory` to provide a custom `SqlSession`
- Loading branch information
Showing
5 changed files
with
137 additions
and
3 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
27 changes: 27 additions & 0 deletions
27
src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSession.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-2024 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 | ||
* | ||
* https://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.session.defaults; | ||
|
||
import org.apache.ibatis.executor.Executor; | ||
import org.apache.ibatis.session.Configuration; | ||
|
||
public class ExtendedSqlSession extends DefaultSqlSession { | ||
|
||
public ExtendedSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { | ||
super(configuration, executor, autoCommit); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactory.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,32 @@ | ||
/* | ||
* Copyright 2009-2024 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 | ||
* | ||
* https://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.session.defaults; | ||
|
||
import org.apache.ibatis.executor.Executor; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSession; | ||
|
||
public class ExtendedSqlSessionFactory extends DefaultSqlSessionFactory { | ||
|
||
public ExtendedSqlSessionFactory(Configuration configuration) { | ||
super(configuration); | ||
} | ||
|
||
@Override | ||
protected SqlSession createSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { | ||
return new ExtendedSqlSession(configuration, executor, autoCommit); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryBuilder.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,28 @@ | ||
/* | ||
* Copyright 2009-2024 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 | ||
* | ||
* https://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.session.defaults; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
|
||
public class ExtendedSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder { | ||
|
||
@Override | ||
public SqlSessionFactory build(Configuration config) { | ||
return new ExtendedSqlSessionFactory(config); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.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,43 @@ | ||
/* | ||
* Copyright 2009-2024 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 | ||
* | ||
* https://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.session.defaults; | ||
|
||
import java.io.Reader; | ||
|
||
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.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
public class ExtendedSqlSessionFactoryTest extends BaseDataTest { | ||
|
||
@Test | ||
public void testCanUseExtendedSqlSessionUsingDefaults() throws Exception { | ||
createBlogDataSource(); | ||
|
||
final String resource = "org/apache/ibatis/builder/MapperConfig.xml"; | ||
final Reader reader = Resources.getResourceAsReader(resource); | ||
|
||
SqlSessionFactory sessionFactory = new ExtendedSqlSessionFactoryBuilder().build(reader); | ||
Assertions.assertThat(sessionFactory).isNotNull(); | ||
|
||
try (SqlSession sqlSession = sessionFactory.openSession()) { | ||
Assertions.assertThat(sqlSession).isNotNull().isInstanceOf(ExtendedSqlSession.class); | ||
} | ||
} | ||
} |