├── .mvn ├── jvm.config ├── maven.config ├── wrapper │ ├── maven-wrapper.properties │ └── MavenWrapperDownloader.java ├── extensions.xml └── settings.xml ├── .gitattributes ├── src ├── main │ ├── resources │ │ └── META-INF │ │ │ ├── services │ │ │ └── jakarta.enterprise.inject.spi.Extension │ │ │ └── beans.xml │ └── java │ │ └── org │ │ └── mybatis │ │ └── cdi │ │ ├── MybatisCdiConfigurationException.java │ │ ├── Mapper.java │ │ ├── SessionFactoryProvider.java │ │ ├── Isolation.java │ │ ├── SqlSessionManagerRegistry.java │ │ ├── JtaTransactionInterceptor.java │ │ ├── Transactional.java │ │ ├── SerializableMapperProxy.java │ │ ├── CDIUtils.java │ │ ├── MyBatisBean.java │ │ ├── LocalTransactionInterceptor.java │ │ └── MybatisExtension.java ├── site │ ├── resources │ │ ├── images │ │ │ ├── en.png │ │ │ ├── es.png │ │ │ ├── ja.png │ │ │ ├── ko.png │ │ │ └── zh.png │ │ └── css │ │ │ └── site.css │ ├── site_zh.xml │ ├── site.xml │ ├── zh_CN │ │ ├── resources │ │ │ └── css │ │ │ │ └── site.css │ │ └── xdoc │ │ │ ├── index.xml │ │ │ ├── getting-started.xml.vm │ │ │ ├── transactions.xml │ │ │ └── injection.xml │ └── xdoc │ │ ├── index.xml │ │ ├── getting-started.xml.vm │ │ ├── transactions.xml │ │ └── injection.xml └── test │ ├── java │ └── org │ │ └── mybatis │ │ └── cdi │ │ ├── RollbackException.java │ │ ├── UserMapper.java │ │ ├── NoRollbackException.java │ │ ├── UserTransactionProvider.java │ │ ├── User.java │ │ ├── SerializableFooService.java │ │ ├── JtaManager.java │ │ ├── OtherQualifier.java │ │ ├── MySpecialManager.java │ │ ├── FooServiceJTA.java │ │ ├── JtaDatasourceFactory.java │ │ ├── ManagerProducersJta.java │ │ ├── FooService.java │ │ ├── ManagerProducers.java │ │ ├── NarayanaDataSourceWrapper.java │ │ ├── FooServiceJTATest.java │ │ ├── MybatisExtensionTest.java │ │ └── FooServiceTest.java │ └── resources │ ├── org │ └── mybatis │ │ └── cdi │ │ ├── CreateDB_1.sql │ │ ├── CreateDB_2.sql │ │ ├── CreateDB_3.sql │ │ ├── CreateDB_4.sql │ │ ├── CreateDB_JTA.sql │ │ ├── UserMapper.xml │ │ ├── mybatis-config_1.xml │ │ ├── mybatis-config_2.xml │ │ ├── mybatis-config_3.xml │ │ ├── mybatis-config_4.xml │ │ └── mybatis-config_jta.xml │ └── META-INF │ └── beans.xml ├── NOTICE ├── renovate.json ├── .gitignore ├── LICENSE_HEADER ├── format.xml ├── .github └── workflows │ ├── sonatype.yaml │ ├── ci.yaml │ ├── site.yaml │ ├── codeql.yaml │ ├── sonar.yaml │ └── coveralls.yaml ├── README.md ├── mvnw.cmd ├── pom.xml ├── LICENSE └── mvnw /.mvn/jvm.config: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behaviour, in case users don't have core.autocrlf set. 2 | * text=auto 3 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension: -------------------------------------------------------------------------------- 1 | org.mybatis.cdi.MybatisExtension 2 | -------------------------------------------------------------------------------- /src/site/resources/images/en.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mybatis/cdi/master/src/site/resources/images/en.png -------------------------------------------------------------------------------- /src/site/resources/images/es.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mybatis/cdi/master/src/site/resources/images/es.png -------------------------------------------------------------------------------- /src/site/resources/images/ja.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mybatis/cdi/master/src/site/resources/images/ja.png -------------------------------------------------------------------------------- /src/site/resources/images/ko.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mybatis/cdi/master/src/site/resources/images/ko.png -------------------------------------------------------------------------------- /src/site/resources/images/zh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mybatis/cdi/master/src/site/resources/images/zh.png -------------------------------------------------------------------------------- /.mvn/maven.config: -------------------------------------------------------------------------------- 1 | -Daether.checksums.algorithms=SHA-512,SHA-256,SHA-1,MD5 2 | -Daether.connector.smartChecksums=false 3 | --no-transfer-progress 4 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | MyBatis CDI 2 | Copyright 2013-2024 3 | 4 | This product includes software developed by 5 | The MyBatis Team (https://www.mybatis.org/). 6 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.settings 2 | /.classpath 3 | /.idea 4 | /.project 5 | /target 6 | .github/keys/ 7 | nb-configuration.xml 8 | *.log 9 | *.tlog 10 | *.iml 11 | .mvn/wrapper/maven-wrapper.jar 12 | pom.xml.releaseBackup 13 | release.properties 14 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionType=source 2 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.12/apache-maven-3.9.12-bin.zip 3 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.4/maven-wrapper-3.3.4.jar 4 | wrapperVersion=3.3.4 5 | -------------------------------------------------------------------------------- /LICENSE_HEADER: -------------------------------------------------------------------------------- 1 | Copyright ${license.git.copyrightYears} the original author or authors. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /format.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/RollbackException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2022 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 | * https://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 org.mybatis.cdi; 17 | 18 | public class RollbackException extends Exception { 19 | 20 | private static final long serialVersionUID = 1L; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/UserMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2022 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 | * https://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 org.mybatis.cdi; 17 | 18 | @Mapper 19 | public interface UserMapper { 20 | 21 | User getUser(Integer id); 22 | 23 | void insertUser(User user); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/NoRollbackException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2022 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 | * https://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 org.mybatis.cdi; 17 | 18 | public class NoRollbackException extends Exception { 19 | 20 | private static final long serialVersionUID = 1L; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /.mvn/extensions.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | fr.jcgay.maven 22 | maven-profiler 23 | 3.3 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/CreateDB_1.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2013-2022 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 | -- https://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 | CREATE TABLE USERS ( 18 | id integer not null, 19 | name varchar(80) not null 20 | ); 21 | 22 | insert into users (id, name) values(1, '1-User1'); 23 | insert into users (id, name) values(2, '1-User2'); 24 | insert into users (id, name) values(3, '1-User3'); 25 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/CreateDB_2.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2013-2022 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 | -- https://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 | CREATE TABLE USERS ( 18 | id integer not null, 19 | name varchar(80) not null 20 | ); 21 | 22 | insert into users (id, name) values(1, '2-User1'); 23 | insert into users (id, name) values(2, '2-User2'); 24 | insert into users (id, name) values(3, '2-User3'); 25 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/CreateDB_3.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2013-2022 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 | -- https://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 | CREATE TABLE USERS ( 18 | id integer not null, 19 | name varchar(80) not null 20 | ); 21 | 22 | insert into users (id, name) values(1, '3-User1'); 23 | insert into users (id, name) values(2, '3-User2'); 24 | insert into users (id, name) values(3, '3-User3'); 25 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/CreateDB_4.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2013-2022 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 | -- https://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 | CREATE TABLE USERS ( 18 | id integer not null, 19 | name varchar(80) not null 20 | ); 21 | 22 | insert into users (id, name) values(1, '4-User1'); 23 | insert into users (id, name) values(2, '4-User2'); 24 | insert into users (id, name) values(3, '4-User3'); 25 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/CreateDB_JTA.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2013-2023 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 | -- https://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 | CREATE TABLE USERS ( 18 | id integer not null, 19 | name varchar(80) not null, 20 | PRIMARY KEY(id) 21 | ); 22 | 23 | insert into users (id, name) values(1, '1-User1'); 24 | insert into users (id, name) values(2, '1-User2'); 25 | insert into users (id, name) values(3, '1-User3'); 26 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | -------------------------------------------------------------------------------- /src/test/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | -------------------------------------------------------------------------------- /.github/workflows/sonatype.yaml: -------------------------------------------------------------------------------- 1 | name: Sonatype 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | permissions: read-all 9 | 10 | concurrency: 11 | group: ${{ github.workflow }}-${{ github.ref }} 12 | cancel-in-progress: true 13 | 14 | jobs: 15 | build: 16 | if: github.repository_owner == 'mybatis' && ! contains(toJSON(github.event.head_commit.message), '[maven-release-plugin]') 17 | runs-on: ubuntu-latest 18 | timeout-minutes: 30 19 | steps: 20 | - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6 21 | - name: Setup Java 22 | uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # v5 23 | with: 24 | cache: maven 25 | distribution: temurin 26 | java-version: 25 27 | - name: Deploy to Sonatype 28 | run: ./mvnw deploy --batch-mode --no-transfer-progress --settings ./.mvn/settings.xml --show-version -Dlicense.skip=true -DskipTests 29 | env: 30 | CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }} 31 | CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }} 32 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/MybatisCdiConfigurationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2022 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 | * https://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 org.mybatis.cdi; 17 | 18 | /** 19 | * Generic exception. 20 | * 21 | * @author Frank David Martínez 22 | */ 23 | public class MybatisCdiConfigurationException extends RuntimeException { 24 | 25 | private static final long serialVersionUID = 1L; 26 | 27 | public MybatisCdiConfigurationException(String message) { 28 | super(message); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/UserTransactionProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.context.ApplicationScoped; 19 | import jakarta.enterprise.inject.Produces; 20 | import jakarta.transaction.UserTransaction; 21 | 22 | public class UserTransactionProvider { 23 | 24 | @Produces 25 | @ApplicationScoped 26 | public UserTransaction initTX() { 27 | return com.arjuna.ats.jta.UserTransaction.userTransaction(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2022 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 | * https://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 org.mybatis.cdi; 17 | 18 | public class User { 19 | 20 | private Integer id; 21 | private String name; 22 | 23 | public Integer getId() { 24 | return this.id; 25 | } 26 | 27 | public void setId(Integer value) { 28 | this.id = value; 29 | } 30 | 31 | public String getName() { 32 | return this.name; 33 | } 34 | 35 | public void setName(String value) { 36 | this.name = value; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [workflow_dispatch, push, pull_request] 4 | 5 | permissions: read-all 6 | 7 | concurrency: 8 | group: ${{ github.workflow }}-${{ github.ref }} 9 | cancel-in-progress: true 10 | 11 | jobs: 12 | test: 13 | runs-on: ${{ matrix.os }} 14 | timeout-minutes: 30 15 | strategy: 16 | matrix: 17 | cache: [maven] 18 | distribution: [temurin] 19 | java: [21, 25, 26-ea] 20 | os: [macos-latest, ubuntu-latest, windows-latest] 21 | fail-fast: false 22 | max-parallel: 6 23 | name: Test JDK ${{ matrix.java }}, ${{ matrix.os }} 24 | 25 | steps: 26 | - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6 27 | - name: Setup Java ${{ matrix.java }} ${{ matrix.distribution }} 28 | uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # v5 29 | with: 30 | cache: ${{ matrix.cache }} 31 | distribution: ${{ matrix.distribution }} 32 | java-version: ${{ matrix.java }} 33 | - name: Test with Maven 34 | run: ./mvnw test --batch-mode --no-transfer-progress --show-version -D"license.skip=true" 35 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 26 | 27 | 28 | insert into users values(#{id}, #{name}) 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/SerializableFooService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.inject.Inject; 19 | import jakarta.inject.Named; 20 | 21 | import java.io.Serializable; 22 | 23 | /** 24 | * @author Frank D. Martinez [mnesarco] 25 | */ 26 | public class SerializableFooService implements Serializable { 27 | 28 | private static final long serialVersionUID = 1L; 29 | 30 | @Inject 31 | @Named("manager1") 32 | private UserMapper userMapper; 33 | 34 | public User getUser(int userId) { 35 | return this.userMapper.getUser(userId); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /.github/workflows/site.yaml: -------------------------------------------------------------------------------- 1 | name: Site 2 | 3 | on: 4 | push: 5 | branches: 6 | - site 7 | 8 | permissions: 9 | contents: write 10 | 11 | concurrency: 12 | group: ${{ github.workflow }}-${{ github.ref }} 13 | cancel-in-progress: true 14 | 15 | jobs: 16 | build: 17 | if: github.repository_owner == 'mybatis' && ! contains(toJSON(github.event.head_commit.message), '[maven-release-plugin]') 18 | runs-on: ubuntu-latest 19 | timeout-minutes: 60 20 | steps: 21 | - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6 22 | - name: Setup Java 23 | uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # v5 24 | with: 25 | cache: maven 26 | distribution: temurin 27 | java-version: 25 28 | - name: Build site 29 | run: ./mvnw site site:stage --batch-mode --no-transfer-progress --settings ./.mvn/settings.xml --show-version -Dlicense.skip=true -DskipTests 30 | env: 31 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 32 | NVD_API_KEY: ${{ secrets.NVD_API_KEY }} 33 | - name: Deploy Site to gh-pages 34 | uses: JamesIves/github-pages-deploy-action@9d877eea73427180ae43cf98e8914934fe157a1a # v4 35 | with: 36 | branch: gh-pages 37 | folder: target/staging 38 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/Mapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import static java.lang.annotation.ElementType.TYPE; 19 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 20 | 21 | import jakarta.enterprise.inject.Stereotype; 22 | 23 | import java.lang.annotation.Inherited; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * Marker interface for MyBatis mappers. 29 | * 30 | * @author Frank David Martínez 31 | */ 32 | @Stereotype 33 | @Inherited 34 | @Retention(RUNTIME) 35 | @Target({ TYPE }) 36 | public @interface Mapper { 37 | // Interface Mapper 38 | } 39 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yaml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | schedule: 9 | - cron: '43 10 * * 2' 10 | 11 | concurrency: 12 | group: ${{ github.workflow }}-${{ github.ref }} 13 | cancel-in-progress: true 14 | 15 | jobs: 16 | analyze: 17 | name: Analyze 18 | runs-on: 'ubuntu-latest' 19 | timeout-minutes: 30 20 | permissions: 21 | actions: read 22 | contents: read 23 | security-events: write 24 | 25 | steps: 26 | - name: Checkout 27 | uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6 28 | 29 | - name: Setup Java 30 | uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # v5 31 | with: 32 | cache: maven 33 | distribution: 'temurin' 34 | java-version: 25 35 | 36 | - name: Initialize CodeQL 37 | uses: github/codeql-action/init@5d4e8d1aca955e8d8589aabd499c5cae939e33c7 # v4 38 | with: 39 | queries: +security-and-quality 40 | 41 | - name: Autobuild 42 | uses: github/codeql-action/autobuild@5d4e8d1aca955e8d8589aabd499c5cae939e33c7 # v4 43 | 44 | - name: Perform CodeQL Analysis 45 | uses: github/codeql-action/analyze@5d4e8d1aca955e8d8589aabd499c5cae939e33c7 # v4 46 | -------------------------------------------------------------------------------- /src/site/site_zh.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/JtaManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import static java.lang.annotation.ElementType.FIELD; 19 | import static java.lang.annotation.ElementType.METHOD; 20 | import static java.lang.annotation.ElementType.PARAMETER; 21 | import static java.lang.annotation.ElementType.TYPE; 22 | 23 | import jakarta.inject.Qualifier; 24 | 25 | import java.lang.annotation.Retention; 26 | import java.lang.annotation.RetentionPolicy; 27 | import java.lang.annotation.Target; 28 | 29 | @Qualifier 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ METHOD, FIELD, TYPE, PARAMETER }) 32 | public @interface JtaManager { 33 | // JtaManager Interface 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/OtherQualifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import static java.lang.annotation.ElementType.FIELD; 19 | import static java.lang.annotation.ElementType.METHOD; 20 | import static java.lang.annotation.ElementType.PARAMETER; 21 | import static java.lang.annotation.ElementType.TYPE; 22 | 23 | import jakarta.inject.Qualifier; 24 | 25 | import java.lang.annotation.Retention; 26 | import java.lang.annotation.RetentionPolicy; 27 | import java.lang.annotation.Target; 28 | 29 | @Qualifier 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ METHOD, FIELD, TYPE, PARAMETER }) 32 | public @interface OtherQualifier { 33 | // OtherQualifier Interface 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/MySpecialManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import static java.lang.annotation.ElementType.FIELD; 19 | import static java.lang.annotation.ElementType.METHOD; 20 | import static java.lang.annotation.ElementType.PARAMETER; 21 | import static java.lang.annotation.ElementType.TYPE; 22 | 23 | import jakarta.inject.Qualifier; 24 | 25 | import java.lang.annotation.Retention; 26 | import java.lang.annotation.RetentionPolicy; 27 | import java.lang.annotation.Target; 28 | 29 | @Qualifier 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ METHOD, FIELD, TYPE, PARAMETER }) 32 | public @interface MySpecialManager { 33 | // MySpecialManager Interface 34 | } 35 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/SessionFactoryProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 19 | 20 | import jakarta.enterprise.inject.Stereotype; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Inherited; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | /** 28 | * Qualifies an SqlSessionFactory provider method as usable by mybatis-cdi. 29 | * 30 | * @author Frank David Martínez 31 | */ 32 | @Stereotype 33 | @Inherited 34 | @Retention(RUNTIME) 35 | @Target({ ElementType.METHOD }) 36 | public @interface SessionFactoryProvider { 37 | // Session Factory Provider 38 | } 39 | -------------------------------------------------------------------------------- /src/site/zh_CN/resources/css/site.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2023 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 | * https://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 | * when new flags are needed, take them from 18 | * 19 | * https://www.printableworldflags.com/flag-icon 20 | * 21 | * that are free for any kind of usage 22 | */ 23 | 24 | ul.i18n {list-style-type:none;} 25 | li.en {background: url('../../images/en.png') left no-repeat;padding-left: 32px; margin: 10px} 26 | li.es {background: url('../../images/es.png') left no-repeat;padding-left: 32px; margin: 10px} 27 | li.ja {background: url('../../images/ja.png') left no-repeat;padding-left: 32px; margin: 10px} 28 | li.zh {background: url('../../images/zh.png') left no-repeat;padding-left: 32px; margin: 10px} 29 | li.ko {background: url('../../images/ko.png') left no-repeat;padding-left: 32px; margin: 10px} 30 | 31 | -------------------------------------------------------------------------------- /src/site/resources/css/site.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2013-2022 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 | * https://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 | * when new flags are needed, take them from 18 | * 19 | * https://www.printableworldflags.com/flag-icon 20 | * 21 | * that are free for any kind of usage 22 | */ 23 | 24 | ul.i18n {list-style-type:none;} 25 | li.en {background: url('../images/en.png') left no-repeat;padding-left: 32px; margin: 10px} 26 | li.es {background: url('../images/es.png') left no-repeat;padding-left: 32px; margin: 10px} 27 | li.ja {background: url('../images/ja.png') left no-repeat;padding-left: 32px; margin: 10px} 28 | li.fr {background: url('../images/fr.png') left no-repeat;padding-left: 32px; margin: 10px} 29 | li.zh {background: url('../images/zh.png') left no-repeat;padding-left: 32px; margin: 10px} 30 | li.ko {background: url('../images/ko.png') left no-repeat;padding-left: 32px; margin: 10px} 31 | -------------------------------------------------------------------------------- /.github/workflows/sonar.yaml: -------------------------------------------------------------------------------- 1 | name: SonarCloud 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | permissions: read-all 9 | 10 | concurrency: 11 | group: ${{ github.workflow }}-${{ github.ref }} 12 | cancel-in-progress: true 13 | 14 | env: 15 | SONAR_ORGANIZATION: mybatis 16 | SONAR_PROJECT_KEY: cdi 17 | 18 | jobs: 19 | build: 20 | if: github.repository_owner == 'mybatis' 21 | runs-on: ubuntu-latest 22 | timeout-minutes: 30 23 | steps: 24 | - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6 25 | with: 26 | # Disabling shallow clone is recommended for improving relevancy of reporting 27 | fetch-depth: 0 28 | - name: Setup Java 29 | uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # v5 30 | with: 31 | cache: maven 32 | distribution: temurin 33 | java-version: 25 34 | - name: Set SONAR_SCANNER_JAVA_OPTS 35 | run: echo "SONAR_SCANNER_JAVA_OPTS=-Xmx512m" >> ${GITHUB_ENV} 36 | - name: Analyze with SonarCloud 37 | run: ./mvnw verify jacoco:report sonar:sonar --batch-mode --no-transfer-progress --show-version -Dlicense.skip=true -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=${{ env.SONAR_ORGANIZATION }} -Dsonar.projectKey=${{ env.SONAR_ORGANIZATION }}_${{ env.SONAR_PROJECT_KEY }} -Dsonar.scanner.skipJreProvisioning=true -Dsonar.token=${{ env.SONAR_TOKEN }} 38 | env: 39 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 40 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} 41 | -------------------------------------------------------------------------------- /.github/workflows/coveralls.yaml: -------------------------------------------------------------------------------- 1 | name: Coveralls 2 | 3 | on: [push, pull_request] 4 | 5 | permissions: read-all 6 | 7 | concurrency: 8 | group: ${{ github.workflow }}-${{ github.ref }} 9 | cancel-in-progress: true 10 | 11 | jobs: 12 | coveralls: 13 | if: github.repository_owner == 'mybatis' 14 | runs-on: ubuntu-latest 15 | timeout-minutes: 30 16 | steps: 17 | - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6 18 | - name: Setup Java 19 | uses: actions/setup-java@f2beeb24e141e01a676f977032f5a29d81c9e27e # v5 20 | with: 21 | cache: maven 22 | distribution: temurin 23 | java-version: 25 24 | - name: Run the build 25 | run: ./mvnw test --batch-mode --no-transfer-progress --quiet --show-version -Dlicense.skip=true 26 | - name: Report Coverage to Coveralls for Pull Requests 27 | if: github.event_name == 'pull_request' 28 | run: ./mvnw generate-sources jacoco:report coveralls:report --batch-mode --no-transfer-progress -DpullRequest=${{ env.PR_NUMBER }} -DrepoToken=${{ env.GITHUB_TOKEN }} -DserviceName=github 29 | env: 30 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 31 | PR_NUMBER: ${{ github.event.number }} 32 | - name: Report Coverage to Coveralls for General Push 33 | if: github.event_name == 'push' 34 | run: ./mvnw generate-sources jacoco:report coveralls:report --batch-mode --no-transfer-progress -DrepoToken=${{ env.GITHUB_TOKEN }} -DserviceName=github 35 | env: 36 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/FooServiceJTA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.inject.Inject; 19 | import jakarta.interceptor.Interceptors; 20 | 21 | public class FooServiceJTA { 22 | 23 | @Inject 24 | @JtaManager 25 | private UserMapper userMapper; 26 | 27 | @Interceptors(JtaTransactionInterceptor.class) 28 | @Transactional 29 | public User getUserWithNoTransaction(int userId) { 30 | return this.userMapper.getUser(userId); 31 | } 32 | 33 | @Interceptors(JtaTransactionInterceptor.class) 34 | @Transactional 35 | public void insertUserWithTransactional(User user) { 36 | this.userMapper.insertUser(user); 37 | } 38 | 39 | @Interceptors(JtaTransactionInterceptor.class) 40 | @Transactional 41 | public void insertUserWithTransactionalAndFail(User user) { 42 | this.userMapper.insertUser(user); 43 | throw new RuntimeException("fail"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/mybatis-config_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/mybatis-config_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/mybatis-config_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/mybatis-config_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/test/resources/org/mybatis/cdi/mybatis-config_jta.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/JtaDatasourceFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import java.util.Properties; 19 | 20 | import javax.sql.DataSource; 21 | 22 | import org.apache.derby.jdbc.EmbeddedXADataSource; 23 | import org.apache.ibatis.datasource.DataSourceFactory; 24 | 25 | public class JtaDatasourceFactory implements DataSourceFactory { 26 | private final DataSource dataSource; 27 | private final EmbeddedXADataSource embeddedXADataSource; 28 | 29 | public JtaDatasourceFactory() { 30 | this.embeddedXADataSource = new EmbeddedXADataSource(); 31 | this.dataSource = new NarayanaDataSourceWrapper(embeddedXADataSource); 32 | } 33 | 34 | @Override 35 | public DataSource getDataSource() { 36 | return dataSource; 37 | } 38 | 39 | @Override 40 | public void setProperties(final Properties properties) { 41 | embeddedXADataSource.setCreateDatabase(properties.getProperty("createDatabase")); 42 | embeddedXADataSource.setDatabaseName(properties.getProperty("databaseName")); 43 | embeddedXADataSource.setDataSourceName(properties.getProperty("resourceName")); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/Isolation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2022 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 | * https://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 org.mybatis.cdi; 17 | 18 | import org.apache.ibatis.session.TransactionIsolationLevel; 19 | 20 | /** 21 | * Enum of isolation levels. This enum exists because Java annotations do not support null default values - so we need 22 | * to add the DEFAULT level which means - do not specify an isolation level. 23 | * 24 | * @author Jeff Butler 25 | */ 26 | public enum Isolation { 27 | 28 | DEFAULT(null), 29 | 30 | NONE(TransactionIsolationLevel.NONE), 31 | 32 | READ_COMMITTED(TransactionIsolationLevel.READ_COMMITTED), 33 | 34 | READ_UNCOMMITTED(TransactionIsolationLevel.READ_UNCOMMITTED), 35 | 36 | REPEATABLE_READ(TransactionIsolationLevel.REPEATABLE_READ), 37 | 38 | SERIALIZABLE(TransactionIsolationLevel.SERIALIZABLE); 39 | 40 | private final TransactionIsolationLevel transactionIsolationLevel; 41 | 42 | Isolation(TransactionIsolationLevel value) { 43 | this.transactionIsolationLevel = value; 44 | } 45 | 46 | public TransactionIsolationLevel getTransactionIsolationLevel() { 47 | return this.transactionIsolationLevel; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /.mvn/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | central 26 | ${env.CI_DEPLOY_USERNAME} 27 | ${env.CI_DEPLOY_PASSWORD} 28 | 29 | 30 | 31 | 32 | gh-pages-scm 33 | 34 | branch 35 | gh-pages 36 | 37 | 38 | 39 | 40 | 41 | github 42 | ${env.GITHUB_TOKEN} 43 | 44 | 45 | 46 | 47 | nvd 48 | ${env.NVD_API_KEY} 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/ManagerProducersJta.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.context.ApplicationScoped; 19 | import jakarta.enterprise.inject.Produces; 20 | 21 | import java.io.IOException; 22 | import java.io.Reader; 23 | import java.sql.Connection; 24 | 25 | import org.apache.ibatis.io.Resources; 26 | import org.apache.ibatis.jdbc.ScriptRunner; 27 | import org.apache.ibatis.session.SqlSession; 28 | import org.apache.ibatis.session.SqlSessionFactory; 29 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 30 | 31 | public class ManagerProducersJta { 32 | 33 | private SqlSessionFactory createSessionManagerJTA() throws IOException { 34 | SqlSessionFactory manager; 35 | try (Reader reader = Resources.getResourceAsReader("org/mybatis/cdi/mybatis-config_jta.xml")) { 36 | manager = new SqlSessionFactoryBuilder().build(reader); 37 | } 38 | 39 | try (SqlSession session = manager.openSession(); 40 | Reader reader = Resources.getResourceAsReader("org/mybatis/cdi/CreateDB_JTA.sql")) { 41 | Connection conn = session.getConnection(); 42 | ScriptRunner runner = new ScriptRunner(conn); 43 | runner.setLogWriter(null); 44 | runner.runScript(reader); 45 | } 46 | 47 | return manager; 48 | } 49 | 50 | @ApplicationScoped 51 | @Produces 52 | @JtaManager 53 | @SessionFactoryProvider 54 | public SqlSessionFactory createManagerJTA() throws IOException { 55 | return createSessionManagerJTA(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/SqlSessionManagerRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.annotation.PostConstruct; 19 | import jakarta.enterprise.context.ApplicationScoped; 20 | import jakarta.enterprise.inject.Any; 21 | import jakarta.enterprise.inject.Instance; 22 | import jakarta.inject.Inject; 23 | 24 | import java.util.Collection; 25 | import java.util.Collections; 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | 29 | import org.apache.ibatis.session.SqlSessionFactory; 30 | import org.apache.ibatis.session.SqlSessionManager; 31 | 32 | /** 33 | * @author Frank D. Martinez [mnesarco] 34 | */ 35 | @ApplicationScoped 36 | public class SqlSessionManagerRegistry { 37 | 38 | private Map managers; 39 | 40 | @Inject 41 | @Any 42 | private Instance factories; 43 | 44 | /** 45 | * Inits the SqlSessionManagerRegistry. 46 | */ 47 | @PostConstruct 48 | public void init() { 49 | if (this.factories.isUnsatisfied()) { 50 | throw new MybatisCdiConfigurationException("There are no SqlSessionFactory producers properly configured."); 51 | } 52 | Map m = new HashMap<>(); 53 | for (SqlSessionFactory factory : this.factories) { 54 | SqlSessionManager manager = SqlSessionManager.newInstance(factory); 55 | m.put(factory, manager); 56 | } 57 | this.managers = Collections.unmodifiableMap(m); 58 | } 59 | 60 | public SqlSessionManager getManager(SqlSessionFactory factory) { 61 | return this.managers.get(factory); 62 | } 63 | 64 | public Collection getManagers() { 65 | return this.managers.values(); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/JtaTransactionInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.inject.Instance; 19 | import jakarta.inject.Inject; 20 | import jakarta.interceptor.Interceptor; 21 | import jakarta.transaction.HeuristicMixedException; 22 | import jakarta.transaction.HeuristicRollbackException; 23 | import jakarta.transaction.NotSupportedException; 24 | import jakarta.transaction.RollbackException; 25 | import jakarta.transaction.Status; 26 | import jakarta.transaction.SystemException; 27 | import jakarta.transaction.UserTransaction; 28 | 29 | /** 30 | * Interceptor for JTA transactions. MyBatis should be configured to use the {@code MANAGED} transaction manager. 31 | * 32 | * @author Eduardo Macarrón 33 | */ 34 | @Transactional 35 | @Interceptor 36 | public class JtaTransactionInterceptor extends LocalTransactionInterceptor { 37 | 38 | private static final long serialVersionUID = 1L; 39 | 40 | @Inject 41 | private transient Instance userTransaction; 42 | 43 | @Override 44 | protected boolean isTransactionActive() throws SystemException { 45 | return this.userTransaction.get().getStatus() != Status.STATUS_NO_TRANSACTION; 46 | } 47 | 48 | @Override 49 | protected void beginJta() throws NotSupportedException, SystemException { 50 | this.userTransaction.get().begin(); 51 | } 52 | 53 | @Override 54 | protected void endJta(boolean isExternaTransaction, boolean needsRollback) 55 | throws SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException { 56 | if (isExternaTransaction) { 57 | if (needsRollback) { 58 | this.userTransaction.get().setRollbackOnly(); 59 | } 60 | } else if (needsRollback) { 61 | this.userTransaction.get().rollback(); 62 | } else { 63 | this.userTransaction.get().commit(); 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/site/zh_CN/xdoc/index.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | 简介 24 | Eduardo Macarron 25 | Frank Martinez 26 | Xavi Lee 27 | 28 | 29 | 30 |
31 | 32 |

