-
Notifications
You must be signed in to change notification settings - Fork 48
Add MyBatis integration module #1921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a1cce1d
Apply PR thread fixes for MyBatis factory, test, and docs wording
Copilot e506b74
Merge origin/7.0.x and resolve MyBatis conflict reverts
Copilot 765c396
Reapply MyBatis module and docs entries after base merge
Copilot 073884b
correct
graemerocher d91d8b6
Update to micronaut-core 5.0.3 (#2023)
radovanradic 7d98cbc
[skip ci] Release v7.0.2
micronaut-build e307d60
chore: Bump version to 7.0.3-SNAPSHOT
micronaut-build 778ed94
Merge branch '7.0.x' into feat/mybatis-support-815
sdelamo 6c409ae
Merge remote-tracking branch 'origin/7.1.x' into feat/mybatis-support…
sdelamo 0f368dc
add logback.xml to mybatis
sdelamo e7faa7d
binaryCompatibility after 7.1.0
sdelamo 7f95cf0
Fix MyBatis factory bean resolution
sdelamo 9a2f0cf
Update
sdelamo 608e6b2
Merge branch '7.1.x' into feat/mybatis-support-815
sdelamo 96137e3
guides example
sdelamo 83dce1e
fix docs
sdelamo 606b952
Move mappers to example.micronaut.mappers
sdelamo d9c737c
fix binary compatibility
sdelamo 500d2c6
remove duplicated
sdelamo 7e5c08f
use kotlin DSL
sdelamo 24cc743
Update mybatis/src/main/java/io/micronaut/configuration/mybatis/packa…
sdelamo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,25 @@ | ||
| plugins { | ||
| id("io.micronaut.build.internal.sql-module") | ||
| } | ||
| dependencies { | ||
| api(projects.micronautJdbc) | ||
| api(mn.micronaut.context) | ||
| api(mn.micronaut.inject) | ||
| api(libs.managed.mybatis) | ||
| testAnnotationProcessor(mn.micronaut.inject.java) | ||
| testImplementation(mnTest.micronaut.test.junit5) | ||
| testRuntimeOnly(projects.micronautJdbcHikari) | ||
| testRuntimeOnly(libs.managed.h2) | ||
| testRuntimeOnly(mnTest.junit.jupiter.engine) | ||
| testRuntimeOnly(mnTest.junit.platform.launcher) | ||
| } | ||
|
|
||
| tasks.withType<Test> { | ||
| useJUnitPlatform() | ||
| } | ||
|
|
||
| micronautBuild { | ||
| binaryCompatibility { | ||
| enabledAfter("7.1.0") | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
mybatis/src/main/java/io/micronaut/configuration/mybatis/MyBatisConfigurationCustomizer.java
This file contains hidden or 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,35 @@ | ||
| /* | ||
| * Copyright 2017-2026 original 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 io.micronaut.configuration.mybatis; | ||
|
|
||
| import org.apache.ibatis.session.Configuration; | ||
|
|
||
| /** | ||
| * Allows custom actions to be performed on a MyBatis {@link Configuration}. | ||
| * Customizers may be annotated with {@link jakarta.inject.Named} to target a specific datasource. | ||
| * | ||
| * @author Graeme Rocher | ||
| * @since 7.1.0 | ||
| */ | ||
| public interface MyBatisConfigurationCustomizer { | ||
|
|
||
| /** | ||
| * Performs custom configuration operations on the given MyBatis configuration. | ||
| * | ||
| * @param configuration The configuration to customize | ||
| */ | ||
| void customize(Configuration configuration); | ||
| } |
92 changes: 92 additions & 0 deletions
92
mybatis/src/main/java/io/micronaut/configuration/mybatis/MyBatisFactory.java
This file contains hidden or 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,92 @@ | ||
| /* | ||
| * Copyright 2017-2026 original 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 io.micronaut.configuration.mybatis; | ||
|
|
||
| import io.micronaut.context.BeanLocator; | ||
| import io.micronaut.context.annotation.Bean; | ||
| import io.micronaut.context.annotation.EachBean; | ||
| import io.micronaut.context.annotation.Factory; | ||
| import io.micronaut.context.annotation.Parameter; | ||
| import io.micronaut.inject.qualifiers.Qualifiers; | ||
| import org.apache.ibatis.mapping.Environment; | ||
| import org.apache.ibatis.session.Configuration; | ||
| import org.apache.ibatis.session.SqlSessionFactory; | ||
| import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
| import org.apache.ibatis.session.SqlSessionManager; | ||
| import org.apache.ibatis.transaction.TransactionFactory; | ||
| import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import javax.sql.DataSource; | ||
|
|
||
| /** | ||
| * Configures MyBatis beans from Micronaut {@link DataSource} beans. | ||
| * | ||
| * @author Graeme Rocher | ||
| * @since 7.1.0 | ||
| */ | ||
| @Factory | ||
| public class MyBatisFactory { | ||
|
|
||
| /** | ||
| * Creates the MyBatis {@link Configuration} for a datasource. | ||
| * | ||
| * @param name The datasource name | ||
| * @param dataSource The datasource | ||
| * @param beanLocator The bean locator | ||
| * @param transactionFactory Transaction Factory | ||
| * @return The MyBatis configuration | ||
| */ | ||
| @EachBean(DataSource.class) | ||
| public Configuration myBatisConfiguration( | ||
| @Parameter String name, | ||
| @Parameter DataSource dataSource, | ||
| @Parameter @Nullable TransactionFactory transactionFactory, | ||
| BeanLocator beanLocator) { | ||
| TransactionFactory resolvedTransactionFactory = transactionFactory != null ? transactionFactory : new JdbcTransactionFactory(); | ||
| Configuration configuration = new Configuration(new Environment(name, resolvedTransactionFactory, dataSource)); | ||
| for (MyBatisConfigurationCustomizer customizer : beanLocator.getBeansOfType( | ||
| MyBatisConfigurationCustomizer.class, | ||
| Qualifiers.byName(name) | ||
| )) { | ||
| customizer.customize(configuration); | ||
| } | ||
| return configuration; | ||
| } | ||
|
|
||
| /** | ||
| * Creates the MyBatis {@link SqlSessionFactory} for a datasource. | ||
| * | ||
| * @param configuration The MyBatis configuration | ||
| * @return The session factory | ||
| */ | ||
| @EachBean(Configuration.class) | ||
| public SqlSessionFactory sqlSessionFactory(Configuration configuration) { | ||
| return new SqlSessionFactoryBuilder().build(configuration); | ||
| } | ||
|
|
||
| /** | ||
| * Creates the thread-safe {@link SqlSessionManager} for a datasource. | ||
| * | ||
| * @param sqlSessionFactory The session factory | ||
| * @return The session manager | ||
| */ | ||
| @EachBean(SqlSessionFactory.class) | ||
| @Bean(typed = SqlSessionManager.class) | ||
| public SqlSessionManager sqlSessionManager(SqlSessionFactory sqlSessionFactory) { | ||
| return SqlSessionManager.newInstance(sqlSessionFactory); | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
mybatis/src/main/java/io/micronaut/configuration/mybatis/package-info.java
This file contains hidden or 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 2017-2026 original 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. | ||
| */ | ||
| /** | ||
| * Configuration for MyBatis integration. | ||
| * | ||
| * @author Graeme Rocher | ||
| * @since 7.1.0 | ||
| */ | ||
| @Configuration | ||
| @Requires(classes = SqlSessionFactory.class) | ||
| package io.micronaut.configuration.mybatis; | ||
|
|
||
| import io.micronaut.context.annotation.Configuration; | ||
| import io.micronaut.context.annotation.Requires; | ||
| import org.apache.ibatis.session.SqlSessionFactory; |
108 changes: 108 additions & 0 deletions
108
mybatis/src/test/java/io/micronaut/configuration/mybatis/MyBatisFactoryTest.java
This file contains hidden or 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,108 @@ | ||
| /* | ||
| * Copyright 2017-2026 original 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 io.micronaut.configuration.mybatis; | ||
|
|
||
| import io.micronaut.context.ApplicationContext; | ||
| import io.micronaut.inject.qualifiers.Qualifiers; | ||
| import io.micronaut.configuration.mybatis.support.TestTransactionFactory; | ||
| import org.apache.ibatis.session.Configuration; | ||
| import org.apache.ibatis.session.SqlSessionFactory; | ||
| import org.apache.ibatis.session.SqlSessionManager; | ||
| import org.apache.ibatis.transaction.TransactionFactory; | ||
| import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.sql.Connection; | ||
| import java.sql.Statement; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class MyBatisFactoryTest { | ||
|
|
||
| @Test | ||
| void doesNotCreateBeansWithoutDataSource() { | ||
| try (ApplicationContext applicationContext = ApplicationContext.builder("test").start()) { | ||
| assertFalse(applicationContext.containsBean(DataSource.class)); | ||
| assertFalse(applicationContext.containsBean(Configuration.class)); | ||
| assertFalse(applicationContext.containsBean(SqlSessionFactory.class)); | ||
| assertFalse(applicationContext.containsBean(SqlSessionManager.class)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void createsMyBatisBeansForTheDefaultDataSource() throws Exception { | ||
| try (ApplicationContext applicationContext = ApplicationContext.builder("test") | ||
| .properties(Map.of("datasources.default", Map.of(), "spec.name", "MyBatisFactoryTest")) | ||
| .start()) { | ||
| assertTrue(applicationContext.containsBean(DataSource.class)); | ||
| assertTrue(applicationContext.containsBean(Configuration.class)); | ||
| assertTrue(applicationContext.containsBean(SqlSessionFactory.class)); | ||
| assertTrue(applicationContext.containsBean(SqlSessionManager.class)); | ||
|
|
||
| Configuration configuration = applicationContext.getBean(Configuration.class); | ||
| assertTrue(configuration.hasMapper(TestMapper.class)); | ||
| assertTrue(configuration.isMapUnderscoreToCamelCase()); | ||
| assertInstanceOf(TestTransactionFactory.class, configuration.getEnvironment().getTransactionFactory()); | ||
| assertSame( | ||
| applicationContext.getBean(TransactionFactory.class, Qualifiers.byName("default")), | ||
| configuration.getEnvironment().getTransactionFactory() | ||
| ); | ||
|
|
||
| SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class); | ||
| assertNotNull(sqlSessionFactory.getConfiguration().getEnvironment()); | ||
| assertEquals("default", sqlSessionFactory.getConfiguration().getEnvironment().getId()); | ||
|
|
||
| initializeSchema(applicationContext.getBean(DataSource.class)); | ||
|
|
||
| SqlSessionManager sqlSessionManager = applicationContext.getBean(SqlSessionManager.class); | ||
| TestMapper mapper = sqlSessionManager.getMapper(TestMapper.class); | ||
| assertEquals(1, mapper.count()); | ||
| mapper.insert(); | ||
| assertEquals(2, mapper.count()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void ignoresWronglyQualifiedBeansAndFallsBackToJdbcTransactions() { | ||
| try (ApplicationContext applicationContext = ApplicationContext.builder("test") | ||
| .properties(Map.of("datasources.default2", Map.of())) | ||
| .start()) { | ||
| assertTrue(applicationContext.containsBean(DataSource.class)); | ||
| assertTrue(applicationContext.containsBean(Configuration.class)); | ||
| assertTrue(applicationContext.containsBean(SqlSessionFactory.class)); | ||
| assertTrue(applicationContext.containsBean(SqlSessionManager.class)); | ||
|
|
||
| Configuration configuration = applicationContext.getBean(Configuration.class); | ||
| assertFalse(configuration.hasMapper(TestMapper.class)); | ||
| assertInstanceOf(JdbcTransactionFactory.class, configuration.getEnvironment().getTransactionFactory()); | ||
| } | ||
| } | ||
|
|
||
| private static void initializeSchema(DataSource dataSource) throws Exception { | ||
| try (Connection connection = dataSource.getConnection(); | ||
| Statement statement = connection.createStatement()) { | ||
| statement.execute("CREATE TABLE foo(id INT PRIMARY KEY)"); | ||
| statement.execute("INSERT INTO foo(id) VALUES(1)"); | ||
|
graemerocher marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
mybatis/src/test/java/io/micronaut/configuration/mybatis/TestMapper.java
This file contains hidden or 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 2017-2026 original 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 io.micronaut.configuration.mybatis; | ||
|
|
||
| import org.apache.ibatis.annotations.Insert; | ||
| import org.apache.ibatis.annotations.Select; | ||
|
|
||
| interface TestMapper { | ||
|
|
||
| @Select("SELECT COUNT(*) FROM foo") | ||
| int count(); | ||
|
|
||
| @Insert("INSERT INTO foo(id) VALUES(2)") | ||
| void insert(); | ||
| } |
31 changes: 31 additions & 0 deletions
31
.../src/test/java/io/micronaut/configuration/mybatis/TestMyBatisConfigurationCustomizer.java
This file contains hidden or 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,31 @@ | ||
| /* | ||
| * Copyright 2017-2026 original 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 io.micronaut.configuration.mybatis; | ||
|
|
||
| import jakarta.inject.Named; | ||
| import jakarta.inject.Singleton; | ||
| import org.apache.ibatis.session.Configuration; | ||
|
|
||
| @Named("default") | ||
| @Singleton | ||
| final class TestMyBatisConfigurationCustomizer implements MyBatisConfigurationCustomizer { | ||
|
|
||
| @Override | ||
| public void customize(Configuration configuration) { | ||
| configuration.addMapper(TestMapper.class); | ||
| configuration.setMapUnderscoreToCamelCase(true); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.