├── .gitattributes ├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ └── maven-wrapper.properties ├── .travis.yml ├── ISSUE_TEMPLATE.md ├── LICENSE ├── NOTICE ├── README.md ├── license.txt ├── mvnw ├── mvnw.cmd ├── mybatis-spring.ipr ├── pom.xml ├── src ├── changes │ └── changes.xml ├── main │ └── java │ │ ├── META-INF │ │ ├── spring.handlers │ │ └── spring.schemas │ │ └── org │ │ └── mybatis │ │ ├── logging │ │ ├── Logger.java │ │ └── LoggerFactory.java │ │ └── spring │ │ ├── MyBatisExceptionTranslator.java │ │ ├── MyBatisSystemException.java │ │ ├── SqlSessionFactoryBean.java │ │ ├── SqlSessionHolder.java │ │ ├── SqlSessionTemplate.java │ │ ├── SqlSessionUtils.java │ │ ├── annotation │ │ ├── MapperScan.java │ │ ├── MapperScannerRegistrar.java │ │ ├── MapperScans.java │ │ └── package-info.java │ │ ├── batch │ │ ├── MyBatisBatchItemWriter.java │ │ ├── MyBatisCursorItemReader.java │ │ ├── MyBatisPagingItemReader.java │ │ ├── builder │ │ │ ├── MyBatisBatchItemWriterBuilder.java │ │ │ ├── MyBatisCursorItemReaderBuilder.java │ │ │ ├── MyBatisPagingItemReaderBuilder.java │ │ │ └── package-info.java │ │ └── package-info.java │ │ ├── config │ │ ├── MapperScannerBeanDefinitionParser.java │ │ ├── NamespaceHandler.java │ │ ├── mybatis-spring.xsd │ │ └── package-info.java │ │ ├── mapper │ │ ├── ClassPathMapperScanner.java │ │ ├── MapperFactoryBean.java │ │ ├── MapperScannerConfigurer.java │ │ └── package-info.java │ │ ├── package-info.java │ │ ├── support │ │ ├── SqlSessionDaoSupport.java │ │ └── package-info.java │ │ └── transaction │ │ ├── SpringManagedTransaction.java │ │ ├── SpringManagedTransactionFactory.java │ │ └── package-info.java ├── site │ ├── es │ │ └── xdoc │ │ │ ├── batch.xml │ │ │ ├── boot.xml │ │ │ ├── factorybean.xml │ │ │ ├── getting-started.xml.vm │ │ │ ├── index.xml │ │ │ ├── mappers.xml │ │ │ ├── sample.xml │ │ │ ├── sqlsession.xml │ │ │ ├── transactions.xml │ │ │ └── using-api.xml │ ├── ja │ │ └── xdoc │ │ │ ├── batch.xml │ │ │ ├── boot.xml │ │ │ ├── factorybean.xml │ │ │ ├── getting-started.xml.vm │ │ │ ├── index.xml │ │ │ ├── mappers.xml │ │ │ ├── sample.xml │ │ │ ├── sqlsession.xml │ │ │ ├── transactions.xml │ │ │ └── using-api.xml │ ├── ko │ │ └── xdoc │ │ │ ├── batch.xml │ │ │ ├── boot.xml │ │ │ ├── factorybean.xml │ │ │ ├── getting-started.xml.vm │ │ │ ├── index.xml │ │ │ ├── mappers.xml │ │ │ ├── sample.xml │ │ │ ├── sqlsession.xml │ │ │ ├── transactions.xml │ │ │ └── using-api.xml │ ├── resources │ │ ├── css │ │ │ └── site.css │ │ ├── es │ │ │ └── css │ │ │ │ └── site.css │ │ ├── images │ │ │ ├── en.png │ │ │ ├── es.png │ │ │ ├── ja.png │ │ │ ├── ko.png │ │ │ └── zh.png │ │ ├── ja │ │ │ └── css │ │ │ │ └── site.css │ │ ├── ko │ │ │ └── css │ │ │ │ └── site.css │ │ └── zh │ │ │ └── css │ │ │ └── site.css │ ├── site.xml │ ├── site_es.xml │ ├── site_ja.xml │ ├── site_ko.xml │ ├── site_zh.xml │ ├── xdoc │ │ ├── batch.xml │ │ ├── boot.xml │ │ ├── factorybean.xml │ │ ├── getting-started.xml.vm │ │ ├── index.xml │ │ ├── mappers.xml │ │ ├── sample.xml │ │ ├── sqlsession.xml │ │ ├── transactions.xml │ │ └── using-api.xml │ └── zh │ │ └── xdoc │ │ ├── batch.xml │ │ ├── boot.xml │ │ ├── factorybean.xml │ │ ├── getting-started.xml.vm │ │ ├── index.xml │ │ ├── mappers.xml │ │ ├── sample.xml │ │ ├── sqlsession.xml │ │ ├── transactions.xml │ │ └── using-api.xml └── test │ ├── java │ └── org │ │ └── mybatis │ │ └── spring │ │ ├── AbstractMyBatisSpringTest.java │ │ ├── ExecutorInterceptor.java │ │ ├── MyBatisSpringTest.java │ │ ├── PooledMockDataSource.java │ │ ├── SqlSessionFactoryBeanTest.java │ │ ├── SqlSessionTemplateTest.java │ │ ├── TestMapper.java │ │ ├── TestMapper.xml │ │ ├── annotation │ │ ├── MapperScanTest.java │ │ └── mapper │ │ │ ├── ds1 │ │ │ └── Ds1Mapper.java │ │ │ └── ds2 │ │ │ └── Ds2Mapper.java │ │ ├── asyncsynchronization │ │ ├── AsyncAfterCompletionHelper.java │ │ └── SqlSessionTemplateAsyncAfterCompletionTest.java │ │ ├── batch │ │ ├── MyBatisBatchItemWriterTest.java │ │ ├── MyBatisCursorItemReaderTest.java │ │ ├── SpringBatchTest.java │ │ ├── applicationContext.xml │ │ ├── builder │ │ │ ├── MyBatisBatchItemWriterBuilderTest.java │ │ │ ├── MyBatisCursorItemReaderBuilderTest.java │ │ │ └── MyBatisPagingItemReaderBuilderTest.java │ │ ├── dao │ │ │ └── EmployeeMapper.xml │ │ ├── db │ │ │ ├── database-schema.sql │ │ │ └── database-test-data.sql │ │ └── domain │ │ │ └── Employee.java │ │ ├── config │ │ ├── NamespaceTest.java │ │ ├── annotation.xml │ │ ├── base-package.xml │ │ ├── factory-ref.xml │ │ ├── marker-and-annotation.xml │ │ ├── marker-interface.xml │ │ ├── name-generator.xml │ │ └── template-ref.xml │ │ ├── mapper │ │ ├── AnnotatedMapper.java │ │ ├── AnnotatedMapperZeroMethods.java │ │ ├── MapperClass.java │ │ ├── MapperFactoryBeanTest.java │ │ ├── MapperImplementation.java │ │ ├── MapperInterface.java │ │ ├── MapperScannerConfigurerTest.java │ │ ├── MapperSubinterface.java │ │ └── child │ │ │ └── MapperChildInterface.java │ │ ├── mybatis-config.xml │ │ ├── sample │ │ ├── AbstractSampleJobTest.java │ │ ├── AbstractSampleTest.java │ │ ├── SampleBatchTest.java │ │ ├── SampleEnableTest.java │ │ ├── SampleJavaConfigTest.java │ │ ├── SampleJobJavaConfigTest.java │ │ ├── SampleJobXmlConfigTest.java │ │ ├── SampleMapperTest.java │ │ ├── SampleNamespaceTest.java │ │ ├── SampleScannerTest.java │ │ ├── SampleSqlSessionTest.java │ │ ├── batch │ │ │ └── UserToPersonItemProcessor.java │ │ ├── config │ │ │ ├── SampleConfig.java │ │ │ ├── SampleJobConfig.java │ │ │ ├── applicationContext-batch.xml │ │ │ ├── applicationContext-infrastructure.xml │ │ │ ├── applicationContext-job.xml │ │ │ ├── applicationContext-mapper.xml │ │ │ ├── applicationContext-namespace.xml │ │ │ ├── applicationContext-scanner.xml │ │ │ └── applicationContext-sqlsession.xml │ │ ├── dao │ │ │ ├── UserDao.java │ │ │ └── UserDaoImpl.java │ │ ├── db │ │ │ ├── database-schema.sql │ │ │ └── database-test-data.sql │ │ ├── domain │ │ │ ├── Person.java │ │ │ └── User.java │ │ ├── mapper │ │ │ ├── PersonMapper.xml │ │ │ ├── UserMapper.java │ │ │ ├── UserMapper.xml │ │ │ └── package-info.java │ │ ├── package-info.java │ │ └── service │ │ │ ├── BarService.java │ │ │ └── FooService.java │ │ ├── submitted │ │ ├── autowire │ │ │ ├── AutowireTest.java │ │ │ ├── BarMapper.java │ │ │ ├── BarMapper.xml │ │ │ ├── FooMapper.java │ │ │ ├── FooMapper.xml │ │ │ ├── IBar.java │ │ │ ├── IFoo.java │ │ │ ├── database-schema-bar.sql │ │ │ ├── database-schema-foo.sql │ │ │ └── spring.xml │ │ ├── webapp_placeholder │ │ │ ├── Mapper.java │ │ │ ├── WebappPlaceholderTest.java │ │ │ ├── conf.properties │ │ │ └── spring.xml │ │ └── xa │ │ │ ├── User.java │ │ │ ├── UserMapper.java │ │ │ ├── UserService.java │ │ │ ├── UserServiceImpl.java │ │ │ ├── UserServiceTest.java │ │ │ ├── applicationContext.xml │ │ │ └── database-schema.sql │ │ ├── support │ │ └── SqlSessionDaoSupportTest.java │ │ ├── transaction │ │ └── SpringTransactionManagerTest.java │ │ └── type │ │ ├── DummyMapperFactoryBean.java │ │ ├── DummyTypeAlias.java │ │ ├── DummyTypeAlias2.java │ │ ├── DummyTypeHandler.java │ │ ├── DummyTypeHandler2.java │ │ └── SuperType.java │ └── resources │ └── log4j2-test.xml └── travis ├── after_success.sh └── settings.xml /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /.settings 3 | /.classpath 4 | /.project 5 | *.epoch 6 | *.log 7 | *.iml 8 | /.idea 9 | .mvn/wrapper/maven-wrapper.jar 10 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar 3 | 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | 4 | matrix: 5 | include: 6 | # 11 7 | - env: JDK='Oracle JDK 11' 8 | jdk: oraclejdk11 9 | - env: JDK='OpenJDK 11' 10 | jdk: openjdk11 11 | # 10 12 | - env: JDK='OpenJDK 10' 13 | install: . ./install-jdk.sh -F 10 -L GPL 14 | # 9 15 | - env: JDK='Oracle JDK 9' 16 | jdk: oraclejdk9 17 | - env: JDK='OpenJDK 9' 18 | install: . ./install-jdk.sh -F 9 19 | # 8 20 | - env: JDK='Oracle JDK 8' 21 | jdk: oraclejdk8 22 | - env: JDK='OpenJDK 8' 23 | jdk: openjdk8 24 | 25 | before_install: 26 | - wget https://raw.githubusercontent.com/sormuras/bach/master/install-jdk.sh 27 | - echo "MAVEN_OPTS='-Dlicense.skip=true'" > ~/.mavenrc 28 | - ./mvnw install -q -DskipTests=true 29 | 30 | install: true 31 | 32 | script: ./mvnw clean verify 33 | 34 | after_success: 35 | - chmod -R 777 ./travis/after_success.sh 36 | - ./travis/after_success.sh 37 | 38 | env: 39 | global: 40 | - secure: "dPX83x9q53WLJjNEsOJZTj2yjcusMp3Rg2SeF9xGUgxLa0NAdiWKM/ejSiJO\nzRM+5nmnQGb4SSigqb99N0ndB0S5YhMzbpACD8+SmCfpYRPdUQtE5dW22xpd\nPGPPUwPAGcqISwq5lcFCbPeve8k4g5Co/ZWGMMkoYy8DfjRss6g=" 41 | - secure: "X/+tqgmKqR6wIvSLzDyYAc3Q0NtzFjnQZT7b4yD9MI+/9S1bLFyZJ8mLhymK\nEakyrz8syFAl38ebl2pGox3yaJ9GRfcQXjJ2Qv0Pgb0r+RQTrtU2Fpvb+3Nr\nh5ymtRIkU0XOpOBCq5M9AxV1TESftw3p1IUD9dzPilRqQdXzTGs=" 42 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 9 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | MyBatis Spring 2 | Copyright 2010-2013 3 | 4 | This product includes software developed by 5 | The MyBatis Team (http://www.mybatis.org/). 6 | 7 | iBATIS 8 | This product includes software developed by 9 | The Apache Software Foundation (http://www.apache.org/). 10 | 11 | Copyright 2010 The Apache Software Foundation 12 | 13 | Licensed under the Apache License, Version 2.0 (the "License"); 14 | you may not use this file except in compliance with the License. 15 | You may obtain a copy of the License at 16 | 17 | http://www.apache.org/licenses/LICENSE-2.0 18 | 19 | Unless required by applicable law or agreed to in writing, software 20 | distributed under the License is distributed on an "AS IS" BASIS, 21 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | See the License for the specific language governing permissions and 23 | limitations under the License. 24 | 25 | Spring Framework 26 | All Spring projects are licensed under the terms of the Apache License, Version 2.0 27 | 28 | Copyright 2002-2010 the original author or authors 29 | 30 | Licensed under the Apache License, Version 2.0 (the "License"); 31 | you may not use this file except in compliance with the License. 32 | You may obtain a copy of the License at 33 | 34 | http://www.apache.org/licenses/LICENSE-2.0 35 | 36 | Unless required by applicable law or agreed to in writing, software 37 | distributed under the License is distributed on an "AS IS" BASIS, 38 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 39 | See the License for the specific language governing permissions and 40 | limitations under the License. 41 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 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 | http://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 | -------------------------------------------------------------------------------- /src/main/java/META-INF/spring.handlers: -------------------------------------------------------------------------------- 1 | http\://mybatis.org/schema/mybatis-spring=org.mybatis.spring.config.NamespaceHandler -------------------------------------------------------------------------------- /src/main/java/META-INF/spring.schemas: -------------------------------------------------------------------------------- 1 | http\://mybatis.org/schema/mybatis-spring-1.2.xsd=org/mybatis/spring/config/mybatis-spring.xsd 2 | http\://mybatis.org/schema/mybatis-spring.xsd=org/mybatis/spring/config/mybatis-spring.xsd -------------------------------------------------------------------------------- /src/main/java/org/mybatis/logging/Logger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.logging; 17 | 18 | import org.apache.ibatis.logging.Log; 19 | 20 | import java.util.function.Supplier; 21 | 22 | /** 23 | * Wrapper of {@link Log}, allow log with lambda expressions. 24 | * 25 | * @author Putthiphong Boonphong 26 | */ 27 | public class Logger { 28 | 29 | private final Log log; 30 | 31 | Logger(Log log) { 32 | this.log = log; 33 | } 34 | 35 | public void error(Supplier s, Throwable e) { 36 | log.error(s.get(), e); 37 | } 38 | 39 | public void error(Supplier s) { 40 | log.error(s.get()); 41 | } 42 | 43 | public void warn(Supplier s) { 44 | log.warn(s.get()); 45 | } 46 | 47 | public void debug(Supplier s) { 48 | if (log.isDebugEnabled()) { 49 | log.debug(s.get()); 50 | } 51 | } 52 | 53 | public void trace(Supplier s) { 54 | if (log.isTraceEnabled()) { 55 | log.trace(s.get()); 56 | } 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /src/main/java/org/mybatis/logging/LoggerFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.logging; 17 | 18 | import org.apache.ibatis.logging.LogFactory; 19 | 20 | /** 21 | * LoggerFactory is a wrapper around {@link LogFactory} to support {@link Logger}. 22 | * 23 | * @author Putthiphong Boonphong 24 | */ 25 | public class LoggerFactory { 26 | 27 | public static Logger getLogger(Class aClass) { 28 | return new Logger(LogFactory.getLog(aClass)); 29 | } 30 | 31 | public static Logger getLogger(String logger) { 32 | return new Logger(LogFactory.getLog(logger)); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring; 17 | 18 | import org.apache.ibatis.exceptions.PersistenceException; 19 | import org.springframework.dao.DataAccessException; 20 | import org.springframework.dao.support.PersistenceExceptionTranslator; 21 | import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; 22 | import org.springframework.jdbc.support.SQLExceptionTranslator; 23 | import org.springframework.transaction.TransactionException; 24 | 25 | import javax.sql.DataSource; 26 | import java.sql.SQLException; 27 | 28 | /** 29 | * Default exception translator. 30 | * 31 | * Translates MyBatis SqlSession returned exception into a Spring 32 | * {@code DataAccessException} using Spring's {@code SQLExceptionTranslator} 33 | * Can load {@code SQLExceptionTranslator} eagerly or when the 34 | * first exception is translated. 35 | * 36 | * @author Eduardo Macarron 37 | */ 38 | public class MyBatisExceptionTranslator implements PersistenceExceptionTranslator { 39 | 40 | private final DataSource dataSource; 41 | 42 | private SQLExceptionTranslator exceptionTranslator; 43 | 44 | /** 45 | * Creates a new {@code DataAccessExceptionTranslator} instance. 46 | * 47 | * @param dataSource DataSource to use to find metadata and establish which error codes are usable. 48 | * @param exceptionTranslatorLazyInit if true, the translator instantiates internal stuff only the first time will 49 | * have the need to translate exceptions. 50 | */ 51 | public MyBatisExceptionTranslator(DataSource dataSource, boolean exceptionTranslatorLazyInit) { 52 | this.dataSource = dataSource; 53 | 54 | if (!exceptionTranslatorLazyInit) { 55 | this.initExceptionTranslator(); 56 | } 57 | } 58 | 59 | /** 60 | * {@inheritDoc} 61 | */ 62 | @Override 63 | public DataAccessException translateExceptionIfPossible(RuntimeException e) { 64 | if (e instanceof PersistenceException) { 65 | // Batch exceptions come inside another PersistenceException 66 | // recursion has a risk of infinite loop so better make another if 67 | if (e.getCause() instanceof PersistenceException) { 68 | e = (PersistenceException) e.getCause(); 69 | } 70 | if (e.getCause() instanceof SQLException) { 71 | this.initExceptionTranslator(); 72 | return this.exceptionTranslator.translate(e.getMessage() + "\n", null, (SQLException) e.getCause()); 73 | } else if (e.getCause() instanceof TransactionException) { 74 | throw (TransactionException) e.getCause(); 75 | } 76 | return new MyBatisSystemException(e); 77 | } 78 | return null; 79 | } 80 | 81 | /** 82 | * Initializes the internal translator reference. 83 | */ 84 | private synchronized void initExceptionTranslator() { 85 | if (this.exceptionTranslator == null) { 86 | this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(this.dataSource); 87 | } 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/MyBatisSystemException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring; 17 | 18 | import org.springframework.dao.UncategorizedDataAccessException; 19 | 20 | /** 21 | * MyBatis specific subclass of {@code UncategorizedDataAccessException}, for MyBatis system errors that do 22 | * not match any concrete {@code org.springframework.dao} exceptions. 23 | * 24 | * In MyBatis 3 {@code org.apache.ibatis.exceptions.PersistenceException} is a {@code RuntimeException}, 25 | * but using this wrapper class to bring everything under a single hierarchy will be easier for client code to 26 | * handle. 27 | * 28 | * MyBatis 自定义的系统异常 29 | * 30 | * @author Hunter Presnall 31 | */ 32 | public class MyBatisSystemException extends UncategorizedDataAccessException { 33 | 34 | private static final long serialVersionUID = -5284728621670758939L; 35 | 36 | public MyBatisSystemException(Throwable cause) { 37 | super(null, cause); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/SqlSessionHolder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2018 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring; 17 | 18 | import org.apache.ibatis.session.ExecutorType; 19 | import org.apache.ibatis.session.SqlSession; 20 | import org.springframework.dao.support.PersistenceExceptionTranslator; 21 | import org.springframework.transaction.support.ResourceHolderSupport; 22 | 23 | import static org.springframework.util.Assert.notNull; 24 | 25 | /** 26 | * Used to keep current {@code SqlSession} in {@code TransactionSynchronizationManager}. 27 | * 28 | * The {@code SqlSessionFactory} that created that {@code SqlSession} is used as a key. 29 | * {@code ExecutorType} is also kept to be able to check if the user is trying to change it 30 | * during a TX (that is not allowed) and throw a Exception in that case. 31 | * 32 | * SqlSession 持有器,用于保存当前 SqlSession 对象,保存到 TransactionSynchronizationManager 中 33 | * 34 | * @author Hunter Presnall 35 | * @author Eduardo Macarron 36 | */ 37 | public final class SqlSessionHolder extends ResourceHolderSupport { 38 | 39 | /** 40 | * SqlSession 对象 41 | */ 42 | private final SqlSession sqlSession; 43 | /** 44 | * 执行器类型 45 | */ 46 | private final ExecutorType executorType; 47 | /** 48 | * PersistenceExceptionTranslator 对象 49 | */ 50 | private final PersistenceExceptionTranslator exceptionTranslator; 51 | 52 | /** 53 | * Creates a new holder instance. 54 | * 55 | * @param sqlSession the {@code SqlSession} has to be hold. 56 | * @param executorType the {@code ExecutorType} has to be hold. 57 | * @param exceptionTranslator the {@code PersistenceExceptionTranslator} has to be hold. 58 | */ 59 | public SqlSessionHolder(SqlSession sqlSession, 60 | ExecutorType executorType, 61 | PersistenceExceptionTranslator exceptionTranslator) { 62 | notNull(sqlSession, "SqlSession must not be null"); 63 | notNull(executorType, "ExecutorType must not be null"); 64 | 65 | this.sqlSession = sqlSession; 66 | this.executorType = executorType; 67 | this.exceptionTranslator = exceptionTranslator; 68 | } 69 | 70 | public SqlSession getSqlSession() { 71 | return sqlSession; 72 | } 73 | 74 | public ExecutorType getExecutorType() { 75 | return executorType; 76 | } 77 | 78 | public PersistenceExceptionTranslator getPersistenceExceptionTranslator() { 79 | return exceptionTranslator; 80 | } 81 | 82 | } -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/annotation/MapperScans.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.annotation; 17 | 18 | import org.springframework.context.annotation.Import; 19 | 20 | import java.lang.annotation.*; 21 | 22 | /** 23 | * The Container annotation that aggregates several {@link MapperScan} annotations. 24 | * 25 | * 多 @MapperScan 的注解,功能是相同的 26 | * 27 | *