33 | MyBatis-CDI 扩展负责 MyBatis 映射器和 SqlSession 的生命周期。 MyBatis 组件直接注入 CDI 组件即可使用,无需创建或销毁它们。 34 | 它还提供本地和 JTA 事务支持,基于 @Transactional 注解。 35 |

36 |
37 | 38 | 39 |

40 | 在开始使用 MyBatis 与 CDI 的集成框架之前,必须熟悉 MyBatis 和 CDI 的术语。本文档不提供 MyBatis 或 CDI 的背景信息或基本设置和配置教程。 41 |

42 |

43 | MyBatis - CDI 需要 Java EE 6 或更高版本以及任意的 MyBatis 版本。 44 |

45 |
46 | 47 | 48 |

49 | 如果你发现此文档在任何方面有所欠缺,或者缺少某个功能的文档,那么最好的做法是了解它,然后自己编写文档。 50 |

51 |

52 | 本手册的源文件以 xdoc 格式提供,网址为: 53 | 项目的 Git 仓库 54 | Fork 这个仓库,更新它们,并提交 Pull Request 吧! 55 |

56 |

57 | 还有其他像你一样的人都需要阅读这份文档,而你,就是这份文档最好的作者。 58 |

59 |
60 | 61 | 62 |

您可以阅读 MyBatis 文档的其他语言版本:

