#351 - Fix and simplify DataAccessStrategy construction.

Removed explicit generation of DefaultDataAccessStrategy since that is now done by default.

Using MyBatisDataAccessStrategy factory for creating an appropriate DataAccessStrategy in the presence of MyBatis.

Removed references to DefaultNamingStrategy since that is now merged into NamingStrategy.
This commit is contained in:
Jens Schauder
2018-03-28 10:37:48 +02:00
parent fdb545fd18
commit 05bc950a46
3 changed files with 8 additions and 89 deletions

View File

@@ -21,25 +21,15 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.sql.DataSource;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.DelegatingDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.mapping.event.BeforeSave;
import org.springframework.data.jdbc.mapping.model.ConversionCustomizer;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.lang.Nullable;
/**
@@ -92,29 +82,30 @@ public class AggregateConfiguration {
Map<String, String> keyColumnAliases = new HashMap<String, String>();
keyColumnAliases.put("models", "name");
return new DefaultNamingStrategy() {
return new NamingStrategy() {
@Override
public String getColumnName(JdbcPersistentProperty property) {
String defaultName = super.getColumnName(property);
String defaultName = NamingStrategy.super.getColumnName(property);
String key = getTableName(property.getOwner().getType()) + "." + defaultName;
return columnAliases.computeIfAbsent(key, __ -> defaultName);
}
@Override
public String getTableName(Class<?> type) {
return tableAliases.computeIfAbsent(super.getTableName(type), key -> key);
return tableAliases.computeIfAbsent(NamingStrategy.super.getTableName(type), key -> key);
}
@Override
public String getReverseColumnName(JdbcPersistentProperty property) {
return reverseColumnAliases.computeIfAbsent(property.getName(), __ -> super.getReverseColumnName(property));
return reverseColumnAliases.computeIfAbsent(property.getName(),
__ -> NamingStrategy.super.getReverseColumnName(property));
}
@Override
public String getKeyColumn(JdbcPersistentProperty property) {
return keyColumnAliases.computeIfAbsent(property.getName(), __ -> super.getKeyColumn(property));
return keyColumnAliases.computeIfAbsent(property.getName(), __ -> NamingStrategy.super.getKeyColumn(property));
}
};
}
@@ -140,21 +131,4 @@ public class AggregateConfiguration {
}
});
}
// temporary workaround for https://jira.spring.io/browse/DATAJDBC-155
@Bean
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, DataSource dataSource) {
NamedParameterJdbcOperations operations = new NamedParameterJdbcTemplate(dataSource);
DelegatingDataAccessStrategy accessStrategy = new DelegatingDataAccessStrategy();
accessStrategy.setDelegate(new DefaultDataAccessStrategy( //
new SqlGeneratorSource(context), //
operations, //
context, //
accessStrategy) //
);
return accessStrategy;
}
}

View File

@@ -15,22 +15,13 @@
*/
package example.springdata.jdbc.basics.simpleentity;
import javax.sql.DataSource;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.DelegatingDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.mapping.event.BeforeSave;
import org.springframework.data.jdbc.mapping.event.JdbcEvent;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
/**
* Contains infrastructure necessary for creating repositories and two listeners.
@@ -66,21 +57,4 @@ public class CategoryConfiguration {
}
};
}
// temporary workaround for https://jira.spring.io/browse/DATAJDBC-155
@Bean
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, DataSource dataSource) {
NamedParameterJdbcOperations operations = new NamedParameterJdbcTemplate(dataSource);
DelegatingDataAccessStrategy accessStrategy = new DelegatingDataAccessStrategy();
accessStrategy.setDelegate(new DefaultDataAccessStrategy( //
new SqlGeneratorSource(context), //
operations, //
context, //
accessStrategy) //
);
return accessStrategy;
}
}

View File

@@ -15,23 +15,13 @@
*/
package example.springdata.jdbc.mybatis;
import static java.util.Arrays.*;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jdbc.core.CascadingDataAccessStrategy;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.DelegatingDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mybatis.MyBatisDataAccessStrategy;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
/**
* @author Jens Schauder
@@ -40,27 +30,8 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@EnableJdbcRepositories
public class MyBatisConfiguration {
// temporary workaround for https://jira.spring.io/browse/DATAJDBC-155
@Bean
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, DataSource dataSource,
SqlSession sqlSession) {
NamedParameterJdbcOperations operations = new NamedParameterJdbcTemplate(dataSource);
DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy();
MyBatisDataAccessStrategy myBatisDataAccessStrategy = new MyBatisDataAccessStrategy(sqlSession);
CascadingDataAccessStrategy cascadingDataAccessStrategy = new CascadingDataAccessStrategy(
asList(myBatisDataAccessStrategy, delegatingDataAccessStrategy));
DefaultDataAccessStrategy defaultDataAccessStrategy = new DefaultDataAccessStrategy( //
new SqlGeneratorSource(context), //
operations, //
context, //
cascadingDataAccessStrategy);
delegatingDataAccessStrategy.setDelegate(defaultDataAccessStrategy);
return cascadingDataAccessStrategy;
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context, SqlSession sqlSession) {
return MyBatisDataAccessStrategy.createCombinedAccessStrategy(context, sqlSession);
}
}