Skip to content

Commit 4515a81

Browse files
committed
Add full Java Config example for SqlSessionFactoryBean (#502) in i18n doc
See gh-1158
1 parent 5cb34a7 commit 4515a81

File tree

5 files changed

+334
-10
lines changed

5 files changed

+334
-10
lines changed

src/site/es/markdown/factorybean.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ En este caso, Spring creará un bean `SqlSessionFactory` durante el arranque de
2121
@Configuration
2222
public class MyBatisConfig {
2323
@Bean
24-
public SqlSessionFactory sqlSessionFactory() {
24+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
2525
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
26-
factoryBean.setDataSource(dataSource());
26+
factoryBean.setDataSource(dataSource);
2727
return factoryBean.getObject();
2828
}
2929
}
@@ -58,6 +58,18 @@ El valor puede contener un patron tipo Ant para cargar todos los ficheros de un
5858
</bean>
5959
```
6060

61+
In Java, the equivalent code would be:
62+
63+
```java
64+
@Bean
65+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
66+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
67+
factoryBean.setDataSource(dataSource);
68+
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sample/config/mappers/**/*.xml"));
69+
return factoryBean.getObject();
70+
}
71+
```
72+
6173
Esto cargaría todos los ficheros de mapeo XML en el paquete sample.config.mappers y sus subpaquetes.
6274

6375
Otra propiedad que puede ser necesaria en un entorno con transacciones gestionadas por contenedor es la `transactionFactoryClass`. Lee la sección de transacciones para obtener más detalles.
@@ -84,6 +96,30 @@ En caso de usar la característica multi-db necesitarás informar la propiedad `
8496
</bean>
8597
```
8698

99+
In Java, the equivalent code would be:
100+
101+
```java
102+
@Bean
103+
public VendorDatabaseIdProvider databaseIdProvider() {
104+
VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
105+
Properties properties = new Properties();
106+
properties.setProperty("SQL Server", "sqlserver");
107+
properties.setProperty("DB2", "db2");
108+
properties.setProperty("Oracle", "oracle");
109+
properties.setProperty("MySQL", "mysql");
110+
databaseIdProvider.setProperties(properties);
111+
return databaseIdProvider;
112+
}
113+
114+
@Bean
115+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, DatabaseIdProvider databaseIdProvider) throws Exception {
116+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
117+
factoryBean.setDataSource(dataSource);
118+
factoryBean.setDatabaseIdProvider(databaseIdProvider);
119+
return factoryBean.getObject();
120+
}
121+
```
122+
87123
<span class="label important">NOTE</span>
88124
Since 1.3.0, `configuration` property has been added. It can be specified a `Configuration` instance directly without MyBatis XML configuration file.
89125
For example:
@@ -98,3 +134,48 @@ For example:
98134
</property>
99135
</bean>
100136
```
137+
138+
In Java, the equivalent code would be:
139+
140+
```java
141+
@Bean
142+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
143+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
144+
factoryBean.setDataSource(dataSource);
145+
146+
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
147+
configuration.setMapUnderscoreToCamelCase(true);
148+
factoryBean.setConfiguration(configuration);
149+
150+
return factoryBean.getObject();
151+
}
152+
```
153+
154+
## Java Configuration Example
155+
156+
Here is a complete example of a configuration class that combines the properties described above.
157+
158+
```java
159+
@Configuration
160+
public class MyBatisConfig {
161+
162+
@Bean
163+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
164+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
165+
factoryBean.setDataSource(dataSource);
166+
167+
// Setting mapper locations
168+
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sample/config/mappers/**/*.xml"));
169+
170+
// Setting configuration property
171+
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
172+
configuration.setMapUnderscoreToCamelCase(true);
173+
factoryBean.setConfiguration(configuration);
174+
175+
return factoryBean.getObject();
176+
}
177+
}
178+
```
179+
180+
<span class="label important">NOTE</span>
181+
This configuration class must be located within a package scanned by the Spring container (e.g., within the main application package). The class name itself (e.g., `MyBatisConfig`) is arbitrary; only the `@Configuration` annotation is required.