Can be used natively, declaring several nested {@link MapperScan} annotations. 28 | * Can also be used in conjunction with Java 8's support for repeatable annotations, 29 | * where {@link MapperScan} can simply be declared several times on the same method, 30 | * implicitly generating this container annotation. 31 | * 32 | * @author Kazuki Shimizu 33 | * @since 2.0.0 34 | * @see MapperScan 35 | */ 36 | @Retention(RetentionPolicy.RUNTIME) 37 | @Target(ElementType.TYPE) 38 | @Documented 39 | @Import(MapperScannerRegistrar.RepeatingRegistrar.class) 40 | public @interface MapperScans { 41 | 42 | /** 43 | * @return @MapperScan 数组 44 | */ 45 | MapperScan[] value(); 46 | 47 | } -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/annotation/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains MyBatis annotations 18 | */ 19 | package org.mybatis.spring.annotation; 20 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/batch/builder/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains classes to builder classes for {@link org.springframework.batch.item.ItemReader} and 18 | * {@link org.springframework.batch.item.ItemWriter}. 19 | * 20 | * @since 2.0.0 21 | */ 22 | package org.mybatis.spring.batch.builder; 23 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/batch/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains classes to facilitate the migration in Spring-Batch applications. 18 | */ 19 | package org.mybatis.spring.batch; 20 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/config/NamespaceHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.config; 17 | 18 | import org.springframework.beans.factory.xml.NamespaceHandlerSupport; 19 | 20 | /** 21 | * Namespace handler for the MyBatis namespace. 22 | * 23 | * @author Lishu Luo 24 | * 25 | * @see MapperScannerBeanDefinitionParser 26 | * @since 1.2.0 27 | */ 28 | public class NamespaceHandler extends NamespaceHandlerSupport { 29 | 30 | /** 31 | * {@inheritDoc} 32 | */ 33 | @Override 34 | public void init() { 35 | registerBeanDefinitionParser("scan", new MapperScannerBeanDefinitionParser()); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/config/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains the MyBatis namespace schema and element handlers. 18 | */ 19 | package org.mybatis.spring.config; 20 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/mapper/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains classes for automatically building MyBatis mapper proxy classes at 18 | * application startup. 19 | */ 20 | package org.mybatis.spring.mapper; 21 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains core classes to build the MyBatis integration with Spring3.X. 18 | */ 19 | package org.mybatis.spring; 20 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/support/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains Spring3.X support classes for MyBatis. 18 | */ 19 | package org.mybatis.spring.support; 20 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/transaction/SpringManagedTransactionFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.transaction; 17 | 18 | import org.apache.ibatis.session.TransactionIsolationLevel; 19 | import org.apache.ibatis.transaction.Transaction; 20 | import org.apache.ibatis.transaction.TransactionFactory; 21 | 22 | import javax.sql.DataSource; 23 | import java.sql.Connection; 24 | import java.util.Properties; 25 | 26 | /** 27 | * Creates a {@code SpringManagedTransaction}. 28 | * 29 | * SpringManagedTransaction 的工厂实现类 30 | * 31 | * @author Hunter Presnall 32 | */ 33 | public class SpringManagedTransactionFactory implements TransactionFactory { 34 | 35 | /** 36 | * {@inheritDoc} 37 | */ 38 | @Override 39 | public Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) { 40 | // 创建 SpringManagedTransaction 对象 41 | return new SpringManagedTransaction(dataSource); 42 | } 43 | 44 | /** 45 | * {@inheritDoc} 46 | */ 47 | @Override 48 | public Transaction newTransaction(Connection conn) { 49 | // 抛出异常,因为 Spring 事务,需要一个 DataSource 对象 50 | throw new UnsupportedOperationException("New Spring transactions require a DataSource"); 51 | } 52 | 53 | /** 54 | * {@inheritDoc} 55 | */ 56 | @Override 57 | public void setProperties(Properties props) { 58 | // not needed in this version 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/transaction/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Contains core classes to manage MyBatis transactions in Spring3.X. 18 | */ 19 | package org.mybatis.spring.transaction; 20 | -------------------------------------------------------------------------------- /src/site/es/xdoc/boot.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | Spring Boot 25 | Eduardo Macarron 26 | 27 | 28 | 29 |