63 | 67 |

想用你的母语来了解 MyBatis-CDI 吗?那就将文档翻译成你的母语并提供给我们吧!

68 |
69 | 70 |
71 | 72 | 73 |
74 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/Transactional.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.util.Nonbinding; 19 | import jakarta.interceptor.InterceptorBinding; 20 | 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | import org.apache.ibatis.session.ExecutorType; 27 | 28 | /** 29 | * Adds transaction demarcation to the annotated method. 30 | * 31 | * @author Frank David Martínez 32 | */ 33 | @InterceptorBinding 34 | @Target({ ElementType.METHOD, ElementType.TYPE }) 35 | @Retention(RetentionPolicy.RUNTIME) 36 | public @interface Transactional { 37 | 38 | /** 39 | * Returns the constant indicating the myBatis executor type. 40 | * 41 | * @return ExecutorType.SIMPLE by default, user defined otherwise. 42 | */ 43 | @Nonbinding 44 | ExecutorType executorType() default ExecutorType.SIMPLE; 45 | 46 | /** 47 | * Returns the constant indicating the transaction isolation level. 48 | * 49 | * @return Isolation.DEFAULT by default, user defined otherwise. 50 | */ 51 | @Nonbinding 52 | Isolation isolation() default Isolation.DEFAULT; 53 | 54 | /** 55 | * Flag to indicate that myBatis has to force the transaction {@code commit().} 56 | * 57 | * @return false by default, user defined otherwise. 58 | */ 59 | @Nonbinding 60 | boolean force() default false; 61 | 62 | /** 63 | * If true, the transaction will never committed but rather rolled back, useful for testing purposes. 64 | * 65 | * @return false by default, user defined otherwise. 66 | */ 67 | @Nonbinding 68 | boolean rollbackOnly() default false; 69 | 70 | /** 71 | * Defines zero (0) or more exception {@code Class classes}, which must be a subclass of {@code Throwable}, indicating 72 | * which exception types must cause a transaction rollback. 73 | * 74 | * @return an empty array by default, user defined otherwise. 75 | */ 76 | @Nonbinding 77 | Class[] rollbackFor() default {}; 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/SerializableMapperProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.context.spi.CreationalContext; 19 | 20 | import java.io.IOException; 21 | import java.io.ObjectInputStream; 22 | import java.io.ObjectOutputStream; 23 | import java.io.Serializable; 24 | import java.lang.reflect.InvocationHandler; 25 | import java.lang.reflect.InvocationTargetException; 26 | import java.lang.reflect.Method; 27 | 28 | import org.apache.ibatis.session.SqlSessionFactory; 29 | 30 | /** 31 | * @author Frank D. Martinez [mnesarco] 32 | */ 33 | public class SerializableMapperProxy implements InvocationHandler, Serializable { 34 | 35 | private static final long serialVersionUID = 1L; 36 | 37 | private transient Object mapper; 38 | 39 | private final MyBatisBean bean; 40 | 41 | private final CreationalContext creationalContext; 42 | 43 | /** 44 | * Instantiates a new serializable mapper proxy. 45 | * 46 | * @param bean 47 | * the bean 48 | * @param creationalContext 49 | * the creational context 50 | */ 51 | public SerializableMapperProxy(MyBatisBean bean, CreationalContext creationalContext) { 52 | this.bean = bean; 53 | this.creationalContext = creationalContext; 54 | this.mapper = getMapper(); 55 | } 56 | 57 | @Override 58 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 59 | try { 60 | return method.invoke(this.mapper, args); 61 | } catch (InvocationTargetException e) { 62 | throw e.getTargetException(); 63 | } 64 | } 65 | 66 | private Object getMapper() { 67 | SqlSessionFactory factory = CDIUtils.findSqlSessionFactory(this.bean.sqlSessionFactoryName, this.bean.qualifiers, 68 | this.creationalContext); 69 | return CDIUtils.getRegistry(this.creationalContext).getManager(factory).getMapper(this.bean.type); 70 | } 71 | 72 | private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException { 73 | is.defaultReadObject(); 74 | this.mapper = getMapper(); 75 | } 76 | 77 | private void writeObject(ObjectOutputStream os) throws IOException { 78 | os.defaultWriteObject(); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/FooService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.inject.Inject; 19 | import jakarta.inject.Named; 20 | import jakarta.interceptor.Interceptors; 21 | 22 | import org.apache.ibatis.session.SqlSession; 23 | 24 | @Interceptors(LocalTransactionInterceptor.class) 25 | @Transactional(rollbackFor = RollbackException.class) 26 | public class FooService { 27 | 28 | @Inject 29 | @Named("manager1") 30 | private SqlSession sqlSession; 31 | 32 | @Inject 33 | @Named("manager1") 34 | private UserMapper userMapper; 35 | 36 | @Inject 37 | @MySpecialManager 38 | @OtherQualifier 39 | private UserMapper userMapper3; 40 | 41 | @Inject 42 | @Named("manager2") 43 | private UserMapper userMapper2; 44 | 45 | @Inject 46 | @Named("manager2") 47 | private UserMapper dummyUserMapper; 48 | 49 | @Inject 50 | @Named("unmanaged") 51 | private SqlSession unmanagedSqlSession; 52 | 53 | public User getUserFromSqlSession(int userId) { 54 | return this.sqlSession.selectOne("getUser", userId); 55 | } 56 | 57 | public User getUserFromUnmanagedSqlSession(int userId) { 58 | return this.unmanagedSqlSession.selectOne("getUser", userId); 59 | } 60 | 61 | public User getUser(int userId) { 62 | return this.userMapper.getUser(userId); 63 | } 64 | 65 | public User getUser2(int userId) { 66 | return this.userMapper2.getUser(userId); 67 | } 68 | 69 | public User getUserDummy(int userId) { 70 | return this.dummyUserMapper.getUser(userId); 71 | } 72 | 73 | public User getUser3(int userId) { 74 | return this.userMapper3.getUser(userId); 75 | } 76 | 77 | public void insertUser(User user) { 78 | this.userMapper.insertUser(user); 79 | } 80 | 81 | public void insertUserAndThrowARuntime(User user) { 82 | this.userMapper.insertUser(user); 83 | throw new RuntimeException("fail"); 84 | } 85 | 86 | public void insertUserAndThrowACheckedThatShouldNotRollback(User user) throws NoRollbackException { 87 | this.userMapper.insertUser(user); 88 | throw new NoRollbackException(); 89 | } 90 | 91 | public void insertUserAndThrowACheckedThatShouldRollback(User user) throws RollbackException { 92 | this.userMapper.insertUser(user); 93 | throw new RollbackException(); 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/ManagerProducers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.context.ApplicationScoped; 19 | import jakarta.enterprise.inject.Disposes; 20 | import jakarta.enterprise.inject.Produces; 21 | import jakarta.inject.Named; 22 | 23 | import java.io.IOException; 24 | import java.io.Reader; 25 | import java.sql.Connection; 26 | 27 | import org.apache.ibatis.io.Resources; 28 | import org.apache.ibatis.jdbc.ScriptRunner; 29 | import org.apache.ibatis.session.SqlSession; 30 | import org.apache.ibatis.session.SqlSessionFactory; 31 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 32 | import org.apache.ibatis.session.SqlSessionManager; 33 | 34 | public class ManagerProducers { 35 | 36 | private SqlSessionFactory createSessionManager(int n) throws IOException { 37 | SqlSessionFactory manager; 38 | try (Reader reader = Resources.getResourceAsReader("org/mybatis/cdi/mybatis-config_" + n + ".xml")) { 39 | manager = new SqlSessionFactoryBuilder().build(reader); 40 | } 41 | 42 | try (SqlSession session = manager.openSession(); 43 | Reader reader = Resources.getResourceAsReader("org/mybatis/cdi/CreateDB_" + n + ".sql");) { 44 | Connection conn = session.getConnection(); 45 | ScriptRunner runner = new ScriptRunner(conn); 46 | runner.setLogWriter(null); 47 | runner.runScript(reader); 48 | } 49 | 50 | return manager; 51 | } 52 | 53 | @ApplicationScoped 54 | @Named("manager1") 55 | @Produces 56 | @SessionFactoryProvider 57 | public SqlSessionFactory createManager1() throws IOException { 58 | return createSessionManager(1); 59 | } 60 | 61 | @ApplicationScoped 62 | @Named("manager2") 63 | @Produces 64 | @SessionFactoryProvider 65 | public SqlSessionFactory createManager2() throws IOException { 66 | return createSessionManager(2); 67 | } 68 | 69 | @ApplicationScoped 70 | @Produces 71 | @MySpecialManager 72 | @OtherQualifier 73 | @SessionFactoryProvider 74 | public SqlSessionFactory createManager3() throws IOException { 75 | return createSessionManager(3); 76 | } 77 | 78 | @ApplicationScoped 79 | @Produces 80 | @Named("unmanaged") 81 | public SqlSession createNonCdiManagedSession() throws IOException { 82 | return SqlSessionManager.newInstance(createSessionManager(4)); 83 | } 84 | 85 | @ApplicationScoped 86 | @Named("unmanaged") 87 | public void closeNonCdiManagedSession(@Disposes SqlSession session) { 88 | session.close(); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/NarayanaDataSourceWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import com.arjuna.ats.internal.jdbc.ConnectionManager; 19 | import com.arjuna.ats.jdbc.TransactionalDriver; 20 | 21 | import jakarta.annotation.Nonnull; 22 | 23 | import java.io.PrintWriter; 24 | import java.sql.Connection; 25 | import java.sql.SQLException; 26 | import java.sql.SQLFeatureNotSupportedException; 27 | import java.util.Properties; 28 | import java.util.logging.Logger; 29 | 30 | import javax.sql.DataSource; 31 | import javax.sql.XADataSource; 32 | 33 | public class NarayanaDataSourceWrapper implements DataSource { 34 | private final XADataSource xaDataSource; 35 | 36 | public NarayanaDataSourceWrapper(@Nonnull final XADataSource xaDataSource) { 37 | this.xaDataSource = xaDataSource; 38 | } 39 | 40 | @Override 41 | public Connection getConnection() throws SQLException { 42 | final var properties = new Properties(); 43 | properties.put(TransactionalDriver.XADataSource, xaDataSource); 44 | return ConnectionManager.create(null, properties); 45 | } 46 | 47 | @Override 48 | public Connection getConnection(final String username, final String password) throws SQLException { 49 | final var properties = new Properties(); 50 | properties.put(TransactionalDriver.XADataSource, xaDataSource); 51 | properties.put(TransactionalDriver.userName, username); 52 | properties.put(TransactionalDriver.password, password); 53 | return ConnectionManager.create(null, properties); 54 | } 55 | 56 | @Override 57 | public int getLoginTimeout() throws SQLException { 58 | return xaDataSource.getLoginTimeout(); 59 | } 60 | 61 | @Override 62 | public PrintWriter getLogWriter() throws SQLException { 63 | return xaDataSource.getLogWriter(); 64 | } 65 | 66 | @Override 67 | public Logger getParentLogger() throws SQLFeatureNotSupportedException { 68 | return xaDataSource.getParentLogger(); 69 | } 70 | 71 | @Override 72 | public boolean isWrapperFor(final Class iface) throws SQLException { 73 | return iface.isAssignableFrom(getClass()); 74 | } 75 | 76 | @Override 77 | public void setLoginTimeout(final int seconds) throws SQLException { 78 | xaDataSource.setLoginTimeout(seconds); 79 | } 80 | 81 | @Override 82 | public void setLogWriter(final PrintWriter out) throws SQLException { 83 | xaDataSource.setLogWriter(out); 84 | } 85 | 86 | @SuppressWarnings("unchecked") 87 | @Override 88 | public T unwrap(final Class iface) throws SQLException { 89 | if (isWrapperFor(iface)) { 90 | return (T) this; 91 | } 92 | throw new SQLException(getClass() + " is not a wrapper for " + iface); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/FooServiceJTATest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.inject.Inject; 19 | import jakarta.transaction.UserTransaction; 20 | 21 | import org.jboss.weld.environment.se.Weld; 22 | import org.jboss.weld.junit5.EnableWeld; 23 | import org.jboss.weld.junit5.WeldInitiator; 24 | import org.jboss.weld.junit5.WeldSetup; 25 | import org.junit.jupiter.api.Assertions; 26 | import org.junit.jupiter.api.Test; 27 | import org.junit.jupiter.api.TestInstance; 28 | import org.junit.jupiter.api.TestInstance.Lifecycle; 29 | 30 | @TestInstance(Lifecycle.PER_CLASS) 31 | @EnableWeld 32 | class FooServiceJTATest { 33 | 34 | @WeldSetup 35 | public WeldInitiator weld = WeldInitiator.of(new Weld()); 36 | 37 | @Inject 38 | private FooServiceJTA fooServiceJTA; 39 | 40 | @Inject 41 | private UserTransaction userTransaction; 42 | 43 | @Test 44 | void jtaShouldGetAUserWithNoTX() throws Exception { 45 | this.userTransaction.begin(); 46 | Assertions.assertEquals("1-User1", this.fooServiceJTA.getUserWithNoTransaction(1).getName()); 47 | this.userTransaction.commit(); 48 | } 49 | 50 | @Test 51 | void jtaShouldInsertAUserAndCommit() { 52 | User user = new User(); 53 | user.setId(20); 54 | user.setName("User20"); 55 | this.fooServiceJTA.insertUserWithTransactional(user); 56 | Assertions.assertEquals(user.getName(), this.fooServiceJTA.getUserWithNoTransaction(user.getId()).getName()); 57 | } 58 | 59 | @Test 60 | void jtaShouldInsertAUserAndRollItBack() { 61 | User user = new User(); 62 | user.setId(30); 63 | user.setName("User30"); 64 | try { 65 | this.fooServiceJTA.insertUserWithTransactionalAndFail(user); 66 | } catch (Exception ignore) { 67 | // ignored 68 | } 69 | Assertions.assertNull(this.fooServiceJTA.getUserWithNoTransaction(user.getId())); 70 | } 71 | 72 | @Test 73 | void jtaShouldInsertAUserWithExistingJtaTxAndCommit() throws Exception { 74 | User user = new User(); 75 | user.setId(40); 76 | user.setName("User40"); 77 | this.userTransaction.begin(); 78 | this.fooServiceJTA.insertUserWithTransactional(user); 79 | this.userTransaction.commit(); 80 | Assertions.assertEquals(user.getName(), this.fooServiceJTA.getUserWithNoTransaction(user.getId()).getName()); 81 | } 82 | 83 | @Test 84 | void jtaShouldInsertAUserWithExistingJtaTxAndRollItBack() throws Exception { 85 | User user = new User(); 86 | user.setId(50); 87 | user.setName("User50"); 88 | this.userTransaction.begin(); 89 | this.fooServiceJTA.insertUserWithTransactional(user); 90 | this.userTransaction.rollback(); 91 | Assertions.assertNull(this.fooServiceJTA.getUserWithNoTransaction(user.getId())); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /src/site/xdoc/index.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | Introduction 24 | Eduardo Macarron 25 | Frank Martinez 26 | 27 | 28 | 29 |
30 | 31 |

32 | MyBatis-CDI extension takes care of the lifecycle of MyBatis mappers and SqlSessions. MyBatis components 33 | are directly injected into your CDI beans ready to be used, there is no need to create or destroy them. 34 | It also provides local and JTA transaction support based on the @Transactional annotation. 35 |

36 |
37 | 38 | 39 |

40 | Before starting with MyBatis-CDI integration, it is very important 41 | that you are familiar with both MyBatis and CDI terminology. This 42 | document does not attempt to provide background information or basic 43 | setup and configuration tutorials for either MyBatis or CDI. 44 |

45 |

46 | MyBatis-CDI requires Java EE 6 or higher and any MyBatis version. 47 |

48 |
49 | 50 | 51 |

52 | If you find this documentation lacking in any way, or missing 53 | documentation for a feature, then the best thing to do is learn 54 | about it and then write the documentation yourself! 55 |

56 |

57 | Sources of this manual are available in xdoc format at 58 | project's Git 59 | Fork the repository, update them and send a pull request. 60 |

61 |

62 | You’re the best author of this documentation, people like you have 63 | to read it! 64 |

65 |
66 | 67 | 68 |

Users can read about MyBatis in following translations:

69 | 73 |

Do you want to read about MyBatis in your own native language? File an issue providing patches with your 74 | mother tongue documentation!

75 |
76 |
77 | 78 | 79 |
80 | -------------------------------------------------------------------------------- /src/site/zh_CN/xdoc/getting-started.xml.vm: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | 入门 24 | Frank Martinez 25 | Eduardo Macarron 26 | Xavi Lee 27 | 28 | 29 | 30 |
31 |

32 | 这一章节将向你展示 MyBatis-CDI 的安装步骤。 33 |

34 | 35 | 36 |

37 | 要使用 MyBatis-CDI 模块,你只需要将 mybatis-cdi-${project.version}.jar 38 | 文件和它的依赖置于类路径中。 39 |

40 |

41 | 如果你使用 Maven, 只需要将以下依赖添加到你的 pom.xml 中: 42 |

43 | 45 | org.mybatis 46 | mybatis-cdi 47 | ${project.version} 48 | ]]> 49 |
50 | 51 | 52 | 53 |

54 | 首先,开启 MyBatis-CDI 需要向你的 META-INF 文件夹中添加 beans.xml 文件。 55 |

56 | 57 |

58 | 接下来,使用 MyBatis-CDI 至少需要两样东西: 59 | SqlSessionFactory 和用于注入 Mybatis mapper 的 CDI 组件(bean). 60 |

61 | 62 |

63 | 创建一个生成器(producer)方法,返回一个应用域(application scoped)的 SqlSessionFactory 并带有 @SessionFactoryProvider 注解: 64 |

65 | 66 | 82 | 83 |

84 | 假定你有一个像下面这样的 mapper 接口(注意:必须在 mapper 上添加 @Mapper 注解): 85 |

86 | 94 | 95 |

96 | 你可以使用 @Inject 将任意的依赖注入到 CDI 组件中去: 97 |

98 | 99 | 111 |
112 |
113 | 114 |
115 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/CDIUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.context.spi.CreationalContext; 19 | import jakarta.enterprise.inject.Any; 20 | import jakarta.enterprise.inject.Default; 21 | import jakarta.enterprise.inject.spi.Bean; 22 | import jakarta.enterprise.inject.spi.BeanManager; 23 | import jakarta.enterprise.inject.spi.CDI; 24 | import jakarta.enterprise.util.AnnotationLiteral; 25 | 26 | import java.lang.annotation.Annotation; 27 | import java.util.Iterator; 28 | import java.util.Set; 29 | 30 | import org.apache.ibatis.session.SqlSessionFactory; 31 | 32 | /** 33 | * @author Frank D. Martinez [mnesarco] 34 | */ 35 | public final class CDIUtils { 36 | 37 | private CDIUtils() { 38 | // this class cannot be instantiated 39 | } 40 | 41 | /** 42 | * Gets a CDI BeanManager instance 43 | * 44 | * @return BeanManager instance 45 | */ 46 | private static BeanManager getBeanManager() { 47 | return CDI.current().getBeanManager(); 48 | } 49 | 50 | /** 51 | * Gets the registry. 52 | * 53 | * @param creationalContext 54 | * the creational context 55 | * 56 | * @return the registry 57 | */ 58 | public static SqlSessionManagerRegistry getRegistry(CreationalContext creationalContext) { 59 | final BeanManager beanManager = getBeanManager(); 60 | Iterator> beans = beanManager.getBeans(SqlSessionManagerRegistry.class).iterator(); 61 | return (SqlSessionManagerRegistry) beanManager.getReference(beans.next(), SqlSessionManagerRegistry.class, 62 | creationalContext); 63 | } 64 | 65 | /** 66 | * Find sql session factory. 67 | * 68 | * @param name 69 | * the name 70 | * @param qualifiers 71 | * the qualifiers 72 | * @param creationalContext 73 | * the creational context 74 | * 75 | * @return the sql session factory 76 | */ 77 | public static SqlSessionFactory findSqlSessionFactory(String name, Set qualifiers, 78 | CreationalContext creationalContext) { 79 | final BeanManager beanManager = getBeanManager(); 80 | Set> beans; 81 | if (name != null) { 82 | beans = beanManager.getBeans(name); 83 | } else { 84 | beans = beanManager.getBeans(SqlSessionFactory.class, qualifiers.toArray(new Annotation[] {})); 85 | } 86 | Bean bean = beanManager.resolve(beans); 87 | if (bean == null) { 88 | throw new MybatisCdiConfigurationException("There are no SqlSessionFactory producers properly configured."); 89 | } 90 | return (SqlSessionFactory) beanManager.getReference(bean, SqlSessionFactory.class, creationalContext); 91 | } 92 | 93 | public static class SerializableDefaultAnnotationLiteral extends AnnotationLiteral { 94 | private static final long serialVersionUID = 1L; 95 | } 96 | 97 | public static class SerializableAnyAnnotationLiteral extends AnnotationLiteral { 98 | private static final long serialVersionUID = 1L; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/MybatisExtensionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import static org.mockito.ArgumentMatchers.any; 19 | import static org.mockito.Mockito.mock; 20 | import static org.mockito.Mockito.verify; 21 | import static org.mockito.Mockito.when; 22 | 23 | import jakarta.enterprise.inject.spi.AfterBeanDiscovery; 24 | import jakarta.enterprise.inject.spi.Annotated; 25 | import jakarta.enterprise.inject.spi.AnnotatedType; 26 | import jakarta.enterprise.inject.spi.Bean; 27 | import jakarta.enterprise.inject.spi.InjectionPoint; 28 | import jakarta.enterprise.inject.spi.InjectionTarget; 29 | import jakarta.enterprise.inject.spi.ProcessAnnotatedType; 30 | import jakarta.enterprise.inject.spi.ProcessInjectionTarget; 31 | 32 | import java.lang.reflect.Type; 33 | import java.util.HashSet; 34 | import java.util.Set; 35 | 36 | import org.junit.jupiter.api.Test; 37 | import org.junit.jupiter.api.extension.ExtendWith; 38 | import org.mockito.junit.jupiter.MockitoExtension; 39 | 40 | @ExtendWith(MockitoExtension.class) 41 | class MybatisExtensionTest { 42 | 43 | @SuppressWarnings("unchecked") 44 | @Test 45 | void mappersFoundAfterTheBeanUsingTheMapperInAnInjectionPointHasBeenScannedShouldBeInstantiated() throws Exception { 46 | 47 | MybatisExtension extension = new MybatisExtension(); 48 | Type type = UserMapper.class; 49 | 50 | projectInjectionTarget(extension, type); 51 | 52 | processAnnotatedType(extension, type); 53 | 54 | AfterBeanDiscovery afterBeanDiscovery = mock(AfterBeanDiscovery.class); 55 | extension.afterBeanDiscovery(afterBeanDiscovery); 56 | 57 | verify(afterBeanDiscovery).addBean((Bean) any()); 58 | 59 | } 60 | 61 | private void projectInjectionTarget(MybatisExtension extension, Type type) { 62 | ProcessInjectionTarget event = mock(ProcessInjectionTarget.class); 63 | InjectionTarget injectTarget = mock(InjectionTarget.class); 64 | Set injectionPoints = new HashSet<>(); 65 | 66 | InjectionPoint injectionPoint = mock(InjectionPoint.class); 67 | Annotated annotated = mock(Annotated.class); 68 | 69 | when(injectionPoint.getAnnotated()).thenReturn(annotated); 70 | 71 | when(annotated.getBaseType()).thenReturn(type); 72 | when(annotated.getAnnotations()).thenReturn(new HashSet<>()); 73 | 74 | injectionPoints.add(injectionPoint); 75 | 76 | when(event.getInjectionTarget()).thenReturn(injectTarget); 77 | when(injectTarget.getInjectionPoints()).thenReturn(injectionPoints); 78 | 79 | extension.processInjectionTarget(event); 80 | } 81 | 82 | @SuppressWarnings("unchecked") 83 | private void processAnnotatedType(MybatisExtension extension, Type type) { 84 | ProcessAnnotatedType pat = mock(ProcessAnnotatedType.class); 85 | AnnotatedType annotatedType = mock(AnnotatedType.class); 86 | 87 | when(annotatedType.isAnnotationPresent(Mapper.class)).thenReturn(true); 88 | when(pat.getAnnotatedType()).thenReturn(annotatedType); 89 | when(annotatedType.getBaseType()).thenReturn(type); 90 | when(annotatedType.getJavaClass()).thenReturn((Class) UserMapper.class); 91 | 92 | extension.processAnnotatedType(pat); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.net.Authenticator; 23 | import java.net.PasswordAuthentication; 24 | import java.net.URI; 25 | import java.net.URL; 26 | import java.nio.file.Files; 27 | import java.nio.file.Path; 28 | import java.nio.file.StandardCopyOption; 29 | import java.util.concurrent.ThreadLocalRandom; 30 | 31 | public final class MavenWrapperDownloader { 32 | private static final String WRAPPER_VERSION = "3.3.4"; 33 | 34 | private static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("MVNW_VERBOSE")); 35 | 36 | public static void main(String[] args) { 37 | log("Apache Maven Wrapper Downloader " + WRAPPER_VERSION); 38 | 39 | if (args.length != 2) { 40 | System.err.println(" - ERROR wrapperUrl or wrapperJarPath parameter missing"); 41 | System.exit(1); 42 | } 43 | 44 | try { 45 | log(" - Downloader started"); 46 | final URL wrapperUrl = URI.create(args[0]).toURL(); 47 | final Path baseDir = Path.of(".").toAbsolutePath().normalize(); 48 | final Path wrapperJarPath = baseDir.resolve(args[1]).normalize(); 49 | if (!wrapperJarPath.startsWith(baseDir)) { 50 | throw new IOException("Invalid path: outside of allowed directory"); 51 | } 52 | downloadFileFromURL(wrapperUrl, wrapperJarPath); 53 | log("Done"); 54 | } catch (IOException e) { 55 | System.err.println("- Error downloading: " + e.getMessage()); 56 | if (VERBOSE) { 57 | e.printStackTrace(); 58 | } 59 | System.exit(1); 60 | } 61 | } 62 | 63 | private static void downloadFileFromURL(URL wrapperUrl, Path wrapperJarPath) 64 | throws IOException { 65 | log(" - Downloading to: " + wrapperJarPath); 66 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 67 | final String username = System.getenv("MVNW_USERNAME"); 68 | final char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 69 | Authenticator.setDefault(new Authenticator() { 70 | @Override 71 | protected PasswordAuthentication getPasswordAuthentication() { 72 | return new PasswordAuthentication(username, password); 73 | } 74 | }); 75 | } 76 | Path temp = wrapperJarPath 77 | .getParent() 78 | .resolve(wrapperJarPath.getFileName() + "." 79 | + Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp"); 80 | try (InputStream inStream = wrapperUrl.openStream()) { 81 | Files.copy(inStream, temp, StandardCopyOption.REPLACE_EXISTING); 82 | Files.move(temp, wrapperJarPath, StandardCopyOption.REPLACE_EXISTING); 83 | } finally { 84 | Files.deleteIfExists(temp); 85 | } 86 | log(" - Downloader complete"); 87 | } 88 | 89 | private static void log(String msg) { 90 | if (VERBOSE) { 91 | System.out.println(msg); 92 | } 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /src/site/xdoc/getting-started.xml.vm: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | Getting Started 24 | Frank Martinez 25 | Eduardo Macarron 26 | 27 | 28 | 29 |
30 |

31 | This chapter will show you in a few steps how to install and setup MyBatis-CDI. 32 |

33 | 34 | 35 |

36 | To use the MyBatis-CDI module, you just need to include the mybatis-cdi-${project.version}.jar 37 | file and its dependencies in the classpath. 38 |

39 |

40 | If you are using Maven just add the following dependency to your pom.xml: 41 |

42 | 44 | org.mybatis 45 | mybatis-cdi 46 | ${project.version} 47 | ]]> 48 |
49 | 50 | 51 | 52 |

53 | First, switch CDI on by adding a beans.xml file to your META-INF directory. 54 |

55 | 56 |

57 | Next, to use MyBatis with CDI you need to provide at least two things: 58 | an SqlSessionFactory and a CDI bean that requires a MyBatis mapper injection. 59 |

60 | 61 |

62 | Create a producer method that returns an application scoped SqlSessionFactory annotated with @SessionFactoryProvider: 63 |

64 | 65 | 81 | 82 |

83 | Assume you have a mapper interface defined like the following (Note that the mapper must be annotated with @Mapper): 84 |

85 | 93 | 94 |

95 | You can inject it into a CDI bean just as any other dependency using @Inject : 96 |

97 | 98 | 110 |
111 |
112 | 113 |
114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MyBatis CDI Extension 2 | ===================== 3 | 4 | [![Java CI](https://github.com/mybatis/cdi/workflows/Java%20CI/badge.svg)](https://github.com/mybatis/cdi/actions?query=workflow%3A%22Java+CI%22) 5 | [![Coverage Status](https://coveralls.io/repos/mybatis/cdi/badge.svg?branch=master&service=github)](https://coveralls.io/github/mybatis/cdi?branch=master) 6 | [![Maven central](https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis-cdi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.mybatis/mybatis-cdi) 7 | [![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/https/oss.sonatype.org/org.mybatis/mybatis-cdi.svg)](https://oss.sonatype.org/content/repositories/snapshots/org/mybatis/mybatis-cdi/) 8 | [![License](https://img.shields.io/:license-apache-brightgreen.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 9 | 10 | ![mybatis-logo](https://mybatis.org/images/mybatis-logo.png) 11 | 12 | MyBatis-CDI extension takes care of the lifecycle of MyBatis mappers and SqlSessions. MyBatis components are directly injected into your 13 | CDI beans ready to be used, there is no need to create or destroy them. It also provides local and JTA transaction support based on the 14 | @Transactional annotation. 15 | 16 | Compatibility 17 | ------------- 18 | 19 | | CDI API VERSION | JDK 17 | JDK 11 | JDK 8 | 20 | | ----------------------- | ------ | ------- | ------ | 21 | | cdi-1.0 (not supported) | N | N | N | 22 | | cdi-1.1 | Y | Y | Y | 23 | | cdi-1.2 | Y | Y | Y | 24 | | cdi-2.0 | Y | Y | Y | 25 | | cdi-3.0 | Y | Y | N | 26 | | cdi-4.0 | Y | Y | N | 27 | | cdi-4.1 | Y | N | N | 28 | 29 | JavaEE is 1.1.4 and is now no longer our focus 30 | 31 | JakartaEE is 2.0.0 and is the main focus 32 | 33 | Jdk17+ requires jboss-classfilewriter to be at 1.2.5 or it will error, [classfilewriter](https://github.com/jbossas/jboss-classfilewriter/issues/24) 34 | 35 | Building from [tag](https://github.com/mybatis/cdi/tree/mybatis-cdi-1.1.4) for javax namespace 36 | ---------------------------------------------------------------------------------------------- 37 | 38 | Maven does not allow multiple auto activated profiles (ie activateByDefault and Jdk). This project supports 3 different CDI support levels currently 39 | and was set to default to cdi-1.2. Jdk 8 does not allow add opens and this is needed now for jdk 17. We further are supporting jakarta EE packaging 40 | in addition to java EE packaging. This results is a tricky situation. Therefore, we have opted to no longer use activateByDefault which means a 41 | build without providing a profile will fail with missing classes. Github actions is provisioned to always provide the specific cdi-api profile. 42 | Any localized usage must now do the same as defined below. 43 | 44 | The cdi profile can be cdi-1.1, cdi-1.2, or cdi-2.0 and required to run with any of the following. 45 | 46 | - mvn clean install -P"cdi-1.1" 47 | - mvn clean install -P"cdi-1.2" 48 | - mvn clean install -P"cdi-2.0" 49 | 50 | Building from master 51 | -------------------- 52 | 53 | Normal maven build without any special profiles necessary. This line is Jakarta. 54 | 55 | Testing with Arquillian (for javaEE, has not been checked with jakarta) 56 | ----------------------------------------------------------------------- 57 | 58 | In order to test with Arquillian, one class needs to be added to 'JavaArchive' in order for this to load properly. Add 'SqlSessionManagerRegistry.class'. 59 | 60 | See attachment on https://github.com/mybatis/cdi/issues/86 from 2021-12-30 ArquillianMybatisExample.zip [here](https://github.com/mybatis/cdi/files/7795050/ArquillianMybatisExample.zip). This is to be run under jdk 8 to be error free but will run as-is under jdk 11 with an invocation error which still allows it to run. Further updates are needed to get this code example current. 61 | 62 | Essentials 63 | ---------- 64 | 65 | - [See the docs](https://mybatis.org/cdi/) 66 | 67 | 68 | Contributed Examples 69 | -------------------- 70 | 71 | - Ready to deploy sample web app (Jee7): [samples](https://github.com/mnesarco/mybatis-cdi-samples/) 72 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/cdi/FooServiceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2025 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.inject.Inject; 19 | 20 | import java.io.InputStream; 21 | import java.io.ObjectInputStream; 22 | import java.io.ObjectOutputStream; 23 | import java.io.OutputStream; 24 | import java.nio.file.Files; 25 | import java.nio.file.Path; 26 | 27 | import org.jboss.weld.environment.se.Weld; 28 | import org.jboss.weld.junit5.EnableWeld; 29 | import org.jboss.weld.junit5.WeldInitiator; 30 | import org.jboss.weld.junit5.WeldSetup; 31 | import org.junit.jupiter.api.Assertions; 32 | import org.junit.jupiter.api.Test; 33 | import org.junit.jupiter.api.TestInstance; 34 | import org.junit.jupiter.api.TestInstance.Lifecycle; 35 | 36 | @TestInstance(Lifecycle.PER_CLASS) 37 | @EnableWeld 38 | class FooServiceTest { 39 | 40 | @WeldSetup 41 | public WeldInitiator weld = WeldInitiator.of(new Weld()); 42 | 43 | @Inject 44 | private FooService fooService; 45 | 46 | @Inject 47 | private SerializableFooService serFooService; 48 | 49 | @Test 50 | void shouldGetAUser() { 51 | Assertions.assertEquals("1-User1", this.fooService.getUserFromSqlSession(1).getName()); 52 | Assertions.assertEquals("1-User1", this.fooService.getUser(1).getName()); 53 | Assertions.assertEquals("2-User2", this.fooService.getUser2(2).getName()); 54 | Assertions.assertEquals("3-User3", this.fooService.getUser3(3).getName()); 55 | Assertions.assertEquals("4-User1", this.fooService.getUserFromUnmanagedSqlSession(1).getName()); 56 | } 57 | 58 | @Test 59 | void shouldInjectTheSameMapper() { 60 | Assertions.assertEquals(this.fooService.getUser2(1).getName(), this.fooService.getUserDummy(1).getName()); 61 | Assertions.assertEquals(this.fooService.getUser2(2).getName(), this.fooService.getUserDummy(2).getName()); 62 | Assertions.assertEquals(this.fooService.getUser2(3).getName(), this.fooService.getUserDummy(3).getName()); 63 | } 64 | 65 | @Test 66 | void shouldInsertAUserAndCommit() { 67 | User user = new User(); 68 | user.setId(20); 69 | user.setName("User20"); 70 | this.fooService.insertUser(user); 71 | Assertions.assertEquals("User20", this.fooService.getUser(20).getName()); 72 | } 73 | 74 | @Test 75 | void shouldInsertAUserThatFailsWithRuntimeAndRollItBack() { 76 | User user = new User(); 77 | user.setId(30); 78 | user.setName("User40"); 79 | try { 80 | this.fooService.insertUserAndThrowARuntime(user); 81 | } catch (Exception ignore) { 82 | // ignored 83 | } 84 | Assertions.assertNull(this.fooService.getUser(40)); 85 | } 86 | 87 | @Test 88 | void shouldInsertAUserThatFailsWithACheckedAndCommit() { 89 | User user = new User(); 90 | user.setId(30); 91 | user.setName("User30"); 92 | try { 93 | this.fooService.insertUserAndThrowACheckedThatShouldNotRollback(user); 94 | } catch (Exception ignore) { 95 | // ignored 96 | } 97 | Assertions.assertEquals("User30", this.fooService.getUser(30).getName()); 98 | } 99 | 100 | @Test 101 | void shouldInsertAUserThatFailsWithACustomExceptionMarkedToRollbackAndRollItBack() { 102 | User user = new User(); 103 | user.setId(30); 104 | user.setName("User30"); 105 | try { 106 | this.fooService.insertUserAndThrowACheckedThatShouldRollback(user); 107 | } catch (Exception ignore) { 108 | // ignored 109 | } 110 | Assertions.assertNull(this.fooService.getUser(30)); 111 | } 112 | 113 | @Test 114 | void injectedMappersAreSerializable() throws Exception { 115 | try (OutputStream outputStream = Files.newOutputStream(Path.of("target/mapper.ser")); 116 | ObjectOutputStream oout = new ObjectOutputStream(outputStream)) { 117 | oout.writeObject(this.serFooService); 118 | } 119 | try (InputStream inputStream = Files.newInputStream(Path.of("target/mapper.ser")); 120 | ObjectInputStream oin = new ObjectInputStream(inputStream)) { 121 | SerializableFooService unserialized = (SerializableFooService) oin.readObject(); 122 | Assertions.assertEquals(this.serFooService.getUser(1).getName(), unserialized.getUser(1).getName()); 123 | } 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/MyBatisBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.context.Dependent; 19 | import jakarta.enterprise.context.spi.CreationalContext; 20 | import jakarta.enterprise.inject.spi.Bean; 21 | import jakarta.enterprise.inject.spi.InjectionPoint; 22 | import jakarta.enterprise.inject.spi.PassivationCapable; 23 | 24 | import java.io.Serializable; 25 | import java.lang.annotation.Annotation; 26 | import java.lang.reflect.Proxy; 27 | import java.lang.reflect.Type; 28 | import java.util.Collections; 29 | import java.util.HashSet; 30 | import java.util.Set; 31 | 32 | import org.apache.ibatis.executor.ErrorContext; 33 | import org.apache.ibatis.session.SqlSession; 34 | import org.apache.ibatis.session.SqlSessionFactory; 35 | import org.apache.ibatis.session.SqlSessionManager; 36 | 37 | /** 38 | * Internal CDI metadata for a mapper bean. 39 | * 40 | * @author Frank D. Martinez [mnesarco] 41 | */ 42 | public class MyBatisBean implements Bean, Serializable, PassivationCapable { 43 | 44 | private static final long serialVersionUID = 1L; 45 | 46 | protected final Class type; 47 | 48 | // Do not make this transient 49 | protected final Set qualifiers; 50 | 51 | protected final String sqlSessionFactoryName; 52 | 53 | protected final String id; 54 | 55 | /** 56 | * Instantiates a new my batis bean. 57 | * 58 | * @param id 59 | * the id 60 | * @param type 61 | * the type 62 | * @param qualifiers 63 | * the qualifiers 64 | * @param sqlSessionFactoryName 65 | * the sql session factory name 66 | */ 67 | public MyBatisBean(String id, Class type, Set qualifiers, String sqlSessionFactoryName) { 68 | this.id = id; 69 | this.type = type; 70 | this.sqlSessionFactoryName = sqlSessionFactoryName; 71 | if (qualifiers == null || qualifiers.isEmpty()) { 72 | this.qualifiers = new HashSet<>(); 73 | this.qualifiers.add(new CDIUtils.SerializableDefaultAnnotationLiteral()); 74 | this.qualifiers.add(new CDIUtils.SerializableAnyAnnotationLiteral()); 75 | } else { 76 | this.qualifiers = qualifiers; 77 | } 78 | } 79 | 80 | @Override 81 | public Set getTypes() { 82 | Set types = new HashSet<>(); 83 | types.add(this.type); 84 | return types; 85 | } 86 | 87 | @Override 88 | public Set getQualifiers() { 89 | return this.qualifiers; 90 | } 91 | 92 | @Override 93 | public Class getScope() { 94 | return Dependent.class; 95 | } 96 | 97 | @Override 98 | public String getName() { 99 | return null; 100 | } 101 | 102 | @Override 103 | public Set> getStereotypes() { 104 | return Collections.emptySet(); 105 | } 106 | 107 | @Override 108 | public Class getBeanClass() { 109 | return this.type; 110 | } 111 | 112 | @Override 113 | public boolean isAlternative() { 114 | return false; 115 | } 116 | 117 | @Override 118 | public Set getInjectionPoints() { 119 | return Collections.emptySet(); 120 | } 121 | 122 | @Override 123 | public Object create(CreationalContext creationalContext) { 124 | if (SqlSession.class.equals(this.type)) { 125 | return findSqlSessionManager(creationalContext); 126 | } 127 | ErrorContext.instance().reset(); 128 | return Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[] { this.type }, 129 | new SerializableMapperProxy<>(this, creationalContext)); 130 | } 131 | 132 | @Override 133 | public void destroy(Object instance, CreationalContext creationalContext) { 134 | creationalContext.release(); 135 | } 136 | 137 | private SqlSessionManager findSqlSessionManager(CreationalContext creationalContext) { 138 | SqlSessionFactory factory = CDIUtils.findSqlSessionFactory(this.sqlSessionFactoryName, this.qualifiers, 139 | creationalContext); 140 | return CDIUtils.getRegistry(creationalContext).getManager(factory); 141 | } 142 | 143 | @Override 144 | public String getId() { 145 | return this.id; 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /src/site/zh_CN/xdoc/transactions.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | 事务 24 | Frank Martinez 25 | Eduardo Macarron 26 | Xavi Lee 27 | 28 | 29 | 30 |
31 |

32 | 正如所料,MyBatis-CDI 提供了基于注解的事务支持。 33 |

34 | 35 |

36 | 默认情况下,每个方法调用都将使用一个孤立的 session,但如果你希望在事务中包含许多方法调用,请使用 @Transactional。 37 |

38 | 39 | 46 | 47 |

NOTE 48 | 批处理语句保存在事务环境中,因此必须使用 @Transactional 属性才能使用批处理执行器。

49 | 50 |

51 | 要启用事务,你需要在中激活本地事务拦截器 beans.xml: 52 |

53 | 54 | 57 | 58 | org.mybatis.cdi.LocalTransactionInterceptor 59 | 60 | ]]> 61 | 62 |

63 | 在这种设置下,MyBatis-CDI 将在进入事务方法之前启动事务,如果事务结束时没有异常,或任何非 RuntimeException 的异常,或注解属性 rollbackFor 中明确指示的异常,则将提交该事务, 且属性 rollbackOnly 为 false。 64 |

65 | 66 |

67 | 如果一个事务方法有对其他嵌套事务方法的调用,则内部方法注解将被忽略,事务将在调用外部方法之前开始,事务将在它结束后结束。 68 |

69 | 70 |

NOTE 71 | 本地事务拦截器使用“尽力而为”的策略。如果有多个 SqlSessionFactory,它将尝试逐个提交或回滚它们。LocalTransactionInterceptor 无法保证此场景下的数据一致性,仅建议在有 no-XA 数据源时使用。否则,你应该改用JtaTransactionInterceptor。有关详细信息,请参阅以下部分。 72 |

73 | 74 | 75 |

76 | 如果你使用多个数据源,则应通过如下配置 bean.xml 文件来使用 JTA 事务拦截器: 77 |

78 | 81 | 82 | org.mybatis.cdi.JtaTransactionInterceptor 83 | 84 | ]]> 85 | 86 |

87 | 并将 MyBatis-config.xml 文件中的事务管理器设置为 MANAGED。 88 |

89 | 90 | ]]> 91 | 92 |
93 | 94 | 95 |

96 | @Transactional 注解支持以下参数:

97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 |
属性默认值描述
executorTypeExecutorType.SIMPLEThe MyBatis 执行器类型
isolationIsolation.DEFAULT事务隔离级别。默认值将使 MyBatis 使用与数据源的默认隔离级别。
forcefalse表示 MyBatis 强制使用 commit() 提交事务。
rollbackOnlyfalse如果为 true,则永远不会提交事务,而是强制回滚。该配置对于测试目的很有用。
rollbackFor未配置指示哪些异常类型必须导致事务回滚。除了指示的异常之外,任何 RuntimeException 也将导致回滚。
133 | 134 |
135 |
136 | 137 |
138 | -------------------------------------------------------------------------------- /src/site/zh_CN/xdoc/injection.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | 注入 MyBatis 组件 24 | Frank Martinez 25 | Eduardo Macarron 26 | Xavi Lee 27 | 28 | 29 | 30 |
31 | 32 |

33 | 本节将带你详细了解发现和注入的过程。 34 |

35 | 36 | 37 |

38 | SqlSessionFactory 是 MyBatis 组件的开始,因此首先需要创建它, 39 | 并让容器知道它的存在。 要做到这一点,请使用 producer 方法创建一个组件,并确保添加了 @ApplicationScoped 注解。因为我们只希望在整个应用程序中拥有一个工厂的实例。 40 |

41 |

42 | 为了让容器标识要使用的生产者,你必须用 @SessionFactoryProvider: 43 |

44 | 45 | 59 | 60 |

NOTE 61 | 如果你忘记添加 @ApplicationScoped,工厂将在每次注入时创建,这意味着将解析和加载所有的 MyBatis xml 文件。你的应用程序不会有问题,但速度会非常慢。 62 |

63 | 64 |

NOTE 65 | 如果你忘记了 @SessionFactoryProvider,该工厂将被 mybatis-cdi 忽略。 66 |

67 | 68 |
69 | 70 | 71 |

72 | 没有什么比将映射器作为 CDI 依赖注入更特别的了: 73 |

74 | 75 | 86 | 87 |

88 | 然后,它将从 CDI 容器中注册的 SqlSessionFactory 中获取该映射器的实例(实际上是代理) ,并将其注入到组件中。这个代理不是一个普通的映射器,而是一个线程安全的单例,所以你不需要担心它的作用域。 89 |

90 | 91 |

92 | 在初始化过程中可能发生的任何配置问题都会使模块抛出 93 | MyBatisCdiConfiurationException。考虑到没有太多需要配置的东西,只有当 SqlSessionFactory 找不到或配置错误时,才会抛出此异常。 94 |

95 | 96 |
97 | 98 | 99 | 100 |

101 | 如果有多个 SqlSessionFactory,则可以使用以下方法选择要在任何注入点中使用的一个: 102 |

103 | 104 |
    105 |
  • 使用一个或多个限定符(由你定义)
  • 106 |
  • SqlSessionFactory 命名(使用 @Named 注解)
  • 107 |
  • 还可以将它们组合使用
  • 108 |
109 | 110 |

111 | 下面是一个代码段,该代码段创建了两个 SqlSessionFactory 的 producer。限定符用于区分它们,就像处理任何其他 CDI 组件一样: 112 |

113 | 114 | 130 | 131 |

132 | 现在没有歧义了,你可以通过向其添加限定符来选择要注入的工厂: 133 |

134 | 135 | 138 | 139 |

140 | 你还可以为 SqlSessionFactory 命名: 141 |

142 | 143 | 151 | 152 |

153 | 在注入点中用它的名字来代替它: 154 |

155 | 156 | 159 | 160 |
161 | 162 | 163 | 164 |

你可以通过请求注入线程安全的 SqlSession,如下所示:

165 | 166 | 169 | 170 |

我们看到的映射器的工厂选择标准也适用于 SqlSession 注入。

171 | 172 |

NOTE 173 | MyBatis-CDI 控制注入的 SqlSession 的生命周期,因此你不能调用任何事务方法,如 commit()rollback()。也没有像 close() 这样的生命周期方法。 174 |

175 | 176 |
177 |
178 | 179 |
180 | -------------------------------------------------------------------------------- /src/site/xdoc/transactions.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | Transactions 24 | Frank Martinez 25 | Eduardo Macarron 26 | 27 | 28 | 29 |
30 |

31 | As you may have expected, MyBatis-CDI provides annotation based transaction support. 32 |

33 | 34 |

35 | By default, each method call will use an isolated session but if you want to enclose many method calls in a transaction, 36 | annotate the method with @Transactional. 37 |

38 | 39 | 46 | 47 |

NOTE 48 | Batch statements are held in the transactional context so you must use the @Transactional attribute to use the batch executor. 49 |

50 | 51 |

52 | To enable transactions you need to activate the local transaction interceptor in beans.xml: 53 |

54 | 55 | 58 | 59 | org.mybatis.cdi.LocalTransactionInterceptor 60 | 61 | ]]> 62 | 63 |

64 | With this setting, MyBatis-CDI will start a transaction before entering the transactional method and will commit it if it 65 | ends with no exception or any exception that is not a RuntimeException or explicitly indicated in the 66 | annotation property rollbackFor and the annotation property rollbackOnly is false. 67 |

68 | 69 |

70 | If a transactional method has calls to other nested transactional methods, inner method annotations will be ignored, transaction 71 | will begin before calling the outer method and will end after it ends. 72 |

73 | 74 |

NOTE 75 | The local transaction interceptor uses a best-effort strategy. In case there are more than one SqlSessionFactorys 76 | it will try to commit or roll them all back one by one. LocalTransactionInterceptor cannot guarantee data consistency 77 | in this scenario, and it is only recommended when you are using at least one no-XA datasource. 78 | Otherwise you should use the JtaTransactionInterceptor instead. See the following section for the details. 79 |

80 | 81 | 82 |

83 | In case you are using more than one datasource you should use the JTA transaction interceptor by configuring the beans.xml file as follows: 84 |

85 | 88 | 89 | org.mybatis.cdi.JtaTransactionInterceptor 90 | 91 | ]]> 92 | 93 |

94 | And configure MyBatis to to use the MANAGED transaction manager in mybatis-config.xml file. 95 |

96 | 97 | ]]> 98 | 99 |
100 | 101 | 102 |