src/site/ja/markdown/factorybean.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Spring の XML 設定ファイルに次の Bean を定義することで Factory
2222
@Configuration
2323
public class MyBatisConfig {
2424
@Bean
25-
public SqlSessionFactory sqlSessionFactory() {
25+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
2626
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
27-
factoryBean.setDataSource(dataSource());
27+
factoryBean.setDataSource(dataSource);
2828
return factoryBean.getObject();
2929
}
3030
}
@@ -57,6 +57,18 @@ Ant スタイルのパターン文字列を使って特定のディレクトリ
5757
</bean>
5858
```
5959

60+
Javaでは、同等のコードは次のようになります。
61+
62+
```java
63+
@Bean
64+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
65+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
66+
factoryBean.setDataSource(dataSource);
67+
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sample/config/mappers/**/*.xml"));
68+
return factoryBean.getObject();
69+
}
70+
```
71+
6072
このように指定すると、クラスパス内の `sample.config.mappers` パッケージと、そのサブパッケージに含まれる全ての MyBatis Mapper XML ファイルがロードされます。
6173

6274
Container-Managed トランザクションを利用する環境では、`transactionFactoryClass` プロパティが必須となります。「トランザクション」章の該当する節を参照してください。
@@ -83,6 +95,30 @@ Container-Managed トランザクションを利用する環境では、`transac
8395
</bean>
8496
```
8597

98+
Javaでは、同等のコードは次のようになります。
99+
100+
```java
101+
@Bean
102+
public VendorDatabaseIdProvider databaseIdProvider() {
103+
VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
104+
Properties properties = new Properties();
105+
properties.setProperty("SQL Server", "sqlserver");
106+
properties.setProperty("DB2", "db2");
107+
properties.setProperty("Oracle", "oracle");
108+
properties.setProperty("MySQL", "mysql");
109+
databaseIdProvider.setProperties(properties);
110+
return databaseIdProvider;
111+
}
112+
113+
@Bean
114+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, DatabaseIdProvider databaseIdProvider) throws Exception {
115+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
116+
factoryBean.setDataSource(dataSource);
117+
factoryBean.setDatabaseIdProvider(databaseIdProvider);
118+
return factoryBean.getObject();
119+
}
120+
```
121+
86122
<span class="label important">NOTE</span>
87123
1.3.0より、`configuration` プロパティが追加されました。このプロパティには、MyBatisのXML設定ファイルを使わずに`Configuration`インスタンスを直接指定することができます。
88124
次の例を見てください。
@@ -97,3 +133,48 @@ Container-Managed トランザクションを利用する環境では、`transac
97133
</property>
98134
</bean>
99135
```
136+
137+
Javaでは、同等のコードは次のようになります。
138+
139+
```java
140+
@Bean
141+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
142+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
143+
factoryBean.setDataSource(dataSource);
144+
145+
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
146+
configuration.setMapUnderscoreToCamelCase(true);
147+
factoryBean.setConfiguration(configuration);
148+
149+
return factoryBean.getObject();
150+
}
151+
```
152+
153+
## Java Configurationサンプル
154+
155+
上記で説明したプロパティを組み合わせた設定クラスの完全な例は以下の通りです。
156+
157+
```java
158+
@Configuration
159+
public class MyBatisConfig {
160+
161+
@Bean
162+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
163+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
164+
factoryBean.setDataSource(dataSource);
165+
166+
// Setting mapper locations
167+
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sample/config/mappers/**/*.xml"));
168+
169+
// Setting configuration property
170+
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
171+
configuration.setMapUnderscoreToCamelCase(true);
172+
factoryBean.setConfiguration(configuration);
173+
174+
return factoryBean.getObject();
175+
}
176+
}
177+
```
178+
179+
<span class="label important">NOTE</span>
180+
この設定クラスは、Springコンテナによってスキャンされるパッケージ内に配置する必要があります(例:メインアプリケーションパッケージ内)。クラス名自体(例: `MyBatisConfig` )は任意で、必要なのは `@Configuration` アノテーションだけです。

src/site/ko/markdown/factorybean.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
@Configuration
2323
public class MyBatisConfig {
2424
@Bean
25-
public SqlSessionFactory sqlSessionFactory() {
25+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
2626
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
27-
factoryBean.setDataSource(dataSource());
27+
factoryBean.setDataSource(dataSource);
2828
return factoryBean.getObject();
2929
}
3030
}
@@ -56,6 +56,18 @@ public class MyBatisConfig {
5656
</bean>
5757
```
5858