30 |

31 | Please see the MyBatis Spring-boot-starter 32 | subproject docs for details. 33 |

34 |
35 | 36 | 37 | -------------------------------------------------------------------------------- /src/site/es/xdoc/using-api.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | Using the MyBatis API 25 | Hunter Presnall 26 | Eduardo Macarron 27 | 28 | 29 | 30 |
31 |

32 | With MyBatis-Spring, you can continue to directly use the MyBatis API. 33 | Simply create an SqlSessionFactory in Spring using 34 | SqlSessionFactoryBean and use the factory in your code. 35 |

36 | 51 | 52 |

53 | Use this option with care because wrong usage may produce runtime errors or 54 | worse, data integrity problems. Be aware of the following caveats with direct API usage: 55 |

56 | 88 |
89 | 90 |
91 | -------------------------------------------------------------------------------- /src/site/ja/xdoc/boot.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | Spring Boot 25 | Eduardo Macarron 26 | 27 | 28 | 29 |
30 |

31 | 詳細は MyBatis Spring-boot-starter のドキュメントを参照してください。 32 |

33 |
34 | 35 |
36 | -------------------------------------------------------------------------------- /src/site/ja/xdoc/using-api.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | MyBatis API の使用 25 | Hunter Presnall 26 | Eduardo Macarron 27 | Iwao AVE! 28 | 29 | 30 | 31 |
32 |

33 | MyBatis-Spring を使っている場合でも、直接 MyBatis API を呼び出すことができます。 34 | Spring の設定で SqlSessionFactoryBean を使って SqlSessionFactory を生成すれば、コード内で使用することができます。 35 |

36 | 51 | 52 |

53 | この方法を使う場合は注意が必要です。 54 | なぜなら、誤った使い方をすると実行時エラーや、最悪の場合データの不整合といった問題を生じる可能性があるからです。 55 | MyBatis API を直接使用する場合、次のような点に注意してください。 56 |

57 | 86 |
87 | 88 |
89 | -------------------------------------------------------------------------------- /src/site/ko/xdoc/boot.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | Spring Boot 25 | Eduardo Macarron 26 | 27 | 28 | 29 |
30 |

31 | 자세한 내용은 MyBatis Spring-boot-starter 32 | 하위 프로젝트 문서를 참조하십시오. 33 |

34 |
35 | 36 |
37 | -------------------------------------------------------------------------------- /src/site/ko/xdoc/using-api.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | 마이바티스 스프링 연동모듈 | 마이바티스 API 사용 25 | Hunter Presnall 26 | Eduardo Macarron 27 | 이동국(한국어 번역) 28 | 29 | 30 | 31 |
32 |

MyBatis-Spring 연동 모듈을 사용해도 계속해서 MyBatis API를 직접 사용할 수 있다. 33 | SqlSessionFactoryBean을 사용해서 스프링에서 SqlSessionFactory를 생성하고 팩토리를 사용하면 된다.

34 | 49 | 50 |

이 방법은 신중히 사용하자. 왜냐하면 잘못사용하면 런타임 에러나 데이터 문제등을 야기할수 있기 때문이다. 51 | API를 직접 사용할때는 다음의 규칙들에 유의해야 한다.