103 | The @Transactional annotation supports the following parameters:

104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 124 | 125 | 126 | 127 | 128 | 130 | 131 | 132 | 133 | 134 | 136 | 137 | 138 | 139 | 140 | 142 | 143 | 144 |
PropertyDefaultDescription
executorTypeExecutorType.SIMPLEThe MyBatis executor type
isolationIsolation.DEFAULTThe transaction isolation level. The default value will 122 | cause MyBatis to use the default isolation level from the 123 | data source.
forcefalseFlag to indicate that MyBatis has to force the 129 | transaction commit()
rollbackOnlyfalseIf true, the transaction will never be committed, but rather the rollback will be forced. 135 | That configuration is useful for testing purposes.
rollbackFornot setIndicates which exception types must cause a transaction rollback. In addition 141 | to the indicated exceptions, any RuntimeException will also cause a rollback.
145 | 146 |
147 |
148 | 149 |
150 | -------------------------------------------------------------------------------- /src/site/xdoc/injection.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis-CDI | Injecting MyBatis beans 24 | Frank Martinez 25 | Eduardo Macarron 26 | 27 | 28 | 29 |
30 | 31 |

32 | This section will take you through de details of the discovery and injection process. 33 |

34 | 35 | 36 |

37 | The SqlSessionFactory is the source of any MyBatis bean so first you need to create one (at least) 38 | and let the container know about it existence. To do so, create a bean with producer method, and make sure 39 | that you add the @ApplicationScoped annotation to it because we just want to have one instance 40 | of the factory for the whole application. 41 |

