DATAJDBC-161 - Polishing.
Removed second constructor and made tests use the new constructor. Changed parameter type to the interface SqlSession. Added some JavaDoc.
This commit is contained in:
@@ -15,16 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mybatis;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.springframework.data.jdbc.core.DataAccessStrategy;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link DataAccessStrategy} implementation based on MyBatis. Each method gets mapped to a statement. The name of the
|
||||
* 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 {
|
||||
|
||||
private final SqlSession sqlSession;
|
||||
|
||||
public MyBatisDataAccessStrategy(SqlSessionFactory sqlSessionFactory) {
|
||||
|
||||
this(new SqlSessionTemplate(sqlSessionFactory));
|
||||
}
|
||||
|
||||
public MyBatisDataAccessStrategy(SqlSessionTemplate sqlSessionTemplate) {
|
||||
|
||||
this.sqlSession = sqlSessionTemplate;
|
||||
/**
|
||||
* Constructs a {@link DataAccessStrategy} based on MyBatis.
|
||||
* <p>
|
||||
* Use a {@link SqlSessionTemplate} for {@link SqlSession} or a similar implementation tying the session to the
|
||||
* proper transaction.
|
||||
*
|
||||
* @param sqlSession Must be non {@literal null}.
|
||||
*/
|
||||
public MyBatisDataAccessStrategy(SqlSession sqlSession) {
|
||||
this.sqlSession = sqlSession;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -39,16 +39,14 @@ import org.springframework.data.mapping.PropertyPath;
|
||||
*/
|
||||
public class MyBatisDataAccessStrategyUnitTests {
|
||||
|
||||
SqlSessionFactory sessionFactory = mock(SqlSessionFactory.class);
|
||||
SqlSession session = mock(SqlSession.class);
|
||||
ArgumentCaptor<MyBatisContext> captor = ArgumentCaptor.forClass(MyBatisContext.class);
|
||||
|
||||
MyBatisDataAccessStrategy accessStrategy = new MyBatisDataAccessStrategy(sessionFactory);
|
||||
MyBatisDataAccessStrategy accessStrategy = new MyBatisDataAccessStrategy(session);
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
|
||||
doReturn(session).when(sessionFactory).openSession();
|
||||
doReturn(false).when(session).selectOne(any(), any());
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
@@ -38,6 +39,8 @@ import org.springframework.test.context.junit4.rules.SpringClassRule;
|
||||
import org.springframework.test.context.junit4.rules.SpringMethodRule;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
/**
|
||||
* Tests the integration with Mybatis.
|
||||
*
|
||||
@@ -75,8 +78,13 @@ public class MyBatisHsqlIntegrationTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
MyBatisDataAccessStrategy dataAccessStrategy(SqlSessionFactory factory) {
|
||||
return new MyBatisDataAccessStrategy(factory);
|
||||
SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory factory) {
|
||||
return new SqlSessionTemplate(factory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
MyBatisDataAccessStrategy dataAccessStrategy(SqlSession sqlSession) {
|
||||
return new MyBatisDataAccessStrategy(sqlSession);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user