52 | 72 |
73 | 74 |
75 | -------------------------------------------------------------------------------- /src/site/resources/css/site.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /* 17 | * when new flags are needed, take them from 18 | * 19 | * http://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 | -------------------------------------------------------------------------------- /src/site/resources/es/css/site.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /* 17 | * when new flags are needed, take them from 18 | * 19 | * http://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 | -------------------------------------------------------------------------------- /src/site/resources/images/en.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YunaiV/mybatis-spring/47e9afa07b7cb471a586e0f3e50d2ffb6216d56f/src/site/resources/images/en.png -------------------------------------------------------------------------------- /src/site/resources/images/es.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YunaiV/mybatis-spring/47e9afa07b7cb471a586e0f3e50d2ffb6216d56f/src/site/resources/images/es.png -------------------------------------------------------------------------------- /src/site/resources/images/ja.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YunaiV/mybatis-spring/47e9afa07b7cb471a586e0f3e50d2ffb6216d56f/src/site/resources/images/ja.png -------------------------------------------------------------------------------- /src/site/resources/images/ko.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YunaiV/mybatis-spring/47e9afa07b7cb471a586e0f3e50d2ffb6216d56f/src/site/resources/images/ko.png -------------------------------------------------------------------------------- /src/site/resources/images/zh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YunaiV/mybatis-spring/47e9afa07b7cb471a586e0f3e50d2ffb6216d56f/src/site/resources/images/zh.png -------------------------------------------------------------------------------- /src/site/resources/ja/css/site.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /* 17 | * when new flags are needed, take them from 18 | * 19 | * http://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 | -------------------------------------------------------------------------------- /src/site/resources/ko/css/site.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /* 17 | * when new flags are needed, take them from 18 | * 19 | * http://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 | -------------------------------------------------------------------------------- /src/site/resources/zh/css/site.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /* 17 | * when new flags are needed, take them from 18 | * 19 | * http://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 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/site/site_es.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/site/site_ja.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/site/site_ko.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/site/site_zh.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/site/xdoc/boot.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | Spring Boot 25 | Eduardo Macarron 26 | 27 | 28 | 29 |
30 |

31 | Please see the MyBatis Spring-boot-starter 32 | subproject docs for details. 33 |

34 |
35 | 36 |
37 | -------------------------------------------------------------------------------- /src/site/xdoc/using-api.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | Using the MyBatis API 25 | Hunter Presnall 26 | Eduardo Macarron 27 | 28 | 29 | 30 |
31 |

32 | With MyBatis-Spring, you can continue to directly use the MyBatis API. 33 | Simply create an SqlSessionFactory in Spring using 34 | SqlSessionFactoryBean and use the factory in your code. 35 |

36 | 51 | 52 |

53 | Use this option with care because wrong usage may produce runtime errors or 54 | worse, data integrity problems. Be aware of the following caveats with direct API usage: 55 |

56 |
    57 |
  • 58 |

    59 | It will not participate in any Spring transactions. 60 |

    61 |
  • 62 |
  • 63 |

    64 | If the SqlSession is using a DataSource 65 | that is also being used by a Spring transaction manager and there is currently 66 | a transaction in progress, this code will throw an exception. 67 |

    68 |
  • 69 |
  • 70 |

    71 | MyBatis' DefaultSqlSession is not thread safe. If you 72 | inject it in your beans you will get errors. 73 |

    74 |
  • 75 |
  • 76 |

    77 | Mappers created using DefaultSqlSession are not thread safe either. 78 | If you inject them it in your beans you will get errors. 79 |

    80 |
  • 81 |
  • 82 |

    83 | You must make sure that your SqlSessions 84 | are always closed in a finally block. 85 |

    86 |
  • 87 |
88 |
89 | 90 |
91 | -------------------------------------------------------------------------------- /src/site/zh/xdoc/boot.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | 搭配 Spring Boot 25 | Eduardo Macarron 26 | 27 | 28 | 29 |
30 |

31 | 请查看 MyBatis Spring-boot-starter 32 | 子项目获取更多信息。 33 |

34 |
35 | 36 |
37 | -------------------------------------------------------------------------------- /src/site/zh/xdoc/using-api.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | MyBatis-Spring | 使用 MyBatis API 25 | Hunter Presnall 26 | Eduardo Macarron 27 | Nan Lei 28 | 29 | 30 | 31 |
32 |

33 | 使用 MyBatis-Spring,你可以继续直接使用 MyBatis 的 API。只需简单地使用 SqlSessionFactoryBean 在 Spring 中创建一个 SqlSessionFactory,然后按你的方式在代码中使用工厂即可。 34 |

35 | 50 | 51 |

52 | 小心使用此选项,错误地使用会产生运行时错误,更糟糕地,会产生数据一致性的问题。直接使用 API 时,注意以下弊端: 53 |

54 |
    55 |
  • 56 |

    57 | 它不会参与到 Spring 的事务管理之中。 58 |

    59 |
  • 60 |
  • 61 |

    62 | 如果 SqlSession 使用与 Spring 事务管理器使用的相同 DataSource,并且有进行中的事务,代码会抛出异常。 63 |

    64 |
  • 65 |
  • 66 |

    67 | MyBatis 的 DefaultSqlSession 是线程不安全的。如果在 bean 中注入了它,会发生错误。 68 |

    69 |
  • 70 |
  • 71 |

    72 | 使用 DefaultSqlSession 创建的映射器也不是线程安全的。如果你将它们注入到 bean 中,会发生错误。 73 |

    74 |
  • 75 |
  • 76 |

    77 | 你必须确保总是在 finally 块中来关闭 SqlSession。 78 |

    79 |
  • 80 |
81 |
82 | 83 |
84 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/ExecutorInterceptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring; 17 | 18 | import java.util.Properties; 19 | 20 | import org.apache.ibatis.executor.Executor; 21 | import org.apache.ibatis.plugin.Interceptor; 22 | import org.apache.ibatis.plugin.Intercepts; 23 | import org.apache.ibatis.plugin.Invocation; 24 | import org.apache.ibatis.plugin.Plugin; 25 | import org.apache.ibatis.plugin.Signature; 26 | 27 | /** 28 | * Keeps track of Executor commits, rollbacks and close status. 29 | * 30 | * The Executor is not accessible from DefaultSqlSession, so it is much easier to use an Interceptor 31 | * rather than subclass a new SqlSessionFactory, etc. Also, there is the potential to change the 32 | * default MyBatis behavior, which could change the test results. 33 | */ 34 | @Intercepts({ 35 | @Signature(type = Executor.class, method = "commit", args = { boolean.class }), 36 | @Signature(type = Executor.class, method = "rollback", args = { boolean.class }), 37 | @Signature(type = Executor.class, method = "close", args = { boolean.class }) 38 | }) 39 | final class ExecutorInterceptor implements Interceptor { 40 | 41 | private int commitCount; 42 | 43 | private int rollbackCount; 44 | 45 | private boolean closed; 46 | 47 | @Override 48 | public Object intercept(Invocation invocation) throws Throwable { 49 | if ("commit".equals(invocation.getMethod().getName())) { 50 | ++this.commitCount; 51 | } else if ("rollback".equals(invocation.getMethod().getName())) { 52 | ++this.rollbackCount; 53 | } else if ("close".equals(invocation.getMethod().getName())) { 54 | this.closed = true; 55 | } 56 | 57 | return invocation.proceed(); 58 | } 59 | 60 | @Override 61 | public Object plugin(Object target) { 62 | return Plugin.wrap(target, this); 63 | } 64 | 65 | @Override 66 | public void setProperties(Properties properties) { 67 | // do nothing 68 | } 69 | 70 | void reset() { 71 | this.commitCount = 0; 72 | this.rollbackCount = 0; 73 | this.closed = false; 74 | } 75 | 76 | int getCommitCount() { 77 | return this.commitCount; 78 | } 79 | 80 | int getRollbackCount() { 81 | return this.rollbackCount; 82 | } 83 | 84 | boolean isExecutorClosed() { 85 | return this.closed; 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/PooledMockDataSource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring; 17 | 18 | import java.sql.Connection; 19 | import java.sql.SQLException; 20 | import java.util.LinkedList; 21 | 22 | import com.mockrunner.mock.jdbc.MockDataSource; 23 | 24 | final class PooledMockDataSource extends MockDataSource { 25 | 26 | private int connectionCount = 0; 27 | 28 | private LinkedList connections = new LinkedList<>(); 29 | 30 | @Override 31 | public Connection getConnection() throws SQLException { 32 | if (connections.isEmpty()) { 33 | throw new SQLException("Sorry, I ran out of connections"); 34 | } 35 | ++this.connectionCount; 36 | return this.connections.removeLast(); 37 | } 38 | 39 | int getConnectionCount() { 40 | return this.connectionCount; 41 | } 42 | 43 | void reset() { 44 | this.connectionCount = 0; 45 | this.connections.clear(); 46 | } 47 | 48 | @Override 49 | public void setupConnection(Connection connection) { 50 | throw new UnsupportedOperationException("used addConnection() instead"); 51 | } 52 | 53 | public void addConnection(Connection c) { 54 | this.connections.add(c); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/TestMapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring; 17 | 18 | public interface TestMapper { 19 | 20 | int findTest(); 21 | 22 | void insertTest(String test); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/TestMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | 27 | 28 | 1 29 | 30 | 31 | 34 | 35 | 36 | INSERT #{test} INTO test 37 | 38 | 39 | 40 | INSERT fail 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/annotation/mapper/ds1/Ds1Mapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.annotation.mapper.ds1; 17 | 18 | public interface Ds1Mapper { 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/annotation/mapper/ds2/Ds2Mapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.annotation.mapper.ds2; 17 | 18 | public interface Ds2Mapper { 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/asyncsynchronization/AsyncAfterCompletionHelper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.asyncsynchronization; 17 | 18 | import java.lang.reflect.InvocationHandler; 19 | import java.lang.reflect.InvocationTargetException; 20 | import java.lang.reflect.Method; 21 | import java.lang.reflect.Proxy; 22 | import java.util.HashSet; 23 | import java.util.Set; 24 | 25 | import org.springframework.transaction.support.TransactionSynchronization; 26 | 27 | /** 28 | * For use as ByteMan helper 29 | * 30 | * @author Alex Rykov 31 | */ 32 | @SuppressWarnings("unused") 33 | public class AsyncAfterCompletionHelper { 34 | /** 35 | * 36 | * Invocation handler that performs afterCompletion on a separate thread 37 | * See Github issue #18 38 | * 39 | * @author Alex Rykov 40 | * 41 | */ 42 | static class AsyncAfterCompletionInvocationHandler implements InvocationHandler { 43 | 44 | private Object target; 45 | 46 | AsyncAfterCompletionInvocationHandler(Object target) { 47 | this.target = target; 48 | } 49 | 50 | @Override 51 | public Object invoke(final Object proxy, final Method method, 52 | final Object[] args) throws Throwable { 53 | if ("afterCompletion".equals(method.getName())) { 54 | final Set retValSet = new HashSet<>(); 55 | final Set exceptionSet = new HashSet<>(); 56 | Thread thread = new Thread(() -> { 57 | try { 58 | retValSet.add(method.invoke(target, args)); 59 | } catch (InvocationTargetException ite) { 60 | exceptionSet.add(ite.getCause()); 61 | 62 | } catch (IllegalArgumentException | IllegalAccessException e) { 63 | exceptionSet.add(e); 64 | 65 | } 66 | }); 67 | thread.start(); 68 | thread.join(); 69 | if (exceptionSet.isEmpty()) { 70 | return retValSet.iterator().next(); 71 | } else { 72 | throw exceptionSet.iterator().next(); 73 | } 74 | } else { 75 | return method.invoke(target, args); 76 | } 77 | } 78 | 79 | } 80 | 81 | /** 82 | * Creates proxy that performs afterCompletion call on a separate thread 83 | * 84 | * @param synchronization 85 | * @return 86 | */ 87 | public TransactionSynchronization createSynchronizationWithAsyncAfterComplete( 88 | TransactionSynchronization synchronization) { 89 | if (Proxy.isProxyClass(synchronization.getClass()) 90 | && Proxy.getInvocationHandler(synchronization) instanceof AsyncAfterCompletionInvocationHandler) { 91 | // avoiding double wrapping just in case 92 | return synchronization; 93 | } 94 | Class[] interfaces = { TransactionSynchronization.class }; 95 | return (TransactionSynchronization) Proxy.newProxyInstance(synchronization 96 | .getClass().getClassLoader(), interfaces, 97 | new AsyncAfterCompletionInvocationHandler(synchronization)); 98 | 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/asyncsynchronization/SqlSessionTemplateAsyncAfterCompletionTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.asyncsynchronization; 17 | 18 | import org.jboss.byteman.contrib.bmunit.BMRule; 19 | import org.jboss.byteman.contrib.bmunit.BMUnitRunner; 20 | import org.junit.Ignore; 21 | import org.junit.runner.RunWith; 22 | import org.mybatis.spring.SqlSessionTemplateTest; 23 | 24 | 25 | /** 26 | * 27 | * The same test as original but afterCompletion is being called on a separate thread 28 | * 29 | * @author Alex Rykov 30 | * 31 | */ 32 | @Ignore // FIXME: Enable after migrate BMUnitRunner to BMUnitExtension 33 | @RunWith(BMUnitRunner.class) 34 | @BMRule(name = "proxy synchronizations", targetClass = "TransactionSynchronizationManager", targetMethod = "registerSynchronization(TransactionSynchronization)", helper = "org.mybatis.spring.asyncsynchronization.AsyncAfterCompletionHelper", action = "$1=createSynchronizationWithAsyncAfterComplete($1)") 35 | public class SqlSessionTemplateAsyncAfterCompletionTest extends SqlSessionTemplateTest { 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/batch/MyBatisCursorItemReaderTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.batch; 17 | 18 | import java.util.Arrays; 19 | import java.util.Collections; 20 | import java.util.List; 21 | 22 | import org.apache.ibatis.cursor.Cursor; 23 | import org.apache.ibatis.session.ExecutorType; 24 | import org.apache.ibatis.session.SqlSession; 25 | import org.apache.ibatis.session.SqlSessionFactory; 26 | import org.assertj.core.api.Assertions; 27 | import org.junit.jupiter.api.BeforeEach; 28 | import org.junit.jupiter.api.Test; 29 | import org.mockito.Mock; 30 | import org.mockito.Mockito; 31 | import org.mockito.MockitoAnnotations; 32 | import org.springframework.batch.item.ExecutionContext; 33 | import org.springframework.batch.item.ItemStreamException; 34 | 35 | import static org.junit.Assert.fail; 36 | 37 | /** 38 | * Tests for {@link MyBatisCursorItemReader}. 39 | */ 40 | class MyBatisCursorItemReaderTest { 41 | 42 | @Mock 43 | private SqlSessionFactory sqlSessionFactory; 44 | 45 | @Mock 46 | private SqlSession sqlSession; 47 | 48 | @Mock 49 | private Cursor cursor; 50 | 51 | @BeforeEach 52 | void setUp() { 53 | MockitoAnnotations.initMocks(this); 54 | } 55 | 56 | @Test 57 | void testCloseOnFailing() throws Exception { 58 | 59 | Mockito.when(this.sqlSessionFactory.openSession(ExecutorType.SIMPLE)) 60 | .thenReturn(this.sqlSession); 61 | Mockito.when(this.cursor.iterator()).thenReturn(getFoos().iterator()); 62 | Mockito.when(this.sqlSession.selectCursor("selectFoo", Collections.singletonMap("id", 1))) 63 | .thenThrow(new RuntimeException("error.")); 64 | 65 | MyBatisCursorItemReader itemReader = new MyBatisCursorItemReader<>(); 66 | itemReader.setSqlSessionFactory(this.sqlSessionFactory); 67 | itemReader.setQueryId("selectFoo"); 68 | itemReader.setParameterValues(Collections.singletonMap("id", 1)); 69 | itemReader.afterPropertiesSet(); 70 | 71 | ExecutionContext executionContext = new ExecutionContext(); 72 | try { 73 | itemReader.open(executionContext); 74 | fail(); 75 | } catch (ItemStreamException e) { 76 | Assertions.assertThat(e).hasMessage("Failed to initialize the reader") 77 | .hasCause(new RuntimeException("error.")); 78 | } finally { 79 | itemReader.close(); 80 | Mockito.verify(this.sqlSession).close(); 81 | } 82 | 83 | } 84 | 85 | @Test 86 | void testCloseBeforeOpen() { 87 | MyBatisCursorItemReader itemReader = new MyBatisCursorItemReader<>(); 88 | itemReader.close(); 89 | } 90 | 91 | private List getFoos() { 92 | return Arrays.asList(new Foo("foo1"), new Foo("foo2"), new Foo("foo3")); 93 | } 94 | 95 | private static class Foo { 96 | private final String name; 97 | 98 | public Foo(String name) { 99 | this.name = name; 100 | } 101 | 102 | public String getName() { 103 | return this.name; 104 | } 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/batch/dao/EmployeeMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | 27 | 28 | 31 | 32 | 36 | 37 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | update employees set salary=#{salary} where id=#{id} 52 | 53 | 54 | 57 | 58 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/batch/db/database-schema.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2016 the original author or authors. 3 | -- 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); 5 | -- you may not use this file except in compliance with the License. 6 | -- You may obtain a copy of the License at 7 | -- 8 | -- http://www.apache.org/licenses/LICENSE-2.0 9 | -- 10 | -- Unless required by applicable law or agreed to in writing, software 11 | -- distributed under the License is distributed on an "AS IS" BASIS, 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | -- See the License for the specific language governing permissions and 14 | -- limitations under the License. 15 | -- 16 | 17 | create table employees ( 18 | id integer not null, 19 | name varchar(80) not null, 20 | salary integer not null, 21 | skill varchar(80) not null 22 | ); 23 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/batch/db/database-test-data.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2016 the original author or authors. 3 | -- 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); 5 | -- you may not use this file except in compliance with the License. 6 | -- You may obtain a copy of the License at 7 | -- 8 | -- http://www.apache.org/licenses/LICENSE-2.0 9 | -- 10 | -- Unless required by applicable law or agreed to in writing, software 11 | -- distributed under the License is distributed on an "AS IS" BASIS, 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | -- See the License for the specific language governing permissions and 14 | -- limitations under the License. 15 | -- 16 | 17 | insert into employees VALUES ( 1, 'Pocoyo' , 1000, 's1'); 18 | insert into employees VALUES ( 1, 'Pocoyo' , 1000, 's2'); 19 | insert into employees VALUES ( 2, 'Pato' , 2000, 's1'); 20 | insert into employees VALUES ( 2, 'Pato' , 2000, 's2'); 21 | insert into employees VALUES ( 3, 'Eli' , 3000, 's1'); 22 | insert into employees VALUES ( 3, 'Eli' , 3000, 's2'); 23 | insert into employees VALUES ( 3, 'Eli' , 3000, 's3'); 24 | insert into employees VALUES ( 4, 'Valentina' , 4000, 's1'); 25 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/batch/domain/Employee.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.batch.domain; 17 | 18 | import java.util.List; 19 | 20 | public class Employee { 21 | 22 | private int id; 23 | private String name; 24 | private int salary; 25 | private List skills; 26 | 27 | public int getId() { 28 | return id; 29 | } 30 | 31 | public void setId(int id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public int getSalary() { 44 | return salary; 45 | } 46 | 47 | public void setSalary(int salary) { 48 | this.salary = salary; 49 | } 50 | 51 | public List getSkills() { 52 | return skills; 53 | } 54 | 55 | public void setSkills(List skills) { 56 | this.skills = skills; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "Employee{" + 62 | "id=" + id + 63 | ", name='" + name + '\'' + 64 | ", salary=" + salary + 65 | ", skills=" + skills + 66 | '}'; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/config/annotation.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 31 | 32 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/config/base-package.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 31 | 32 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/config/factory-ref.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 31 | 32 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/config/marker-and-annotation.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 31 | 32 | 35 | 38 | 39 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/config/marker-interface.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 31 | 32 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/config/name-generator.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 26 | 28 | 29 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/config/template-ref.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 31 | 32 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mapper/AnnotatedMapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.mapper; 17 | 18 | import org.springframework.stereotype.Component; 19 | 20 | // annotated interface for MapperScannerPostProcessor tests 21 | // ensures annotated classes are loaded 22 | @Component 23 | public interface AnnotatedMapper { 24 | void method(); 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mapper/AnnotatedMapperZeroMethods.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.mapper; 17 | 18 | import org.springframework.stereotype.Component; 19 | 20 | // annotated interface for MapperScannerPostProcessor tests 21 | // ensures annotated class with no methods is not loaded 22 | @Component 23 | public interface AnnotatedMapperZeroMethods { 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mapper/MapperClass.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.mapper; 17 | 18 | // child class for MapperScannerPostProcessor tests 19 | // ensures classes are not loaded as MapperFactoryBeans 20 | public class MapperClass { 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mapper/MapperImplementation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.mapper; 17 | 18 | // implementation of MapperInterface to test conflicting types 19 | final class MapperImplementation implements MapperInterface { 20 | @Override 21 | public void method() { 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mapper/MapperInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.mapper; 17 | 18 | // marker interface for MapperScannerPostProcessor tests 19 | // when used as a marker interface, 20 | // this class should be ignored even though it has methods 21 | public interface MapperInterface { 22 | void method(); 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mapper/MapperSubinterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.mapper; 17 | 18 | // interface for MapperScannerPostProcessor tests 19 | // ensures marker interface usage 20 | // has no methods except parent methods 21 | public interface MapperSubinterface extends MapperInterface { 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mapper/child/MapperChildInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.mapper.child; 17 | 18 | import org.mybatis.spring.mapper.MapperInterface; 19 | import org.springframework.stereotype.Component; 20 | 21 | // interface for MapperScannerPostProcessor tests 22 | // tests subpackage search 23 | @Component 24 | public interface MapperChildInterface extends MapperInterface { 25 | void childMethod(); 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/AbstractSampleTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.junit.jupiter.api.Test; 19 | import org.mybatis.spring.sample.domain.User; 20 | import org.mybatis.spring.sample.service.FooService; 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.test.annotation.DirtiesContext; 23 | 24 | import static org.assertj.core.api.Assertions.assertThat; 25 | 26 | /** 27 | * 示例单元测试基类 28 | * 29 | * 1. {@link SampleEnableTest} 30 | * 基于 @MapperScan 注解,扫描指定包 31 | * 32 | * 2. {@link SampleMapperTest} 33 | * 基于 {@link org.mybatis.spring.mapper.MapperFactoryBean} 类,直接声明指定的 Mapper 接口 34 | * 35 | * 3. {@link SampleNamespaceTest} 36 | * 基于 标签,扫描指定包 37 | * 38 | * 4. {@link SampleScannerTest} 39 | * 基于 {@link org.mybatis.spring.mapper.MapperScannerConfigurer} 类,扫描指定包 40 | * 41 | * 5. {@link SampleBatchTest} 42 | * 在 SampleMapperTest 的基础上,使用 BatchExecutor 执行器 43 | */ 44 | @DirtiesContext 45 | public abstract class AbstractSampleTest { 46 | 47 | @Autowired 48 | protected FooService fooService; 49 | 50 | @Test 51 | final void testFooService() { 52 | User user = this.fooService.doSomeBusinessStuff("u1"); 53 | assertThat(user).isNotNull(); 54 | assertThat(user.getName()).isEqualTo("Pocoyo"); 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleBatchTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 19 | 20 | /** 21 | * Example of MyBatis-Spring batch integration usage. 22 | */ 23 | @SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-batch.xml" }) 24 | public class SampleBatchTest extends AbstractSampleTest { 25 | // Note this does not actually test batch functionality since FooService 26 | // only calls one DAO method. This class and associated Spring context 27 | // simply show that no implementation changes are needed to enable 28 | // different MyBatis configurations. 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleEnableTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.mybatis.spring.annotation.MapperScan; 19 | import org.springframework.context.annotation.Configuration; 20 | import org.springframework.context.annotation.ImportResource; 21 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 22 | 23 | /** 24 | * Test to ensure that the {@link MapperScan} 25 | * annotation works as expected. 26 | * 27 | * @since 1.2.0 28 | */ 29 | @SpringJUnitConfig 30 | public class SampleEnableTest extends AbstractSampleTest { 31 | 32 | @Configuration 33 | @ImportResource("classpath:org/mybatis/spring/sample/config/applicationContext-infrastructure.xml") 34 | @MapperScan("org.mybatis.spring.sample.mapper") 35 | static class AppConfig { 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleJavaConfigTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * MyBatis @Configuration style sample 18 | */ 19 | package org.mybatis.spring.sample; 20 | 21 | import static org.assertj.core.api.Assertions.assertThat; 22 | 23 | import org.junit.jupiter.api.Test; 24 | import org.mybatis.spring.sample.config.SampleConfig; 25 | import org.mybatis.spring.sample.domain.User; 26 | import org.mybatis.spring.sample.service.FooService; 27 | import org.springframework.beans.factory.annotation.Autowired; 28 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 29 | 30 | @SpringJUnitConfig(classes = SampleConfig.class) 31 | public class SampleJavaConfigTest { 32 | 33 | @Autowired 34 | private FooService fooService; 35 | 36 | @Autowired 37 | private FooService fooServiceWithMapperFactoryBean; 38 | 39 | @Test 40 | void test() { 41 | User user = fooService.doSomeBusinessStuff("u1"); 42 | assertThat(user.getName()).isEqualTo("Pocoyo"); 43 | } 44 | 45 | @Test 46 | void testWithMapperFactoryBean() { 47 | User user = fooServiceWithMapperFactoryBean.doSomeBusinessStuff("u1"); 48 | assertThat(user.getName()).isEqualTo("Pocoyo"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleJobJavaConfigTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.mybatis.spring.sample.config.SampleJobConfig; 19 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 20 | 21 | @SpringJUnitConfig({SampleJobConfig.class, AbstractSampleJobTest.LocalContext.class}) 22 | class SampleJobJavaConfigTest extends AbstractSampleJobTest { 23 | @Override 24 | protected String getExpectedOperationBy() { 25 | return "batch_java_config_user"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleJobXmlConfigTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.springframework.context.annotation.Configuration; 19 | import org.springframework.context.annotation.ImportResource; 20 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 21 | 22 | @SpringJUnitConfig({AbstractSampleJobTest.LocalContext.class, 23 | SampleJobXmlConfigTest.LocalContext.class}) 24 | class SampleJobXmlConfigTest extends AbstractSampleJobTest { 25 | 26 | @Override 27 | protected String getExpectedOperationBy() { 28 | return "batch_xml_config_user"; 29 | } 30 | 31 | @ImportResource("classpath:org/mybatis/spring/sample/config/applicationContext-job.xml") 32 | @Configuration 33 | static class LocalContext { 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleMapperTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 19 | 20 | /** 21 | * Example of MyBatis-Spring integration with a DAO created by 22 | * MapperFactoryBean. 23 | */ 24 | @SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-mapper.xml" }) 25 | public class SampleMapperTest extends AbstractSampleTest { 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleNamespaceTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 19 | 20 | /** 21 | * Example of MyBatis-Spring integration with a DAO configured via 22 | * MapperScannerConfigurer. 23 | */ 24 | @SpringJUnitConfig(locations = {"classpath:org/mybatis/spring/sample/config/applicationContext-namespace.xml"}) 25 | public class SampleNamespaceTest extends AbstractSampleTest { 26 | } -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleScannerTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 19 | 20 | /** 21 | * Example of MyBatis-Spring integration with a DAO configured via 22 | * MapperScannerConfigurer. 23 | */ 24 | @SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-scanner.xml" }) 25 | public class SampleScannerTest extends AbstractSampleTest { 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/SampleSqlSessionTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample; 17 | 18 | import org.junit.jupiter.api.Test; 19 | import org.mybatis.spring.sample.domain.User; 20 | import org.mybatis.spring.sample.service.BarService; 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.test.annotation.DirtiesContext; 23 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 24 | 25 | import static org.assertj.core.api.Assertions.assertThat; 26 | 27 | /** 28 | * Example of basic MyBatis-Spring integration usage with a manual DAO 29 | * implementation that subclasses SqlSessionDaoSupport. 30 | */ 31 | @DirtiesContext 32 | @SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-sqlsession.xml" }) 33 | public class SampleSqlSessionTest { 34 | 35 | @Autowired 36 | private BarService barService; 37 | 38 | @Test 39 | final void testFooService() { 40 | User user = this.barService.doSomeBusinessStuff("u1"); 41 | assertThat(user).isNotNull(); 42 | assertThat(user.getName()).isEqualTo("Pocoyo"); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/batch/UserToPersonItemProcessor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.batch; 17 | 18 | import org.mybatis.spring.sample.domain.Person; 19 | 20 | import org.mybatis.spring.sample.domain.User; 21 | import org.springframework.batch.item.ItemProcessor; 22 | 23 | public class UserToPersonItemProcessor implements ItemProcessor { 24 | 25 | @Override 26 | public Person process(final User user) throws Exception { 27 | final String[] names = user.getName().split(" "); 28 | if (names.length == 1) { 29 | return new Person(names[0], null); 30 | } else { 31 | return new Person(names[0], names[1]); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/SampleConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.config; 17 | 18 | import org.apache.ibatis.session.SqlSessionFactory; 19 | import org.mybatis.spring.SqlSessionFactoryBean; 20 | import org.mybatis.spring.SqlSessionTemplate; 21 | import org.mybatis.spring.mapper.MapperFactoryBean; 22 | import org.mybatis.spring.sample.mapper.UserMapper; 23 | import org.mybatis.spring.sample.service.FooService; 24 | import org.springframework.context.annotation.Bean; 25 | import org.springframework.context.annotation.Configuration; 26 | import org.springframework.core.io.ClassPathResource; 27 | import org.springframework.core.io.Resource; 28 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 29 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 30 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 31 | import org.springframework.transaction.PlatformTransactionManager; 32 | 33 | import javax.sql.DataSource; 34 | 35 | @Configuration 36 | public class SampleConfig { 37 | @Bean 38 | public DataSource dataSource() { 39 | return new EmbeddedDatabaseBuilder() 40 | .setType(EmbeddedDatabaseType.HSQL) 41 | .addScript("org/mybatis/spring/sample/db/database-schema.sql") 42 | .addScript("org/mybatis/spring/sample/db/database-test-data.sql") 43 | .build(); 44 | } 45 | 46 | @Bean 47 | public PlatformTransactionManager transactionalManager() { 48 | return new DataSourceTransactionManager(dataSource()); 49 | } 50 | 51 | @Bean 52 | public SqlSessionFactory sqlSessionFactory() throws Exception { 53 | SqlSessionFactoryBean ss = new SqlSessionFactoryBean(); 54 | ss.setDataSource(dataSource()); 55 | ss.setMapperLocations(new Resource[] { new ClassPathResource("org/mybatis/spring/sample/mapper/UserMapper.xml") }); 56 | return ss.getObject(); 57 | } 58 | 59 | @Bean 60 | public UserMapper userMapper() throws Exception { 61 | // when using javaconfig a template requires less lines than a MapperFactoryBean 62 | SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory()); 63 | return sqlSessionTemplate.getMapper(UserMapper.class); 64 | } 65 | 66 | @Bean 67 | public UserMapper userMapperWithFactory() throws Exception { 68 | MapperFactoryBean mapperFactoryBean = new MapperFactoryBean<>(); 69 | mapperFactoryBean.setMapperInterface(UserMapper.class); 70 | mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory()); 71 | mapperFactoryBean.afterPropertiesSet(); 72 | return mapperFactoryBean.getObject(); 73 | } 74 | 75 | @Bean 76 | public FooService fooService() throws Exception { 77 | return new FooService(userMapper()); 78 | } 79 | 80 | @Bean 81 | public FooService fooServiceWithMapperFactoryBean() throws Exception { 82 | return new FooService(userMapperWithFactory()); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/applicationContext-batch.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 23 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/applicationContext-infrastructure.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 23 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/applicationContext-job.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/applicationContext-mapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 25 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/applicationContext-namespace.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 24 | 30 | 31 | 32 | 33 | 34 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/applicationContext-scanner.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 24 | 28 | 29 | 30 | 31 | 32 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/config/applicationContext-sqlsession.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 23 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.dao; 17 | 18 | import org.mybatis.spring.sample.domain.User; 19 | 20 | public interface UserDao { 21 | User getUser(String userId); 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/dao/UserDaoImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.dao; 17 | 18 | import org.mybatis.spring.sample.domain.User; 19 | import org.mybatis.spring.support.SqlSessionDaoSupport; 20 | 21 | /** 22 | * This DAO extends SqlSessionDaoSupport and uses a Spring managed SqlSession 23 | * instead of the MyBatis one. SqlSessions are handled by Spring so you don't 24 | * need to open/close/commit/rollback. 25 | * MyBatis exceptions are translated to Spring Data Exceptions. 26 | */ 27 | public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { 28 | 29 | @Override 30 | public User getUser(String userId) { 31 | return getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/db/database-schema.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2018 the original author or authors. 3 | -- 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); 5 | -- you may not use this file except in compliance with the License. 6 | -- You may obtain a copy of the License at 7 | -- 8 | -- http://www.apache.org/licenses/LICENSE-2.0 9 | -- 10 | -- Unless required by applicable law or agreed to in writing, software 11 | -- distributed under the License is distributed on an "AS IS" BASIS, 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | -- See the License for the specific language governing permissions and 14 | -- limitations under the License. 15 | -- 16 | 17 | drop table users if exists; 18 | create table users ( 19 | id varchar(80) not null, 20 | name varchar(80) not null, 21 | constraint pk_user primary key (id) 22 | ); 23 | 24 | drop table persons if exists; 25 | create table persons ( 26 | person_id integer identity not null primary key, 27 | first_name varchar(20), 28 | last_name varchar(20), 29 | operation_by varchar(64), 30 | operation_at timestamp 31 | ); 32 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/db/database-test-data.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2017 the original author or authors. 3 | -- 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); 5 | -- you may not use this file except in compliance with the License. 6 | -- You may obtain a copy of the License at 7 | -- 8 | -- http://www.apache.org/licenses/LICENSE-2.0 9 | -- 10 | -- Unless required by applicable law or agreed to in writing, software 11 | -- distributed under the License is distributed on an "AS IS" BASIS, 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | -- See the License for the specific language governing permissions and 14 | -- limitations under the License. 15 | -- 16 | 17 | insert into users VALUES ( 'u1', 'Pocoyo' ); 18 | insert into users VALUES ( 'u2', 'Pato' ); 19 | insert into users VALUES ( 'u3', 'Eli' ); 20 | insert into users VALUES ( 'u4', 'Valentina' ); 21 | insert into users VALUES ( 'u5', 'Taro Yamada' ); 22 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/domain/Person.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.domain; 17 | 18 | public class Person { 19 | 20 | private String lastName; 21 | private String firstName; 22 | 23 | public Person() {} 24 | 25 | public Person(String firstName, String lastName) { 26 | this.firstName = firstName; 27 | this.lastName = lastName; 28 | } 29 | 30 | public void setFirstName(String firstName) { 31 | this.firstName = firstName; 32 | } 33 | 34 | public String getFirstName() { 35 | return firstName; 36 | } 37 | 38 | public String getLastName() { 39 | return lastName; 40 | } 41 | 42 | public void setLastName(String lastName) { 43 | this.lastName = lastName; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "firstName: " + firstName + ", lastName: " + lastName; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/domain/User.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.domain; 17 | 18 | /** 19 | * A simple bean that holds User info. 20 | */ 21 | public class User { 22 | 23 | private String id; 24 | 25 | private String name; 26 | 27 | public String getId() { 28 | return id; 29 | } 30 | 31 | public void setId(String id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | StringBuilder buf = new StringBuilder(30); 46 | buf.append("{"); 47 | buf.append(id); 48 | buf.append(", "); 49 | buf.append(name); 50 | buf.append("}"); 51 | return buf.toString(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/mapper/PersonMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | 25 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.mapper; 17 | 18 | import org.mybatis.spring.sample.domain.User; 19 | 20 | /** 21 | * A org.mybatis.spring sample mapper. This interface will be used by MapperFactoryBean to create a 22 | * proxy implementation at Spring application startup. 23 | */ 24 | public interface UserMapper { 25 | 26 | User getUser(String userId); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | 25 | 28 | 29 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/mapper/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Package with DAOs/mappers. 18 | */ 19 | package org.mybatis.spring.sample.mapper; 20 | 21 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Sample for four usage scenarios. 18 | *

    19 | *
  • Automatic configuration with MapperScannerConfigurer
  • 20 | *
  • Automatic DAO creation with MapperFactoryBean
  • 21 | *
  • Extending SqlSessionDaoSupport to code your mapper implementation
  • 22 | *
  • Configuring a DAO for batch processing
  • 23 | *
24 | */ 25 | package org.mybatis.spring.sample; 26 | 27 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/service/BarService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.service; 17 | 18 | import org.mybatis.spring.sample.dao.UserDao; 19 | import org.mybatis.spring.sample.domain.User; 20 | import org.springframework.transaction.annotation.Transactional; 21 | 22 | /** 23 | * BarService simply receives a userId and uses a dao to get a record from the database. 24 | */ 25 | @Transactional 26 | public class BarService { 27 | 28 | private final UserDao userDao; 29 | 30 | public BarService(UserDao userDao) { 31 | this.userDao = userDao; 32 | } 33 | 34 | public User doSomeBusinessStuff(String userId) { 35 | return this.userDao.getUser(userId); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/sample/service/FooService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.sample.service; 17 | 18 | import org.mybatis.spring.sample.domain.User; 19 | import org.mybatis.spring.sample.mapper.UserMapper; 20 | import org.springframework.transaction.annotation.Transactional; 21 | 22 | /** 23 | * FooService simply receives a userId and uses a mapper to get a record from the database. 24 | */ 25 | @Transactional 26 | public class FooService { 27 | 28 | private final UserMapper userMapper; 29 | 30 | public FooService(UserMapper userMapper) { 31 | this.userMapper = userMapper; 32 | } 33 | 34 | public User doSomeBusinessStuff(String userId) { 35 | return this.userMapper.getUser(userId); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/AutowireTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.autowire; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import org.junit.jupiter.api.Test; 21 | import org.springframework.context.support.ClassPathXmlApplicationContext; 22 | 23 | public class AutowireTest { 24 | private ClassPathXmlApplicationContext context; 25 | 26 | @Test 27 | void shouldReturnMapper() { 28 | context = new ClassPathXmlApplicationContext("classpath:org/mybatis/spring/submitted/autowire/spring.xml"); 29 | 30 | FooMapper fooMapper = (FooMapper) context.getBean("fooMapper"); 31 | assertThat(fooMapper).isNotNull(); 32 | fooMapper.executeFoo(); 33 | 34 | BarMapper barMapper = (BarMapper) context.getBean("barMapper"); 35 | assertThat(barMapper).isNotNull(); 36 | barMapper.executeBar(); 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/BarMapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.autowire; 17 | 18 | public interface BarMapper extends IBar { 19 | String executeBar(); 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/BarMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 24 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/FooMapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.autowire; 17 | 18 | public interface FooMapper extends IFoo { 19 | String executeFoo(); 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/FooMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 24 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/IBar.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.autowire; 17 | 18 | public interface IBar { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/IFoo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.autowire; 17 | 18 | public interface IFoo { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/database-schema-bar.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2016 the original author or authors. 3 | -- 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); 5 | -- you may not use this file except in compliance with the License. 6 | -- You may obtain a copy of the License at 7 | -- 8 | -- http://www.apache.org/licenses/LICENSE-2.0 9 | -- 10 | -- Unless required by applicable law or agreed to in writing, software 11 | -- distributed under the License is distributed on an "AS IS" BASIS, 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | -- See the License for the specific language governing permissions and 14 | -- limitations under the License. 15 | -- 16 | 17 | create table bar ( 18 | id varchar(80) not null, 19 | name varchar(80) not null, 20 | constraint pk_user primary key (id) 21 | ); 22 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/database-schema-foo.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2016 the original author or authors. 3 | -- 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); 5 | -- you may not use this file except in compliance with the License. 6 | -- You may obtain a copy of the License at 7 | -- 8 | -- http://www.apache.org/licenses/LICENSE-2.0 9 | -- 10 | -- Unless required by applicable law or agreed to in writing, software 11 | -- distributed under the License is distributed on an "AS IS" BASIS, 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | -- See the License for the specific language governing permissions and 14 | -- limitations under the License. 15 | -- 16 | 17 | create table foo ( 18 | id varchar(80) not null, 19 | name varchar(80) not null, 20 | constraint pk_user primary key (id) 21 | ); 22 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/autowire/spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/webapp_placeholder/Mapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.webapp_placeholder; 17 | 18 | public interface Mapper { 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/webapp_placeholder/WebappPlaceholderTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.webapp_placeholder; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import org.junit.jupiter.api.Test; 21 | import org.junit.jupiter.api.extension.ExtendWith; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.test.context.junit.jupiter.SpringExtension; 24 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 25 | import org.springframework.test.context.web.WebAppConfiguration; 26 | 27 | @ExtendWith(SpringExtension.class) 28 | @WebAppConfiguration 29 | @SpringJUnitConfig(locations = "file:src/test/java/org/mybatis/spring/submitted/webapp_placeholder/spring.xml") 30 | public class WebappPlaceholderTest { 31 | 32 | @Autowired 33 | private Mapper mapper; 34 | 35 | @Test 36 | void testName() throws Exception { 37 | assertThat(mapper).isNotNull(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/webapp_placeholder/conf.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2016 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | basePkg=org.mybatis.spring.submitted.webapp_placeholder 18 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/webapp_placeholder/spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/xa/User.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.xa; 17 | 18 | public class User { 19 | 20 | private Integer id; 21 | private String name; 22 | 23 | public User() { 24 | } 25 | 26 | public User(Integer id, String name) { 27 | this.id = id; 28 | this.name = name; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public Integer getId() { 40 | return this.id; 41 | } 42 | 43 | public void setId(Integer id) { 44 | this.id = id; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/xa/UserMapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.xa; 17 | 18 | import org.apache.ibatis.annotations.Insert; 19 | import org.apache.ibatis.annotations.Select; 20 | 21 | public interface UserMapper { 22 | 23 | @Insert("INSERT INTO USERS VALUES(#{id}, #{name})") 24 | void save(User user); 25 | 26 | @Select("SELECT * FROM USERS WHERE ID=#{id}") 27 | User select(int id); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/xa/UserService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.xa; 17 | 18 | public interface UserService { 19 | 20 | void saveWithNoFailure(User user); 21 | 22 | void saveWithFailure(User user); 23 | 24 | boolean checkUserExists(int id); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/xa/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.xa; 17 | 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.stereotype.Service; 20 | import org.springframework.transaction.annotation.Transactional; 21 | 22 | @Service 23 | public class UserServiceImpl implements UserService { 24 | 25 | @Autowired 26 | private UserMapper userMapperMaster; 27 | @Autowired 28 | private UserMapper userMapperSlave; 29 | 30 | @Override 31 | @Transactional 32 | public void saveWithNoFailure(User user) { 33 | userMapperMaster.save(user); 34 | userMapperSlave.save(user); 35 | } 36 | 37 | @Override 38 | @Transactional 39 | public void saveWithFailure(User user) { 40 | userMapperMaster.save(user); 41 | userMapperSlave.save(user); 42 | throw new RuntimeException("failed!"); 43 | } 44 | 45 | @Override 46 | public boolean checkUserExists(int id) { 47 | if (userMapperMaster.select(id) != null) return true; 48 | if (userMapperSlave.select(id) != null) return true; 49 | return false; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/xa/UserServiceTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.submitted.xa; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import javax.transaction.UserTransaction; 21 | 22 | import org.junit.jupiter.api.Test; 23 | import org.junit.jupiter.api.extension.ExtendWith; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.test.context.junit.jupiter.SpringExtension; 26 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 27 | 28 | @ExtendWith(SpringExtension.class) 29 | @SpringJUnitConfig(locations = "classpath:org/mybatis/spring/submitted/xa/applicationContext.xml") 30 | public class UserServiceTest { 31 | 32 | @Autowired UserTransaction userTransaction; 33 | 34 | @Autowired 35 | private UserService userService; 36 | 37 | @Test 38 | void testCommit() { 39 | User user = new User(1, "Pocoyo"); 40 | userService.saveWithNoFailure(user); 41 | assertThat(userService.checkUserExists(user.getId())).isTrue(); 42 | } 43 | 44 | @Test 45 | void testRollback() { 46 | User user = new User(2, "Pocoyo"); 47 | try { 48 | userService.saveWithFailure(user); 49 | } catch (RuntimeException ignore) { 50 | // ignored 51 | } 52 | assertThat(userService.checkUserExists(user.getId())).isFalse(); 53 | } 54 | 55 | @Test 56 | void testCommitWithExistingTx() throws Exception { 57 | userTransaction.begin(); 58 | User user = new User(3, "Pocoyo"); 59 | userService.saveWithNoFailure(user); 60 | userTransaction.commit(); 61 | assertThat(userService.checkUserExists(user.getId())).isTrue(); 62 | } 63 | 64 | // TODO when the outer JTA tx is rolledback, 65 | // SqlSession should be rolledback but it is committed 66 | // because Spring calls beforeCommmit from its TX interceptor 67 | // then, the JTA TX may be rolledback. 68 | @Test 69 | void testRollbackWithExistingTx() throws Exception { 70 | userTransaction.begin(); 71 | User user = new User(5, "Pocoyo"); 72 | userService.saveWithNoFailure(user); 73 | userTransaction.rollback(); 74 | assertThat(userService.checkUserExists(user.getId())).isFalse(); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/submitted/xa/database-schema.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2016 the original author or authors. 3 | -- 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); 5 | -- you may not use this file except in compliance with the License. 6 | -- You may obtain a copy of the License at 7 | -- 8 | -- http://www.apache.org/licenses/LICENSE-2.0 9 | -- 10 | -- Unless required by applicable law or agreed to in writing, software 11 | -- distributed under the License is distributed on an "AS IS" BASIS, 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | -- See the License for the specific language governing permissions and 14 | -- limitations under the License. 15 | -- 16 | 17 | CREATE TABLE USERS ( 18 | id integer not null, 19 | name varchar(80) not null 20 | ); 21 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/type/DummyMapperFactoryBean.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.type; 17 | 18 | import org.apache.ibatis.session.SqlSessionFactory; 19 | import org.mybatis.logging.Logger; 20 | import org.mybatis.logging.LoggerFactory; 21 | import org.mybatis.spring.mapper.MapperFactoryBean; 22 | 23 | import java.lang.reflect.Proxy; 24 | import java.util.concurrent.atomic.AtomicInteger; 25 | 26 | public class DummyMapperFactoryBean extends MapperFactoryBean { 27 | 28 | public DummyMapperFactoryBean() { 29 | super(); 30 | } 31 | 32 | public DummyMapperFactoryBean(Class mapperInterface) { 33 | super(mapperInterface); 34 | } 35 | 36 | private static final Logger LOGGER = LoggerFactory.getLogger(DummyMapperFactoryBean.class); 37 | 38 | private static final AtomicInteger mapperInstanceCount = new AtomicInteger(0); 39 | 40 | @Override 41 | protected void checkDaoConfig() { 42 | super.checkDaoConfig(); 43 | // make something more 44 | if (isAddToConfig()) { 45 | LOGGER.debug(() -> "register mapper for interface : " + getMapperInterface()); 46 | } 47 | } 48 | 49 | @Override 50 | public T getObject() throws Exception { 51 | MapperFactoryBean mapperFactoryBean = new MapperFactoryBean<>(); 52 | mapperFactoryBean.setMapperInterface(getMapperInterface()); 53 | mapperFactoryBean.setAddToConfig(isAddToConfig()); 54 | mapperFactoryBean.setSqlSessionFactory(getCustomSessionFactoryForClass()); 55 | T object = mapperFactoryBean.getObject(); 56 | mapperInstanceCount.incrementAndGet(); 57 | return object; 58 | } 59 | 60 | private SqlSessionFactory getCustomSessionFactoryForClass() { 61 | // can for example read a custom annotation to set a custom sqlSessionFactory 62 | 63 | // just a dummy implementation example 64 | return (SqlSessionFactory) Proxy.newProxyInstance( 65 | SqlSessionFactory.class.getClassLoader(), 66 | new Class[]{SqlSessionFactory.class}, 67 | (proxy, method, args) -> { 68 | if ("getConfiguration".equals(method.getName())) { 69 | return getSqlSession().getConfiguration(); 70 | } 71 | // dummy 72 | return null; 73 | } 74 | ); 75 | } 76 | 77 | public static int getMapperCount(){ 78 | return mapperInstanceCount.get(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/type/DummyTypeAlias.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.type; 17 | 18 | import org.apache.ibatis.type.Alias; 19 | 20 | @Alias("testAlias") 21 | public class DummyTypeAlias { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/type/DummyTypeAlias2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.type; 17 | 18 | import org.apache.ibatis.type.Alias; 19 | 20 | @Alias("testAlias2") 21 | public class DummyTypeAlias2 extends SuperType { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/type/DummyTypeHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.type; 17 | 18 | import java.math.BigInteger; 19 | import java.sql.CallableStatement; 20 | import java.sql.PreparedStatement; 21 | import java.sql.ResultSet; 22 | import java.sql.SQLException; 23 | 24 | import org.apache.ibatis.type.JdbcType; 25 | import org.apache.ibatis.type.MappedTypes; 26 | import org.apache.ibatis.type.TypeHandler; 27 | 28 | @MappedTypes(BigInteger.class) 29 | public class DummyTypeHandler implements TypeHandler { 30 | 31 | @Override 32 | public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { 33 | } 34 | 35 | @Override 36 | public Object getResult(ResultSet rs, String columnName) throws SQLException { 37 | return null; 38 | } 39 | 40 | @Override 41 | public Object getResult(CallableStatement cs, int columnIndex) throws SQLException { 42 | return null; 43 | } 44 | 45 | @Override 46 | public Object getResult(ResultSet rs, int columnIndex) throws SQLException { 47 | return null; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/type/DummyTypeHandler2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.type; 17 | 18 | import java.math.BigDecimal; 19 | import java.sql.CallableStatement; 20 | import java.sql.PreparedStatement; 21 | import java.sql.ResultSet; 22 | import java.sql.SQLException; 23 | 24 | import org.apache.ibatis.type.JdbcType; 25 | import org.apache.ibatis.type.MappedTypes; 26 | import org.apache.ibatis.type.TypeHandler; 27 | 28 | @MappedTypes(BigDecimal.class) 29 | public class DummyTypeHandler2 implements TypeHandler { 30 | 31 | @Override 32 | public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { 33 | } 34 | 35 | @Override 36 | public Object getResult(ResultSet rs, String columnName) throws SQLException { 37 | return null; 38 | } 39 | 40 | @Override 41 | public Object getResult(CallableStatement cs, int columnIndex) throws SQLException { 42 | return null; 43 | } 44 | 45 | @Override 46 | public Object getResult(ResultSet rs, int columnIndex) throws SQLException { 47 | return null; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/type/SuperType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2010-2015 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.spring.type; 17 | 18 | public class SuperType { 19 | } 20 | -------------------------------------------------------------------------------- /src/test/resources/log4j2-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /travis/after_success.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright 2010-2017 the original author or authors. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | 19 | # Get Commit Message 20 | commit_message=$(git log --format=%B -n 1) 21 | echo "Current commit detected: ${commit_message}" 22 | 23 | # We build for several JDKs on Travis. 24 | # Some actions, like analyzing the code (Coveralls) and uploading 25 | # artifacts on a Maven repository, should only be made for one version. 26 | 27 | # If the version is 1.8, then perform the following actions. 28 | # 1. Upload artifacts to Sonatype. 29 | # 2. Use -q option to only display Maven errors and warnings. 30 | # 3. Use --settings to force the usage of our "settings.xml" file. 31 | # 4. Notify Coveralls. 32 | # 5. Deploy site 33 | # 6. Notify Sonar 34 | 35 | if [ $TRAVIS_REPO_SLUG == "mybatis/spring" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ] && [[ "$commit_message" != *"[maven-release-plugin]"* ]]; then 36 | 37 | if [ $TRAVIS_JDK_VERSION == "oraclejdk8" ]; then 38 | 39 | # Deploy to sonatype 40 | ./mvnw clean deploy -q --settings ./travis/settings.xml 41 | echo -e "Successfully deployed SNAPSHOT artifacts to Sonatype under Travis job ${TRAVIS_JOB_NUMBER}" 42 | 43 | # Deploy to coveralls 44 | ./mvnw clean test jacoco:report coveralls:report -q --settings ./travis/settings.xml 45 | echo -e "Successfully ran coveralls under Travis job ${TRAVIS_JOB_NUMBER}" 46 | 47 | # Deploy to site 48 | # Cannot currently run site this way 49 | # ./mvnw site site:deploy -q 50 | # echo -e "Successfully deploy site under Travis job ${TRAVIS_JOB_NUMBER}" 51 | 52 | # Notify Sonar 53 | ./mvnw clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.host.url=https://sonarqube.com -Dsonar.login=ccf0be39fd0ca5ea5aa712247c79da7233cd3caa 54 | echo -e "Successfully ran Sonar integration under Travis job ${TRAVIS_JOB_NUMBER}" 55 | else 56 | echo "Java Version does not support additonal activity for travis CI" 57 | fi 58 | else 59 | echo "Travis Pull Request: $TRAVIS_PULL_REQUEST" 60 | echo "Travis Branch: $TRAVIS_BRANCH" 61 | echo "Travis build skipped" 62 | fi 63 | -------------------------------------------------------------------------------- /travis/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | ossrh 24 | ${env.CI_DEPLOY_USERNAME} 25 | ${env.CI_DEPLOY_PASSWORD} 26 | 27 | 28 | gh-pages 29 | git 30 | ${env.CI_SITE_PASSWORD} 31 | 32 | 33 | 34 | --------------------------------------------------------------------------------