Skip to content

Commit 9a90d7e

Browse files
committed
DATAJDBC-161 - Polishing.
Removed second constructor and made tests use the new constructor. Changed parameter type to the interface SqlSession. Added some JavaDoc.
1 parent 66556f9 commit 9a90d7e

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
*/
1616
package org.springframework.data.jdbc.mybatis;
1717

18-
import java.util.Collections;
19-
import java.util.Map;
20-
2118
import org.apache.ibatis.session.SqlSession;
22-
import org.apache.ibatis.session.SqlSessionFactory;
2319
import org.mybatis.spring.SqlSessionTemplate;
2420
import org.springframework.data.jdbc.core.DataAccessStrategy;
2521
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
2622
import org.springframework.data.mapping.PropertyPath;
2723

24+
import java.util.Collections;
25+
import java.util.Map;
26+
2827
/**
2928
* {@link DataAccessStrategy} implementation based on MyBatis. Each method gets mapped to a statement. The name of the
3029
* statement gets constructed as follows: The namespace is based on the class of the entity plus the suffix "Mapper".
@@ -42,14 +41,16 @@ public class MyBatisDataAccessStrategy implements DataAccessStrategy {
4241

4342
private final SqlSession sqlSession;
4443

45-
public MyBatisDataAccessStrategy(SqlSessionFactory sqlSessionFactory) {
46-
47-
this(new SqlSessionTemplate(sqlSessionFactory));
48-
}
49-
50-
public MyBatisDataAccessStrategy(SqlSessionTemplate sqlSessionTemplate) {
51-
52-
this.sqlSession = sqlSessionTemplate;
44+
/**
45+
* Constructs a {@link DataAccessStrategy} based on MyBatis.
46+
* <p>
47+
* Use a {@link SqlSessionTemplate} for {@link SqlSession} or a similar implementation tying the session to the
48+
* proper transaction.
49+
*
50+
* @param sqlSession Must be non {@literal null}.
51+
*/
52+
public MyBatisDataAccessStrategy(SqlSession sqlSession) {
53+
this.sqlSession = sqlSession;
5354
}
5455

5556
@Override

src/test/java/org/springframework/data/jdbc/core/MyBatisDataAccessStrategyUnitTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@
3939
*/
4040
public class MyBatisDataAccessStrategyUnitTests {
4141

42-
SqlSessionFactory sessionFactory = mock(SqlSessionFactory.class);
4342
SqlSession session = mock(SqlSession.class);
4443
ArgumentCaptor<MyBatisContext> captor = ArgumentCaptor.forClass(MyBatisContext.class);
4544

46-
MyBatisDataAccessStrategy accessStrategy = new MyBatisDataAccessStrategy(sessionFactory);
45+
MyBatisDataAccessStrategy accessStrategy = new MyBatisDataAccessStrategy(session);
4746

4847
@Before
4948
public void before() {
5049

51-
doReturn(session).when(sessionFactory).openSession();
5250
doReturn(false).when(session).selectOne(any(), any());
5351
}
5452

src/test/java/org/springframework/data/jdbc/mybatis/MyBatisHsqlIntegrationTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.Rule;
2727
import org.junit.Test;
2828
import org.mybatis.spring.SqlSessionFactoryBean;
29+
import org.mybatis.spring.SqlSessionTemplate;
2930
import org.springframework.beans.factory.annotation.Autowired;
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.Import;
@@ -38,6 +39,8 @@
3839
import org.springframework.test.context.junit4.rules.SpringMethodRule;
3940
import org.springframework.transaction.annotation.Transactional;
4041

42+
import javax.net.ssl.SSLSocketFactory;
43+
4144
/**
4245
* Tests the integration with Mybatis.
4346
*
@@ -75,8 +78,13 @@ SqlSessionFactoryBean createSessionFactory(EmbeddedDatabase db) {
7578
}
7679

7780
@Bean
78-
MyBatisDataAccessStrategy dataAccessStrategy(SqlSessionFactory factory) {
79-
return new MyBatisDataAccessStrategy(factory);
81+
SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory factory) {
82+
return new SqlSessionTemplate(factory);
83+
}
84+
85+
@Bean
86+
MyBatisDataAccessStrategy dataAccessStrategy(SqlSession sqlSession) {
87+
return new MyBatisDataAccessStrategy(sqlSession);
8088
}
8189
}
8290

0 commit comments

Comments
 (0)