42 |

43 | In order to let the container identify the producers to be used, you must annotate them with @SessionFactoryProvider. 44 |

45 | 46 | 60 | 61 |

NOTE 62 | If you forget the @ApplicationScoped the factory will be created on each injection, what implies 63 | parsing and loading the whole MyBatis xml file set. Your application will not fail but it will be terribly slow. 64 |

65 | 66 |

NOTE 67 | If you forget the @SessionFactoryProvider the factory will be ignored by mybatis-cdi. 68 |

69 | 70 |
71 | 72 | 73 |

74 | There is nothing special more than injecting the mapper as any other CDI dependecy: 75 |

76 | 77 | 88 | 89 |

90 | Then it will get an instance (a proxy in fact) of that mapper from the SqlSessionFactory registered in 91 | the CDI container and inject it into the bean. This proxy is not a normal mapper but a thread safe singleton 92 | so you should not worry about its scope. 93 |

94 | 95 |

96 | Any configuration problem that may happen during the initialization process will make the module throw a 97 | MyBatisCdiConfiurationException. Given that there is not much to configure, this 98 | exception is only thrown when the SqlSessionFactory cannot be found or is misconfigured. 99 |

100 | 101 |
102 | 103 | 104 | 105 |

106 | In case you have more than one SqlSessionFactory you can choose the one you want to use 107 | in any injection point by using: 108 |

