@@ -21,9 +21,9 @@ In Java, the equivalent code would be:
2121@Configuration
2222public 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}
@@ -57,6 +57,18 @@ For example:
5757</bean >
5858```
5959
60+ In Java, the equivalent code would be:
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+
6072This will load all the MyBatis mapper XML files in the ` sample.config.mappers ` package and its sub-packages from the classpath.
6173
6274One property that may be required in an environment with container managed transactions is ` transactionFactoryClass ` . Please see the relevant section in the Transactions chapter.
@@ -83,6 +95,29 @@ In case you are using the multi-db feature you will need to set the `databaseIdP
8395</bean >
8496```
8597
98+ In Java, the equivalent code would be:
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+ ```
86121<span class =" label important " >NOTE</span >
87122Since 1.3.0, ` configuration ` property has been added. It can be specified a ` Configuration ` instance directly without MyBatis XML configuration file.
88123
@@ -98,3 +133,48 @@ For example:
98133 </property >
99134</bean >
100135```
136+
137+ In Java, the equivalent code would be:
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 Example
154+
155+ Here is a complete example of a configuration class that combines the properties described above.
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+ 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