File tree Expand file tree Collapse file tree 3 files changed +24
-17
lines changed
main/java/org/springframework/data/jdbc/mybatis
test/java/org/springframework/data/jdbc Expand file tree Collapse file tree 3 files changed +24
-17
lines changed Original file line number Diff line number Diff line change 1515 */
1616package org .springframework .data .jdbc .mybatis ;
1717
18- import java .util .Collections ;
19- import java .util .Map ;
20-
2118import org .apache .ibatis .session .SqlSession ;
22- import org .apache .ibatis .session .SqlSessionFactory ;
2319import org .mybatis .spring .SqlSessionTemplate ;
2420import org .springframework .data .jdbc .core .DataAccessStrategy ;
2521import org .springframework .data .jdbc .mapping .model .JdbcPersistentProperty ;
2622import 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
Original file line number Diff line number Diff line change 3939 */
4040public 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
Original file line number Diff line number Diff line change 2626import org .junit .Rule ;
2727import org .junit .Test ;
2828import org .mybatis .spring .SqlSessionFactoryBean ;
29+ import org .mybatis .spring .SqlSessionTemplate ;
2930import org .springframework .beans .factory .annotation .Autowired ;
3031import org .springframework .context .annotation .Bean ;
3132import org .springframework .context .annotation .Import ;
3839import org .springframework .test .context .junit4 .rules .SpringMethodRule ;
3940import 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
You can’t perform that action at this time.
0 commit comments