109 | 110 |
    111 |
  • One or more qualifiers (defined by you)
  • 112 |
  • The name of the SqlSessionFactory (qualified with @Named)
  • 113 |
  • Any paranoid combination of both
  • 114 |
115 | 116 |

117 | Follows below an snippet that shows a producer that creates two SqlSessionFactorys. A qualifier is used 118 | to differentiate them as you would do with any other CDI bean: 119 |

120 | 121 | 137 | 138 |

139 | Now that there is no ambiguity you can select what factory you want to inject by adding the qualifier to it: 140 |

141 | 142 | 145 | 146 |

147 | You can also give a name to your SqlSessionFactory: 148 |

149 | 150 | 158 | 159 |

160 | And refer to it by its name in an injection point: 161 |

162 | 163 | 166 | 167 |
168 | 169 | 170 | 171 |

You can inject a thread safe SqlSession by requesting its injection as follows:

172 | 173 | 176 | 177 |

The factory selection criteria we saw for mappers apply also to SqlSession injection.

178 | 179 |

NOTE 180 | MyBatis-CDI controls the lifecycle of the injected SqlSession so you must not call any transactional method 181 | like commit() or rollback(), nor any lifecycle method like close(). 182 |

183 | 184 |
185 |
186 | 187 |
188 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/LocalTransactionInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.inject.Inject; 19 | import jakarta.interceptor.AroundInvoke; 20 | import jakarta.interceptor.Interceptor; 21 | import jakarta.interceptor.InvocationContext; 22 | import jakarta.transaction.HeuristicMixedException; 23 | import jakarta.transaction.HeuristicRollbackException; 24 | import jakarta.transaction.NotSupportedException; 25 | import jakarta.transaction.RollbackException; 26 | import jakarta.transaction.SystemException; 27 | 28 | import java.io.Serializable; 29 | import java.lang.reflect.InvocationTargetException; 30 | import java.lang.reflect.UndeclaredThrowableException; 31 | 32 | import org.apache.ibatis.session.SqlSessionManager; 33 | 34 | /** 35 | * Best-effort interceptor for local transactions. It locates all the instances of {@code SqlSssionManager} and starts 36 | * transactions on all them. It cannot guarantee atomiticy if there is more than one {@code SqlSssionManager}. Use XA 37 | * drivers, a JTA container and the {@link JtaTransactionInterceptor} in that case. 38 | * 39 | * @see JtaTransactionInterceptor 40 | * 41 | * @author Frank David Martínez 42 | */ 43 | @Transactional 44 | @Interceptor 45 | public class LocalTransactionInterceptor implements Serializable { 46 | 47 | private static final long serialVersionUID = 1L; 48 | 49 | @Inject 50 | private transient SqlSessionManagerRegistry registry; 51 | 52 | /** 53 | * Invoke. 54 | * 55 | * @param ctx 56 | * the ctx 57 | * 58 | * @return the object 59 | * 60 | * @throws Exception 61 | * the exception 62 | */ 63 | @AroundInvoke 64 | public Object invoke(InvocationContext ctx) throws Exception { 65 | Transactional transactional = getTransactionalAnnotation(ctx); 66 | boolean isInitiator = start(transactional); 67 | boolean isExternalJta = isTransactionActive(); 68 | if (isInitiator && !isExternalJta) { 69 | beginJta(); 70 | } 71 | boolean needsRollback = transactional.rollbackOnly(); 72 | Object result; 73 | try { 74 | result = ctx.proceed(); 75 | } catch (Exception ex) { 76 | Exception unwrapped = unwrapException(ex); 77 | needsRollback = needsRollback || needsRollback(transactional, unwrapped); 78 | throw unwrapped; 79 | } finally { 80 | if (isInitiator) { 81 | try { 82 | if (needsRollback) { 83 | rollback(transactional); 84 | } else { 85 | commit(transactional); 86 | } 87 | } finally { 88 | close(); 89 | endJta(isExternalJta, needsRollback); 90 | } 91 | } 92 | } 93 | return result; 94 | } 95 | 96 | /** 97 | * Checks if is transaction active. 98 | * 99 | * @return true, if is transaction active 100 | * 101 | * @throws SystemException 102 | * used by jtaTransactionInterceptor 103 | */ 104 | protected boolean isTransactionActive() throws SystemException { 105 | return false; 106 | } 107 | 108 | /** 109 | * Begin jta. 110 | * 111 | * @throws NotSupportedException 112 | * used by jtaTransactionInterceptor 113 | * @throws SystemException 114 | * used by jtaTransactionInterceptor 115 | */ 116 | protected void beginJta() throws NotSupportedException, SystemException { 117 | // nothing to do 118 | } 119 | 120 | /** 121 | * End jta. 122 | * 123 | * @param isExternaTransaction 124 | * the is externa transaction 125 | * @param commit 126 | * the commit 127 | * 128 | * @throws SystemException 129 | * used by jtaTransactionInterceptor 130 | * @throws RollbackException 131 | * used by jtaTransactionInterceptor 132 | * @throws HeuristicMixedException 133 | * used by jtaTransactionInterceptor 134 | * @throws HeuristicRollbackException 135 | * used by jtaTransactionInterceptor 136 | */ 137 | protected void endJta(boolean isExternaTransaction, boolean commit) 138 | throws SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException { 139 | // nothing to do 140 | } 141 | 142 | private boolean needsRollback(Transactional transactional, Throwable throwable) { 143 | if (RuntimeException.class.isAssignableFrom(throwable.getClass())) { 144 | return true; 145 | } 146 | for (Class exceptionClass : transactional.rollbackFor()) { 147 | if (exceptionClass.isAssignableFrom(throwable.getClass())) { 148 | return true; 149 | } 150 | } 151 | return false; 152 | } 153 | 154 | protected Transactional getTransactionalAnnotation(InvocationContext ctx) { 155 | Transactional t = ctx.getMethod().getAnnotation(Transactional.class); 156 | if (t == null) { 157 | t = ctx.getMethod().getDeclaringClass().getAnnotation(Transactional.class); 158 | } 159 | return t; 160 | } 161 | 162 | private boolean start(Transactional transactional) { 163 | boolean started = false; 164 | for (SqlSessionManager manager : this.registry.getManagers()) { 165 | if (!manager.isManagedSessionStarted()) { 166 | manager.startManagedSession(transactional.executorType(), 167 | transactional.isolation().getTransactionIsolationLevel()); 168 | started = true; 169 | } 170 | } 171 | return started; 172 | } 173 | 174 | private void commit(Transactional transactional) { 175 | for (SqlSessionManager manager : this.registry.getManagers()) { 176 | manager.commit(transactional.force()); 177 | } 178 | } 179 | 180 | private void rollback(Transactional transactional) { 181 | for (SqlSessionManager manager : this.registry.getManagers()) { 182 | manager.rollback(transactional.force()); 183 | } 184 | } 185 | 186 | private void close() { 187 | for (SqlSessionManager manager : this.registry.getManagers()) { 188 | manager.close(); 189 | } 190 | } 191 | 192 | private Exception unwrapException(Exception wrapped) { 193 | Throwable unwrapped = wrapped; 194 | while (true) { 195 | if (unwrapped instanceof InvocationTargetException) { 196 | unwrapped = ((InvocationTargetException) unwrapped).getTargetException(); 197 | } else if (unwrapped instanceof UndeclaredThrowableException) { 198 | unwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable(); 199 | } else if (!(unwrapped instanceof Exception)) { 200 | return new RuntimeException(unwrapped); 201 | } else { 202 | return (Exception) unwrapped; 203 | } 204 | } 205 | } 206 | 207 | } 208 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Apache Maven Wrapper startup batch script, version 3.3.4 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 28 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 29 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 30 | @REM e.g. to debug Maven itself, use 31 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 32 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 33 | @REM ---------------------------------------------------------------------------- 34 | 35 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 36 | @echo off 37 | @REM set title of command window 38 | title %0 39 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 40 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 41 | 42 | @REM set %HOME% to equivalent of $HOME 43 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 44 | 45 | @REM Execute a user defined script before this one 46 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 47 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 48 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 49 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 50 | :skipRcPre 51 | 52 | @setlocal 53 | 54 | set ERROR_CODE=0 55 | 56 | @REM To isolate internal variables from possible post scripts, we use another setlocal 57 | @setlocal 58 | 59 | @REM ==== START VALIDATION ==== 60 | if not "%JAVA_HOME%" == "" goto OkJHome 61 | 62 | echo. >&2 63 | echo Error: JAVA_HOME not found in your environment. >&2 64 | echo Please set the JAVA_HOME variable in your environment to match the >&2 65 | echo location of your Java installation. >&2 66 | echo. >&2 67 | goto error 68 | 69 | :OkJHome 70 | if exist "%JAVA_HOME%\bin\java.exe" goto init 71 | 72 | echo. >&2 73 | echo Error: JAVA_HOME is set to an invalid directory. >&2 74 | echo JAVA_HOME = "%JAVA_HOME%" >&2 75 | echo Please set the JAVA_HOME variable in your environment to match the >&2 76 | echo location of your Java installation. >&2 77 | echo. >&2 78 | goto error 79 | 80 | @REM ==== END VALIDATION ==== 81 | 82 | :init 83 | 84 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 85 | @REM Fallback to current working directory if not found. 86 | 87 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 88 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 89 | 90 | set EXEC_DIR=%CD% 91 | set WDIR=%EXEC_DIR% 92 | :findBaseDir 93 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 94 | cd .. 95 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 96 | set WDIR=%CD% 97 | goto findBaseDir 98 | 99 | :baseDirFound 100 | set MAVEN_PROJECTBASEDIR=%WDIR% 101 | cd "%EXEC_DIR%" 102 | goto endDetectBaseDir 103 | 104 | :baseDirNotFound 105 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 106 | cd "%EXEC_DIR%" 107 | 108 | :endDetectBaseDir 109 | 110 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 111 | 112 | @setlocal EnableExtensions EnableDelayedExpansion 113 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 114 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 115 | 116 | :endReadAdditionalConfig 117 | 118 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | 121 | @REM Maven main class is here to fix maven 4.0.0-beta-5 through 4.0.0-rc-4 122 | set MAVEN_MAIN_CLASS=org.apache.maven.cling.MavenCling 123 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 124 | 125 | set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.4/maven-wrapper-3.3.4.jar" 126 | 127 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 128 | IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B 129 | ) 130 | 131 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 132 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 133 | if exist %WRAPPER_JAR% ( 134 | if "%MVNW_VERBOSE%" == "true" ( 135 | echo Found %WRAPPER_JAR% 136 | ) 137 | ) else ( 138 | if not "%MVNW_REPOURL%" == "" ( 139 | SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.3.4/maven-wrapper-3.3.4.jar" 140 | ) 141 | if "%MVNW_VERBOSE%" == "true" ( 142 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 143 | echo Downloading from: %WRAPPER_URL% 144 | ) 145 | 146 | powershell -Command "&{"^ 147 | "$webclient = new-object System.Net.WebClient;"^ 148 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 149 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 150 | "}"^ 151 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ 152 | "}" 153 | if "%MVNW_VERBOSE%" == "true" ( 154 | echo Finished downloading %WRAPPER_JAR% 155 | ) 156 | ) 157 | @REM End of extension 158 | 159 | @REM If specified, validate the SHA-256 sum of the Maven wrapper jar file 160 | SET WRAPPER_SHA_256_SUM="" 161 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 162 | IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B 163 | ) 164 | IF NOT %WRAPPER_SHA_256_SUM%=="" ( 165 | powershell -Command "&{"^ 166 | "Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash;"^ 167 | "$hash = (Get-FileHash \"%WRAPPER_JAR%\" -Algorithm SHA256).Hash.ToLower();"^ 168 | "If('%WRAPPER_SHA_256_SUM%' -ne $hash){"^ 169 | " Write-Error 'Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised.';"^ 170 | " Write-Error 'Investigate or delete %WRAPPER_JAR% to attempt a clean download.';"^ 171 | " Write-Error 'If you updated your Maven version, you need to update the specified wrapperSha256Sum property.';"^ 172 | " exit 1;"^ 173 | "}"^ 174 | "}" 175 | if ERRORLEVEL 1 goto error 176 | ) 177 | 178 | @REM Provide a "standardized" way to retrieve the CLI args that will 179 | @REM work with both Windows and non-Windows executions. 180 | set MAVEN_CMD_LINE_ARGS=%* 181 | 182 | %MAVEN_JAVA_EXE% ^ 183 | %JVM_CONFIG_MAVEN_PROPS% ^ 184 | %MAVEN_OPTS% ^ 185 | %MAVEN_DEBUG_OPTS% ^ 186 | -classpath %WRAPPER_JAR% ^ 187 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 188 | "-Dmaven.mainClass=%MAVEN_MAIN_CLASS%" ^ 189 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 190 | if ERRORLEVEL 1 goto error 191 | goto end 192 | 193 | :error 194 | set ERROR_CODE=1 195 | 196 | :end 197 | @endlocal & set ERROR_CODE=%ERROR_CODE% 198 | 199 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 200 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 201 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 202 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 203 | :skipRcPost 204 | 205 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 206 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 207 | 208 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 209 | 210 | cmd /C exit /B %ERROR_CODE% 211 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/cdi/MybatisExtension.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2023 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 | * https://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 org.mybatis.cdi; 17 | 18 | import jakarta.enterprise.event.Observes; 19 | import jakarta.enterprise.inject.spi.AfterBeanDiscovery; 20 | import jakarta.enterprise.inject.spi.AnnotatedMember; 21 | import jakarta.enterprise.inject.spi.AnnotatedType; 22 | import jakarta.enterprise.inject.spi.Extension; 23 | import jakarta.enterprise.inject.spi.InjectionPoint; 24 | import jakarta.enterprise.inject.spi.InjectionTarget; 25 | import jakarta.enterprise.inject.spi.ProcessAnnotatedType; 26 | import jakarta.enterprise.inject.spi.ProcessInjectionTarget; 27 | import jakarta.enterprise.inject.spi.ProcessProducer; 28 | import jakarta.enterprise.inject.spi.WithAnnotations; 29 | import jakarta.inject.Named; 30 | import jakarta.inject.Qualifier; 31 | 32 | import java.lang.annotation.Annotation; 33 | import java.lang.reflect.Type; 34 | import java.util.ArrayList; 35 | import java.util.Collections; 36 | import java.util.HashSet; 37 | import java.util.List; 38 | import java.util.Set; 39 | 40 | import org.apache.ibatis.session.SqlSession; 41 | import org.apache.ibatis.session.SqlSessionFactory; 42 | import org.slf4j.Logger; 43 | import org.slf4j.LoggerFactory; 44 | 45 | /** 46 | * MyBatis CDI extension. 47 | * 48 | * @author Frank D. Martinez [mnesarco] 49 | */ 50 | public class MybatisExtension implements Extension { 51 | 52 | private static final Logger LOGGER = LoggerFactory.getLogger(MybatisExtension.class.getName()); 53 | 54 | private final Set sessionProducers = new HashSet<>(); 55 | 56 | private final Set mapperTypes = new HashSet<>(); 57 | 58 | private final Set injectionPoints = new HashSet<>(); 59 | 60 | /** 61 | * Collect types of all mappers annotated with Mapper. 62 | * 63 | * @param 64 | * the generic type 65 | * @param pat 66 | * the pat 67 | */ 68 | protected void processAnnotatedType( 69 | @Observes @WithAnnotations({ Mapper.class }) final ProcessAnnotatedType pat) { 70 | final AnnotatedType at = pat.getAnnotatedType(); 71 | if (at.isAnnotationPresent(Mapper.class)) { 72 | LOGGER.info("MyBatis CDI Module - Found class with @Mapper-annotation: {}", at.getJavaClass().getSimpleName()); 73 | this.mapperTypes.add(at.getBaseType()); 74 | } 75 | } 76 | 77 | /** 78 | * Collect all SqlSessionFactory producers annotated with SessionFactoryProvider. 79 | * 80 | * @param 81 | * the generic type 82 | * @param 83 | * the generic type 84 | * @param pp 85 | * the pp 86 | */ 87 | @SuppressWarnings("unchecked") 88 | protected void processProducer(@Observes final ProcessProducer pp) { 89 | final AnnotatedMember am = pp.getAnnotatedMember(); 90 | final boolean isAnnotated = am.isAnnotationPresent(SessionFactoryProvider.class); 91 | final boolean isSqlSessionFactory = am.getBaseType().equals(SqlSessionFactory.class); 92 | final String logData = String.join(".", am.getJavaMember().getDeclaringClass().getSimpleName(), 93 | am.getJavaMember().getName()); 94 | if (isAnnotated) { 95 | if (isSqlSessionFactory) { 96 | LOGGER.info("MyBatis CDI Module - SqlSessionFactory producer {}", logData); 97 | this.sessionProducers.add(new BeanKey((Class) (Type) SqlSession.class, am.getAnnotations())); 98 | } else { 99 | LOGGER.error("MyBatis CDI Module - Invalid return type (Must be SqlSessionFactory): {}", logData); 100 | pp.addDefinitionError(new MybatisCdiConfigurationException( 101 | String.format("SessionFactoryProvider producers must return SqlSessionFactory (%s)", logData))); 102 | } 103 | } else if (isSqlSessionFactory) { 104 | LOGGER.warn( 105 | "MyBatis CDI Module - Ignored SqlSessionFactory producer because it is not annotated with @SessionFactoryProvider: {}", 106 | logData); 107 | } 108 | } 109 | 110 | /** 111 | * Collect all targets to match Mappers and Session providers dependency. 112 | * 113 | * @param 114 | * the generic type 115 | * @param event 116 | * the event 117 | */ 118 | protected void processInjectionTarget(@Observes ProcessInjectionTarget event) { 119 | final InjectionTarget it = event.getInjectionTarget(); 120 | this.injectionPoints.addAll(it.getInjectionPoints()); 121 | } 122 | 123 | /** 124 | * Register all mybatis injectable beans. 125 | * 126 | * @param abd 127 | * the abd 128 | */ 129 | @SuppressWarnings("unchecked") 130 | protected void afterBeanDiscovery(@Observes final AfterBeanDiscovery abd) { 131 | LOGGER.info("MyBatis CDI Module - Activated"); 132 | 133 | Set mappers = new HashSet<>(); 134 | Set sessionTargets = new HashSet<>(); 135 | 136 | for (InjectionPoint ip : injectionPoints) { 137 | if (this.mapperTypes.contains(ip.getAnnotated().getBaseType())) { 138 | LOGGER.info("MyBatis CDI Module - Found a bean, which needs a Mapper {}", ip.getAnnotated().getBaseType()); 139 | mappers.add(new BeanKey((Class) ip.getAnnotated().getBaseType(), ip.getAnnotated().getAnnotations())); 140 | } else if (SqlSession.class.equals(ip.getAnnotated().getBaseType())) { 141 | sessionTargets 142 | .add(new BeanKey((Class) ip.getAnnotated().getBaseType(), ip.getAnnotated().getAnnotations())); 143 | } 144 | } 145 | this.injectionPoints.clear(); 146 | 147 | // Mappers ----------------------------------------------------------------- 148 | for (BeanKey key : mappers) { 149 | LOGGER.info("MyBatis CDI Module - Managed Mapper dependency: {}, {}", key.getKey(), key.type.getName()); 150 | abd.addBean(key.createBean()); 151 | } 152 | this.mapperTypes.clear(); 153 | 154 | // SqlSessionFactories ----------------------------------------------------- 155 | for (BeanKey key : this.sessionProducers) { 156 | LOGGER.info("MyBatis CDI Module - Managed SqlSession: {}, {}", key.getKey(), key.type.getName()); 157 | abd.addBean(key.createBean()); 158 | sessionTargets.remove(key); 159 | } 160 | this.sessionProducers.clear(); 161 | 162 | // Unmanaged SqlSession targets -------------------------------------------- 163 | for (BeanKey key : sessionTargets) { 164 | LOGGER.warn("MyBatis CDI Module - Unmanaged SqlSession: {}, {}", key.getKey(), key.type.getName()); 165 | } 166 | 167 | } 168 | 169 | /** 170 | * Unique key for fully qualified Mappers and Sessions. 171 | */ 172 | private static final class BeanKey implements Comparable { 173 | 174 | private final String key; 175 | 176 | private final List qualifiers; 177 | 178 | private final Class type; 179 | 180 | private final String sqlSessionManagerName; 181 | 182 | public BeanKey(Class type, Set annotations) { 183 | this.type = type; 184 | this.qualifiers = sort(filterQualifiers(annotations)); 185 | 186 | // Create key = type(.qualifier)*(.name)? 187 | final StringBuilder sb = new StringBuilder(); 188 | String name = null; 189 | sb.append(type.getName()); 190 | for (Annotation q : this.qualifiers) { 191 | if (q instanceof Named) { 192 | name = ((Named) q).value(); 193 | } else { 194 | sb.append(".").append(q.annotationType().getSimpleName()); 195 | } 196 | } 197 | if (name != null) { 198 | sb.append("_").append(name); 199 | } 200 | this.key = sb.toString(); 201 | this.sqlSessionManagerName = name; 202 | } 203 | 204 | private Set filterQualifiers(Set annotations) { 205 | final Set set = new HashSet<>(); 206 | for (Annotation a : annotations) { 207 | if (a.annotationType().isAnnotationPresent(Qualifier.class)) { 208 | set.add(a); 209 | } 210 | } 211 | return set; 212 | } 213 | 214 | private List sort(Set annotations) { 215 | final List list = new ArrayList<>(annotations); 216 | Collections.sort(list, (a, b) -> a.getClass().getName().compareTo(b.getClass().getName())); 217 | return list; 218 | } 219 | 220 | @Override 221 | public int compareTo(BeanKey o) { 222 | return this.key.compareTo(o.key); 223 | } 224 | 225 | @Override 226 | public int hashCode() { 227 | int hash = 3; 228 | return 43 * hash + (this.key != null ? this.key.hashCode() : 0); 229 | } 230 | 231 | @Override 232 | public boolean equals(Object obj) { 233 | if (obj == null || this.getClass() != obj.getClass()) { 234 | return false; 235 | } 236 | final BeanKey other = (BeanKey) obj; 237 | return !(this.key == null ? other.key != null : !this.key.equals(other.key)); 238 | } 239 | 240 | public MyBatisBean createBean() { 241 | return new MyBatisBean(this.key, this.type, new HashSet<>(this.qualifiers), this.sqlSessionManagerName); 242 | } 243 | 244 | public String getKey() { 245 | return this.key; 246 | } 247 | 248 | @Override 249 | public String toString() { 250 | return this.key; 251 | } 252 | 253 | } 254 | 255 | } 256 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 4.0.0 22 | 23 | 24 | org.mybatis 25 | mybatis-parent 26 | 51 27 | 28 | 29 | 30 | org.mybatis 31 | mybatis-cdi 32 | 2.1.2-SNAPSHOT 33 | 34 | mybatis-cdi 35 | https://www.mybatis.org/cdi/ 36 | 37 | 2013 38 | 39 | 40 | scm:git:ssh://git@github.com/mybatis/cdi.git 41 | scm:git:ssh://git@github.com/mybatis/cdi.git 42 | HEAD 43 | https://github.com/mybatis/cdi/ 44 | 45 | 46 | GitHub 47 | https://github.com/mybatis/cdi/issues 48 | 49 | 50 | Github 51 | https://github.com/mybatis/cdi/actions 52 | 53 | 54 | 55 | gh-pages-scm 56 | Mybatis GitHub Pages 57 | scm:git:ssh://git@github.com/mybatis/cdi.git 58 | 59 | 60 | 61 | 62 | 63 | 17 64 | 17 65 | 66 | 67 | 1.18.2 68 | 10.17.1.0 69 | 5.21.0 70 | 2.0.17 71 | 72 | 73 | 2.0.0 74 | org.mybatis.cdi.* 75 | 76 | 77 | org.mybatis.cdi 78 | 79 | 80 | * 81 | 82 | 83 | 1759689584 84 | 85 | 86 | -javaagent:${settings.localRepository}/net/bytebuddy/byte-buddy-agent/${byte-buddy.version}/byte-buddy-agent-${byte-buddy.version}.jar 87 | 88 | 89 | 90 | 91 | 92 | org.mybatis 93 | mybatis 94 | 3.5.19 95 | provided 96 | 97 | 98 | 99 | 100 | jakarta.annotation 101 | jakarta.annotation-api 102 | 3.0.0 103 | provided 104 | 105 | 106 | jakarta.transaction 107 | jakarta.transaction-api 108 | 2.0.1 109 | provided 110 | 111 | 112 | jakarta.enterprise 113 | jakarta.enterprise.cdi-api 114 | 4.1.0 115 | provided 116 | 117 | 118 | 119 | 120 | org.jboss.classfilewriter 121 | jboss-classfilewriter 122 | 1.3.1.Final 123 | provided 124 | 125 | 126 | org.jboss.weld.se 127 | weld-se-core 128 | 6.0.3.Final 129 | test 130 | 131 | 132 | 133 | 134 | net.bytebuddy 135 | byte-buddy 136 | ${byte-buddy.version} 137 | test 138 | 139 | 140 | net.bytebuddy 141 | byte-buddy-agent 142 | ${byte-buddy.version} 143 | test 144 | 145 | 146 | org.junit.jupiter 147 | junit-jupiter-engine 148 | 6.0.1 149 | test 150 | 151 | 152 | org.mockito 153 | mockito-core 154 | ${mockito.version} 155 | test 156 | 157 | 158 | org.mockito 159 | mockito-junit-jupiter 160 | ${mockito.version} 161 | test 162 | 163 | 164 | org.jboss.weld 165 | weld-junit5 166 | 5.0.3.Final 167 | test 168 | 169 | 170 | 171 | 172 | org.jboss.narayana.jta 173 | narayana-jta 174 | 7.3.3.Final 175 | test 176 | 177 | 178 | org.apache.derby 179 | derby 180 | ${derby.version} 181 | test 182 | 183 | 184 | org.apache.derby 185 | derbyshared 186 | ${derby.version} 187 | test 188 | 189 | 190 | org.apache.derby 191 | derbyoptionaltools 192 | ${derby.version} 193 | test 194 | 195 | 196 | 197 | 198 | org.slf4j 199 | slf4j-api 200 | ${slf4j.version} 201 | compile 202 | 203 | 204 | org.slf4j 205 | slf4j-simple 206 | ${slf4j.version} 207 | test 208 | 209 | 210 | 211 | 212 | 213 | 214 | org.apache.maven.plugins 215 | maven-compiler-plugin 216 | 217 | 218 | 219 | net.bytebuddy 220 | byte-buddy-agent 221 | ${byte-buddy.version} 222 | 223 | 224 | 225 | 226 | 227 | org.apache.maven.plugins 228 | maven-surefire-plugin 229 | 230 | 231 | 232 | derby.stream.error.file 233 | ${project.build.directory}/derby.log 234 | 235 | 236 | 237 | 238 | 239 | default-test 240 | 241 | 242 | **/FooServiceJTATest.java 243 | 244 | 245 | 246 | 247 | jta-test 248 | 249 | test 250 | 251 | 252 | 253 | **/FooServiceTest.java 254 | **/MybatisExtensionTest.java 255 | 256 | 257 | 258 | 259 | 260 | 261 | org.apache.maven.plugins 262 | maven-site-plugin 263 | 264 | 265 | default,zh_CN 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | derby-17-18 274 | 275 | [17,19) 276 | 277 | 278 | 10.16.1.1 279 | 280 | 281 | 282 | 283 | 284 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | https://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | https://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.3.4 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | # e.g. to debug Maven itself, use 32 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | # ---------------------------------------------------------------------------- 35 | 36 | if [ -z "$MAVEN_SKIP_RC" ]; then 37 | 38 | if [ -f /usr/local/etc/mavenrc ]; then 39 | . /usr/local/etc/mavenrc 40 | fi 41 | 42 | if [ -f /etc/mavenrc ]; then 43 | . /etc/mavenrc 44 | fi 45 | 46 | if [ -f "$HOME/.mavenrc" ]; then 47 | . "$HOME/.mavenrc" 48 | fi 49 | 50 | fi 51 | 52 | # OS specific support. $var _must_ be set to either true or false. 53 | cygwin=false 54 | darwin=false 55 | mingw=false 56 | case "$(uname)" in 57 | CYGWIN*) cygwin=true ;; 58 | MINGW*) mingw=true ;; 59 | Darwin*) 60 | darwin=true 61 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 62 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 63 | if [ -z "$JAVA_HOME" ]; then 64 | if [ -x "/usr/libexec/java_home" ]; then 65 | JAVA_HOME="$(/usr/libexec/java_home)" 66 | export JAVA_HOME 67 | else 68 | JAVA_HOME="/Library/Java/Home" 69 | export JAVA_HOME 70 | fi 71 | fi 72 | ;; 73 | esac 74 | 75 | if [ -z "$JAVA_HOME" ]; then 76 | if [ -r /etc/gentoo-release ]; then 77 | JAVA_HOME=$(java-config --jre-home) 78 | fi 79 | fi 80 | 81 | # For Cygwin, ensure paths are in UNIX format before anything is touched 82 | if $cygwin; then 83 | [ -n "$JAVA_HOME" ] \ 84 | && JAVA_HOME=$(cygpath --unix "$JAVA_HOME") 85 | [ -n "$CLASSPATH" ] \ 86 | && CLASSPATH=$(cygpath --path --unix "$CLASSPATH") 87 | fi 88 | 89 | # For Mingw, ensure paths are in UNIX format before anything is touched 90 | if $mingw; then 91 | [ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] \ 92 | && JAVA_HOME="$( 93 | cd "$JAVA_HOME" || ( 94 | echo "cannot cd into $JAVA_HOME." >&2 95 | exit 1 96 | ) 97 | pwd 98 | )" 99 | fi 100 | 101 | if [ -z "$JAVA_HOME" ]; then 102 | javaExecutable="$(which javac)" 103 | if [ -n "$javaExecutable" ] && ! [ "$(expr "$javaExecutable" : '\([^ ]*\)')" = "no" ]; then 104 | # readlink(1) is not available as standard on Solaris 10. 105 | readLink=$(which readlink) 106 | if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then 107 | if $darwin; then 108 | javaHome="$(dirname "$javaExecutable")" 109 | javaExecutable="$(cd "$javaHome" && pwd -P)/javac" 110 | else 111 | javaExecutable="$(readlink -f "$javaExecutable")" 112 | fi 113 | javaHome="$(dirname "$javaExecutable")" 114 | javaHome=$(expr "$javaHome" : '\(.*\)/bin') 115 | JAVA_HOME="$javaHome" 116 | export JAVA_HOME 117 | fi 118 | fi 119 | fi 120 | 121 | if [ -z "$JAVACMD" ]; then 122 | if [ -n "$JAVA_HOME" ]; then 123 | if [ -x "$JAVA_HOME/jre/sh/java" ]; then 124 | # IBM's JDK on AIX uses strange locations for the executables 125 | JAVACMD="$JAVA_HOME/jre/sh/java" 126 | else 127 | JAVACMD="$JAVA_HOME/bin/java" 128 | fi 129 | else 130 | JAVACMD="$( 131 | \unset -f command 2>/dev/null 132 | \command -v java 133 | )" 134 | fi 135 | fi 136 | 137 | if [ ! -x "$JAVACMD" ]; then 138 | echo "Error: JAVA_HOME is not defined correctly." >&2 139 | echo " We cannot execute $JAVACMD" >&2 140 | exit 1 141 | fi 142 | 143 | if [ -z "$JAVA_HOME" ]; then 144 | echo "Warning: JAVA_HOME environment variable is not set." >&2 145 | fi 146 | 147 | # traverses directory structure from process work directory to filesystem root 148 | # first directory with .mvn subdirectory is considered project base directory 149 | find_maven_basedir() { 150 | if [ -z "$1" ]; then 151 | echo "Path not specified to find_maven_basedir" >&2 152 | return 1 153 | fi 154 | 155 | basedir="$1" 156 | wdir="$1" 157 | while [ "$wdir" != '/' ]; do 158 | if [ -d "$wdir"/.mvn ]; then 159 | basedir=$wdir 160 | break 161 | fi 162 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 163 | if [ -d "${wdir}" ]; then 164 | wdir=$( 165 | cd "$wdir/.." || exit 1 166 | pwd 167 | ) 168 | fi 169 | # end of workaround 170 | done 171 | printf '%s' "$( 172 | cd "$basedir" || exit 1 173 | pwd 174 | )" 175 | } 176 | 177 | # concatenates all lines of a file 178 | concat_lines() { 179 | if [ -f "$1" ]; then 180 | # Remove \r in case we run on Windows within Git Bash 181 | # and check out the repository with auto CRLF management 182 | # enabled. Otherwise, we may read lines that are delimited with 183 | # \r\n and produce $'-Xarg\r' rather than -Xarg due to word 184 | # splitting rules. 185 | tr -s '\r\n' ' ' <"$1" 186 | fi 187 | } 188 | 189 | log() { 190 | if [ "$MVNW_VERBOSE" = true ]; then 191 | printf '%s\n' "$1" 192 | fi 193 | } 194 | 195 | BASE_DIR=$(find_maven_basedir "$(dirname "$0")") 196 | if [ -z "$BASE_DIR" ]; then 197 | exit 1 198 | fi 199 | 200 | MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 201 | export MAVEN_PROJECTBASEDIR 202 | log "$MAVEN_PROJECTBASEDIR" 203 | 204 | trim() { 205 | # MWRAPPER-139: 206 | # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. 207 | # Needed for removing poorly interpreted newline sequences when running in more 208 | # exotic environments such as mingw bash on Windows. 209 | printf "%s" "${1}" | tr -d '[:space:]' 210 | } 211 | 212 | ########################################################################################## 213 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 214 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 215 | ########################################################################################## 216 | wrapperJarPath="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" 217 | if [ -r "$wrapperJarPath" ]; then 218 | log "Found $wrapperJarPath" 219 | else 220 | log "Couldn't find $wrapperJarPath, downloading it ..." 221 | 222 | if [ -n "$MVNW_REPOURL" ]; then 223 | wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.3.4/maven-wrapper-3.3.4.jar" 224 | else 225 | wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.4/maven-wrapper-3.3.4.jar" 226 | fi 227 | while IFS="=" read -r key value; do 228 | case "$key" in wrapperUrl) 229 | wrapperUrl=$(trim "${value-}") 230 | break 231 | ;; 232 | esac 233 | done <"$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" 234 | log "Downloading from: $wrapperUrl" 235 | 236 | if $cygwin; then 237 | wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath") 238 | fi 239 | 240 | if command -v wget >/dev/null; then 241 | log "Found wget ... using wget" 242 | [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--quiet" 243 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 244 | wget ${QUIET:+"$QUIET"} "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 245 | else 246 | wget ${QUIET:+"$QUIET"} --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 247 | fi 248 | elif command -v curl >/dev/null; then 249 | log "Found curl ... using curl" 250 | [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--silent" 251 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 252 | curl ${QUIET:+"$QUIET"} -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" 253 | else 254 | curl ${QUIET:+"$QUIET"} --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" 255 | fi 256 | else 257 | log "Falling back to using Java to download" 258 | javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java" 259 | javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class" 260 | # For Cygwin, switch paths to Windows format before running javac 261 | if $cygwin; then 262 | javaSource=$(cygpath --path --windows "$javaSource") 263 | javaClass=$(cygpath --path --windows "$javaClass") 264 | fi 265 | if [ -e "$javaSource" ]; then 266 | if [ ! -e "$javaClass" ]; then 267 | log " - Compiling MavenWrapperDownloader.java ..." 268 | ("$JAVA_HOME/bin/javac" "$javaSource") 269 | fi 270 | if [ -e "$javaClass" ]; then 271 | log " - Running MavenWrapperDownloader.java ..." 272 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath" 273 | fi 274 | fi 275 | fi 276 | fi 277 | ########################################################################################## 278 | # End of extension 279 | ########################################################################################## 280 | 281 | # If specified, validate the SHA-256 sum of the Maven wrapper jar file 282 | wrapperSha256Sum="" 283 | while IFS="=" read -r key value; do 284 | case "$key" in wrapperSha256Sum) 285 | wrapperSha256Sum=$(trim "${value-}") 286 | break 287 | ;; 288 | esac 289 | done <"$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" 290 | if [ -n "$wrapperSha256Sum" ]; then 291 | wrapperSha256Result=false 292 | if command -v sha256sum >/dev/null; then 293 | if echo "$wrapperSha256Sum $wrapperJarPath" | sha256sum -c - >/dev/null 2>&1; then 294 | wrapperSha256Result=true 295 | fi 296 | elif command -v shasum >/dev/null; then 297 | if echo "$wrapperSha256Sum $wrapperJarPath" | shasum -a 256 -c >/dev/null 2>&1; then 298 | wrapperSha256Result=true 299 | fi 300 | else 301 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 302 | echo "Please install either command, or disable validation by removing 'wrapperSha256Sum' from your maven-wrapper.properties." >&2 303 | exit 1 304 | fi 305 | if [ $wrapperSha256Result = false ]; then 306 | echo "Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised." >&2 307 | echo "Investigate or delete $wrapperJarPath to attempt a clean download." >&2 308 | echo "If you updated your Maven version, you need to update the specified wrapperSha256Sum property." >&2 309 | exit 1 310 | fi 311 | fi 312 | 313 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 314 | 315 | # For Cygwin, switch paths to Windows format before running java 316 | if $cygwin; then 317 | [ -n "$JAVA_HOME" ] \ 318 | && JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME") 319 | [ -n "$CLASSPATH" ] \ 320 | && CLASSPATH=$(cygpath --path --windows "$CLASSPATH") 321 | [ -n "$MAVEN_PROJECTBASEDIR" ] \ 322 | && MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR") 323 | fi 324 | 325 | # Provide a "standardized" way to retrieve the CLI args that will 326 | # work with both Windows and non-Windows executions. 327 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*" 328 | export MAVEN_CMD_LINE_ARGS 329 | 330 | # Maven main class is here to fix maven 4.0.0-beta-5 through 4.0.0-rc-4 331 | MAVEN_MAIN_CLASS=org.apache.maven.cling.MavenCling 332 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 333 | 334 | # shellcheck disable=SC2086 # safe args 335 | exec "$JAVACMD" \ 336 | $MAVEN_OPTS \ 337 | $MAVEN_DEBUG_OPTS \ 338 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 339 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 340 | "-Dmaven.mainClass=${MAVEN_MAIN_CLASS}" \ 341 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 342 | --------------------------------------------------------------------------------