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/resources/org/mybatis/spring/sample/config/applicationContext-scanner.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
24 |
28 |
29 |
30 |
31 |
32 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/java/org/mybatis/spring/annotation/MapperScans.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2010-2025 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.spring.annotation;
17 |
18 | import java.lang.annotation.Documented;
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | import org.springframework.context.annotation.Import;
25 |
26 | /**
27 | * The Container annotation that aggregates several {@link MapperScan} annotations.
28 | *
29 | * Can be used natively, declaring several nested {@link MapperScan} annotations. Can also be used in conjunction with
30 | * Java 8's support for repeatable annotations, where {@link MapperScan} can simply be declared several times on the
31 | * same method, implicitly generating this container annotation.
32 | *
33 | * @author Kazuki Shimizu
34 | *
35 | * @since 2.0.0
36 | *
37 | * @see MapperScan
38 | */
39 | @Retention(RetentionPolicy.RUNTIME)
40 | @Target(ElementType.TYPE)
41 | @Documented
42 | @Import(MapperScannerRegistrar.RepeatingRegistrar.class)
43 | public @interface MapperScans {
44 |
45 | /**
46 | * Value.
47 | *
48 | * @return the mapper scan[]
49 | */
50 | MapperScan[] value();
51 | }
52 |
--------------------------------------------------------------------------------
/.mvn/settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
21 |
22 |
23 |
24 |
25 | central
26 | ${env.CI_DEPLOY_USERNAME}
27 | ${env.CI_DEPLOY_PASSWORD}
28 |
29 |
30 |
31 |
32 | gh-pages-scm
33 |
34 | branch
35 | gh-pages
36 |
37 |
38 |
39 |
40 |
41 | github
42 | ${env.GITHUB_TOKEN}
43 |
44 |
45 |
46 |
47 | nvd
48 | ${env.NVD_API_KEY}
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/config/marker-and-annotation.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
25 |
31 |
32 |
35 |
38 |
39 |
--------------------------------------------------------------------------------
/src/test/resources/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/resources/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/core/jdk/type/AtomicNumberTypeHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2010-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.core.jdk.type;
17 |
18 | import java.sql.CallableStatement;
19 | import java.sql.PreparedStatement;
20 | import java.sql.ResultSet;
21 | import java.sql.SQLException;
22 | import java.util.concurrent.atomic.AtomicInteger;
23 | import java.util.concurrent.atomic.AtomicLong;
24 |
25 | import org.apache.ibatis.type.JdbcType;
26 | import org.apache.ibatis.type.MappedTypes;
27 | import org.apache.ibatis.type.TypeHandler;
28 |
29 | @MappedTypes({ AtomicInteger.class, AtomicLong.class })
30 | public class AtomicNumberTypeHandler implements TypeHandler {
31 |
32 | public AtomicNumberTypeHandler(Class> type) {
33 | }
34 |
35 | @Override
36 | public void setParameter(PreparedStatement ps, int i, Number parameter, JdbcType jdbcType) throws SQLException {
37 | }
38 |
39 | @Override
40 | public Number getResult(ResultSet rs, String columnName) throws SQLException {
41 | return null;
42 | }
43 |
44 | @Override
45 | public Number getResult(CallableStatement cs, int columnIndex) throws SQLException {
46 | return null;
47 | }
48 |
49 | @Override
50 | public Number getResult(ResultSet rs, int columnIndex) throws SQLException {
51 | return null;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/config/default-scope.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
25 |
26 |
27 |
28 |
29 |
30 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/sample/config/applicationContext-sqlsession.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
23 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | org.mybatis.spring.sample.mapper.UserMapper
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/config/process-property-placeholders-false.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/test/resources/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 |
45 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/config/process-property-placeholders-true.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/test/java/org/mybatis/spring/submitted/webapp_placeholder/WebappPlaceholderTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2010-2024 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.spring.submitted.webapp_placeholder;
17 |
18 | import static org.assertj.core.api.Assertions.assertThat;
19 |
20 | import org.apache.ibatis.session.SqlSessionFactory;
21 | import org.junit.jupiter.api.Assertions;
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.context.ApplicationContext;
26 | import org.springframework.test.context.junit.jupiter.SpringExtension;
27 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
28 | import org.springframework.test.context.web.WebAppConfiguration;
29 |
30 | @ExtendWith(SpringExtension.class)
31 | @WebAppConfiguration
32 | @SpringJUnitConfig(locations = "classpath:org/mybatis/spring/submitted/webapp_placeholder/spring.xml")
33 | class WebappPlaceholderTest {
34 |
35 | @Autowired
36 | private SqlSessionFactory sqlSessionFactory;
37 |
38 | @Autowired
39 | private ApplicationContext applicationContext;
40 |
41 | @Test
42 | void testName() {
43 | Assertions.assertEquals(0, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
44 | var mapper = applicationContext.getBean(Mapper.class);
45 | assertThat(mapper).isNotNull();
46 | Assertions.assertEquals(1, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/site/zh_CN/markdown/sample.md:
--------------------------------------------------------------------------------
1 |
2 | # 示例代码
3 |
4 | 提示
5 | 查看 [JPetstore 6 demo](https://github.com/mybatis/jpetstore-6) 了解如何在完整的 Web 应用服务器上使用 Spring。
6 |
7 | 您可以在 MyBatis-Spring 的 [代码仓库](https://github.com/mybatis/spring/tree/master/src/test/java/org/mybatis/spring/sample) 中查看示例代码:
8 |
9 | 所有示例都能在 JUnit 5 下运行。
10 |
11 | 示例代码演示了事务服务从数据访问层获取域对象的典型设计。
12 |
13 | `FooService.java` 作为服务:
14 |
15 | ```java
16 | @Transactional
17 | public class FooService {
18 |
19 | private final UserMapper userMapper;
20 |
21 | public FooService(UserMapper userMapper) {
22 | this.userMapper = userMapper;
23 | }
24 |
25 | public User doSomeBusinessStuff(String userId) {
26 | return this.userMapper.getUser(userId);
27 | }
28 |
29 | }
30 | ```
31 |
32 | 它是一个事务 bean,所以当调用它的任何方法时,事务被启动,在方法结束且没有抛出任何未经检查的异常的时候事务将会被提交。注意,事务的行为可以通过 `@Transactional` 的属性进行配置。这不是必需的;你可以使用 Spring 提供的任何其他方式来划分你的事务范围。
33 |
34 | 此服务调用使用 MyBatis 构建的数据访问层.。该层只包含一个接口,`UserMapper.java`,这将被 MyBatis 构建的动态代理使用,在运行时通过 Spring 注入到服务之中。
35 |
36 | ```java
37 | public interface UserMapper {
38 |
39 | User getUser(String userId);
40 |
41 | }
42 | ```
43 |
44 | 注意,为了简单起见,我们使用了接口 `UserMapper.java`。在使用 DAO 的场景中,一个 DAO 类应该分为一个接口和一个实现类。回到这个例子里,准确来说,这个接口应该叫 `UserDao.java` 。
45 |
46 | 我们将看到不同的方法来发现映射器接口,将其注册到 Spring 并将其注入到服务 bean 中:
47 |
48 | ## 测试场景
49 |
50 | | 样例测试 | 描述 |
51 | | --- | --- |
52 | | `SampleMapperTest.java` | 演示基于 `MapperFactoryBean` 的基本配置,这将动态构建 `UserMapper` 的一个实现。 |
53 | | `SampleScannerTest.java` | 演示如何使用 `MapperScannerConfigurer` 来自动发现项目中所有的映射器。 |
54 | | `SampleSqlSessionTest.java` | 演示如何基于 Spring 管理的 `SqlSession` 手动编写 DAO,并在 `UserDaoImpl.java` 中提供你自己的实现。 |
55 | | `SampleEnableTest.java` | 演示如何使用 Spring 的 `@Configuration` 和 `@MapperScann` 注解来自动发现 mappers. |
56 | | `SampleNamespaceTest.java` | 演示如何使用自定义 MyBatis XML 命名空间. |
57 | | `SampleJavaConfigTest.java` | 演示如何基于 Spring 的 `@Configuration` 来手工创建 MyBatis 的 bean。 |
58 | | `SampleJobJavaConfigTest.java` | 演示如何在 Java 配置中使用 Spring Batch 中的 `ItemReader` 和 `ItemWriter`。 |
59 | | `SampleJobXmlConfigTest.java` | 演示如何在 XML 配置中使用 Spring Batch 中的 `ItemReader` 和 `ItemWriter`。 |
60 |
61 | 查看不同的 `applicationContext.xml` 文件以了解 MyBatis-Spring 在实践中是如何运用的。
62 |
63 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/filter/xml/appContextAspectJFilter.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/filter/xml/appContextCustFilter.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/test/resources/org/mybatis/spring/filter/xml/appContextInvalidFilter1.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------