59+
In Java, the equivalent code would be:
60+
61+
```java
62+
@Bean
63+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
64+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
65+
factoryBean.setDataSource(dataSource);
66+
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sample/config/mappers/**/*.xml"));
67+
return factoryBean.getObject();
68+
}
69+
```
70+
5971
이 설정은 `sample.config.mappers` 패키지 아래와 그 하위 패키지를 모두 검색해서 마이바티스 매퍼 XML파일을 모두 로드할 것이다.
6072

6173
컨테이너 관리 트랜잭션을 사용하는 환경에서 필요한 하나의 프로퍼티는 `transactionFactoryClass` 이다. 이에 관련해서는 트랜잭션을 다루는 장에서 볼 수 있다.
@@ -82,6 +94,30 @@ public class MyBatisConfig {
8294
</bean>
8395
````
8496

97+
In Java, the equivalent code would be:
98+
99+
```java
100+
@Bean
101+
public VendorDatabaseIdProvider databaseIdProvider() {
102+
VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
103+
Properties properties = new Properties();
104+
properties.setProperty("SQL Server", "sqlserver");
105+
properties.setProperty("DB2", "db2");
106+
properties.setProperty("Oracle", "oracle");
107+
properties.setProperty("MySQL", "mysql");
108+
databaseIdProvider.setProperties(properties);
109+
return databaseIdProvider;
110+
}
111+
112+
@Bean
113+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, DatabaseIdProvider databaseIdProvider) throws Exception {
114+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
115+
factoryBean.setDataSource(dataSource);
116+
factoryBean.setDatabaseIdProvider(databaseIdProvider);
117+
return factoryBean.getObject();
118+
}
119+
```
120+
85121
<span class="label important">NOTE</span>
86122
1.3.0 버전부터 `configuration` 속성이 추가되었다. 다음과 같이 MyBatis XML 설정 파일 없이 `Configuration` 인스턴스를 직접 지정할 수 있습니다.
87123

@@ -95,3 +131,48 @@ public class MyBatisConfig {
95131
</property>
96132
</bean>
97133
```
134+
135+
In Java, the equivalent code would be:
136+
137+
```java
138+
@Bean
139+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
140+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
141+
factoryBean.setDataSource(dataSource);
142+
143+
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
144+
configuration.setMapUnderscoreToCamelCase(true);
145+
factoryBean.setConfiguration(configuration);
146+
147+
return factoryBean.getObject();
148+
}
149+
```
150+
151+
## Java Configuration Example
152+
153+
Here is a complete example of a configuration class that combines the properties described above.
154+
155+
```java
156+
@Configuration
157+
public class MyBatisConfig {
158+
159+
@Bean
160+
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
161+
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
162+
factoryBean.setDataSource(dataSource);
163+
164+
// Setting mapper locations
165+
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sample/config/mappers/**/*.xml"));
166+
167+
// Setting configuration property
168+
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
169+
configuration.setMapUnderscoreToCamelCase(true);
170+
factoryBean.setConfiguration(configuration);
171+
172+
return factoryBean.getObject();
173+
}
174+
}
175+
```
176+
177+
<span class="label important">NOTE</span>
178+
This configuration class must be located within a package scanned by the Spring container (e.g., within the main application package). The class name itself (e.g., `MyBatisConfig`) is arbitrary; only the `@Configuration` annotation is required.

src/site/markdown/factorybean.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public class MyBatisConfig {
162162
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
163163
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
164164
factoryBean.setDataSource(dataSource);
165-
165+
166166
// Setting mapper locations
167167
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sample/config/mappers/**/*.xml"));
168168

@@ -177,4 +177,4 @@ public class MyBatisConfig {
177177
```
178178

179179
<span class="label important">NOTE</span>
180-
This configuration class must be located within a package scanned by the Spring container (e.g., within the main application package). The class name itself (e.g., `MyBatisConfig`) is arbitrary; only the `@Configuration` annotation is required.
180+
This configuration class must be located within a package scanned by the Spring container (e.g., within the main application package). The class name itself (e.g., `MyBatisConfig`) is arbitrary; only the `@Configuration` annotation is required.

0 commit comments

Comments
 (0)