本文整理汇总了Java中org.mybatis.spring.SqlSessionTemplate类的典型用法代码示例。如果您正苦于以下问题:Java SqlSessionTemplate类的具体用法?Java SqlSessionTemplate怎么用?Java SqlSessionTemplate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SqlSessionTemplate类属于org.mybatis.spring包,在下文中一共展示了SqlSessionTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
public static QueryLookupStrategy create(
MybatisMappingContext context,
SqlSessionTemplate sqlSessionTemplate,
Dialect dialect,
Key key,
EvaluationContextProvider evaluationContextProvider) {
Assert.notNull(evaluationContextProvider, "EvaluationContextProvider must not be null!");
switch (key != null ? key : Key.CREATE_IF_NOT_FOUND) {
case CREATE:
return new CreateQueryLookupStrategy(context, sqlSessionTemplate, dialect);
case USE_DECLARED_QUERY:
return new DeclaredQueryLookupStrategy(sqlSessionTemplate, evaluationContextProvider);
case CREATE_IF_NOT_FOUND:
return new CreateIfNotFoundQueryLookupStrategy(
new CreateQueryLookupStrategy(context, sqlSessionTemplate, dialect),
new DeclaredQueryLookupStrategy(sqlSessionTemplate, evaluationContextProvider));
default:
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key));
}
}
示例2: PartTreeMybatisQuery
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
protected PartTreeMybatisQuery(
MybatisMappingContext context,
SqlSessionTemplate sqlSessionTemplate, Dialect dialect, MybatisQueryMethod method) {
super(sqlSessionTemplate, method);
this.context = context;
this.dialect = dialect;
this.entityInformation = method.getEntityInformation();
this.domainClass = this.entityInformation.getJavaType();
this.tree = new PartTree(method.getName(), domainClass);
this.parameters = method.getParameters();
this.persistentEntity = context.getPersistentEntity(domainClass);
this.generator = new MybatisMapperGenerator(dialect, persistentEntity);
this.statementName = super.getStatementName() + UUID.randomUUID().toString().replace("-", "");
doCreateQueryStatement(method); // prepare mybatis statement.
}
示例3: myBatisBatchOperationsSession
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Bean
@Qualifier("batch-operations")
@SuppressWarnings("SpringJavaAutowiringInspection")
public SqlSession myBatisBatchOperationsSession(DataSource dataSource) throws Exception {
/*
NOTE: Unfortunately, in MyBatis it's not possible to execute batch and non-batch operations in single SqlSession.
To support this scenario, we have to create completely new SqlSessionFactoryBean and completely new
SqlSession. Surprisingly, this does not necessarily mean that the batch and non-batch operations will be
executed in different transactions (as we would expect) - we tested this configuration using scenario 8.
and it turned out that the bot non-batch and batch operations were run using same connection and in same transaction.
I guess this has something to do with how connection is obtained by MyBatis from dataSource...
*/
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
return new SqlSessionTemplate(sqlSessionFactoryBean.getObject(), ExecutorType.BATCH);
}
示例4: setApplicationContext
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, SqlSessionFactory> sqlSessionFactories = applicationContext.getBeansOfType(SqlSessionFactory.class);
if (sqlSessionFactories.isEmpty()) {
return;
}
MapperHelperForSharding mapperHelperForSharding = new MapperHelperForSharding();
List<SqlSession> sqlSessions = new ArrayList<>(sqlSessionFactories.size());
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactories.values()) {
SqlSession sqlSession = new SqlSessionTemplate(sqlSessionFactory);
sqlSessions.add(sqlSession);
}
//Mapper代码增强 每个方法扩展出一个ShardingMapper类,增强为512个方法。
this.needEnhancedClassesArray = needEnhancedClasses.split(",");
this.enhanceMapperClass();
mapperHelperForSharding.setMappers(needEnhancedClassesArray);
mapperHelperForSharding.setSqlSessions(sqlSessions.toArray(new SqlSession[0]));
mapperHelperForSharding.initMapper();
}
示例5: testMixedWithConfigurationFileAndInterceptor
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testMixedWithConfigurationFileAndInterceptor() {
TestPropertyValues.of(
"mybatis.config-location:mybatis-config-settings-only.xml")
.applyTo(this.context);
this.context.register(EmbeddedDataSourceConfiguration.class,
MybatisInterceptorConfiguration.class);
this.context.refresh();
org.apache.ibatis.session.Configuration configuration = this.context.getBean(
SqlSessionFactory.class).getConfiguration();
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
assertThat(configuration.getInterceptors()).hasSize(1);
assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class);
}
示例6: testMixedWithConfigurationFileAndDatabaseIdProvider
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testMixedWithConfigurationFileAndDatabaseIdProvider() {
TestPropertyValues.of(
"mybatis.config-location:mybatis-config-settings-only.xml")
.applyTo(this.context);
this.context.register(EmbeddedDataSourceConfiguration.class,
MybatisBootMapperScanAutoConfiguration.class,
DatabaseProvidersConfiguration.class);
this.context.refresh();
org.apache.ibatis.session.Configuration configuration = this.context.getBean(
SqlSessionFactory.class).getConfiguration();
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
assertThat(configuration.getDatabaseId()).isEqualTo("h2");
}
示例7: testMixedWithConfigurationFileAndTypeHandlersPackage
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testMixedWithConfigurationFileAndTypeHandlersPackage() {
TestPropertyValues.of(
"mybatis.config-location:mybatis-config-settings-only.xml",
"mybatis.type-handlers-package:org.mybatis.spring.boot.autoconfigure.handler")
.applyTo(this.context);
this.context.register(EmbeddedDataSourceConfiguration.class,
MybatisBootMapperScanAutoConfiguration.class);
this.context.refresh();
org.apache.ibatis.session.Configuration configuration = this.context.getBean(
SqlSessionFactory.class).getConfiguration();
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(BigInteger.class)).isInstanceOf(DummyTypeHandler.class);
}
示例8: testMixedWithConfigurationFileAndTypeAliasesPackageAndMapperLocations
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testMixedWithConfigurationFileAndTypeAliasesPackageAndMapperLocations() {
TestPropertyValues.of(
"mybatis.config-location:mybatis-config-settings-only.xml",
"mybatis.type-aliases-package:org.mybatis.spring.boot.autoconfigure.domain",
"mybatis.mapper-locations:classpath:org/mybatis/spring/boot/autoconfigure/repository/CityMapper.xml")
.applyTo(this.context);
this.context.register(EmbeddedDataSourceConfiguration.class,
MybatisBootMapperScanAutoConfiguration.class);
this.context.refresh();
org.apache.ibatis.session.Configuration configuration = this.context.getBean(
SqlSessionFactory.class).getConfiguration();
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
assertThat(configuration.getMappedStatementNames()).contains("selectCityById");
assertThat(configuration.getMappedStatementNames()).contains("org.mybatis.spring.boot.autoconfigure.repository.CityMapperImpl.selectCityById");
}
示例9: testScanWithExplicitSqlSessionTemplate
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testScanWithExplicitSqlSessionTemplate() throws Exception {
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(SqlSessionTemplate.class);
ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();
constructorArgs.addGenericArgumentValue(new RuntimeBeanReference("sqlSessionFactory"));
definition.setConstructorArgumentValues(constructorArgs);
applicationContext.registerBeanDefinition("sqlSessionTemplate", definition);
applicationContext.register(AppConfigWithSqlSessionTemplate.class);
startContext();
// all interfaces with methods should be loaded
applicationContext.getBean("mapperInterface");
applicationContext.getBean("mapperSubinterface");
applicationContext.getBean("mapperChildInterface");
applicationContext.getBean("annotatedMapper");
}
示例10: testAddToConfigTrue
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testAddToConfigTrue() throws Exception {
// the default SqlSessionFactory in AbstractMyBatisSpringTest is created with an explicitly set
// MapperLocations list, so create a new factory here that tests auto-loading the config
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDatabaseIdProvider(null);
// mapperLocations properties defaults to null
factoryBean.setDataSource(dataSource);
factoryBean.setPlugins(new Interceptor[] { executorInterceptor });
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
find(new SqlSessionTemplate(sqlSessionFactory), true);
assertCommit(); // SqlSesssionTemplate autocommits
assertSingleConnection();
assertExecuteCount(1);
}
示例11: testAddToConfigFalse
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test(expected = org.apache.ibatis.binding.BindingException.class)
public void testAddToConfigFalse() throws Throwable {
try {
// the default SqlSessionFactory in AbstractMyBatisSpringTest is created with an explicitly
// set MapperLocations list, so create a new factory here that tests auto-loading the
// config
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// mapperLocations properties defaults to null
factoryBean.setDataSource(dataSource);
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
find(new SqlSessionTemplate(sqlSessionFactory), false);
fail("TestDao's mapper xml should not be loaded");
} catch (MyBatisSystemException mbse) {
// unwrap exception so the exact MyBatis exception can be tested
throw mbse.getCause();
} finally {
// connection not used; force close to avoid failing in validateConnectionClosed()
connection.close();
}
}
示例12: testWithNonSpringTransactionFactory
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testWithNonSpringTransactionFactory() throws Exception {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), dataSource,null);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
try {
find(new SqlSessionTemplate(sqlSessionFactory));
assertCommit(); // SqlSessionTemplate autocommits
assertCommitSession();
assertSingleConnection();
assertExecuteCount(1);
} finally {
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
示例13: testQuery
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
@Transactional
public void testQuery() throws Exception {
springfieldQueryTemplateRepository.save(new SpringfieldQueryTemplate("a", 1));
springfieldQueryTemplateRepository.save(new SpringfieldQueryTemplate("b", 2));
springfieldQueryTemplateRepository.save(new SpringfieldQueryTemplate("c", 3));
List<SpringfieldQueryTemplate> result = springfieldQueryTemplateRepository.execute(
new TemplateCallback<List<SpringfieldQueryTemplate>, SqlSessionTemplate>() {
public List<SpringfieldQueryTemplate> doInTemplate(SqlSessionTemplate template) {
SpringfieldQueryTemplate query = new SpringfieldQueryTemplate("a", 1);
Map<String,Object> parameter = new HashMap<String,Object>();
parameter.put("query", query);
String statement = SpringfieldQueryTemplate.class.getName()+".customQuery";
return template.selectList(statement, parameter);
}
});
Assert.assertEquals(result.size(), 1);
}
示例14: testWithNonSpringTransactionFactory
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Test
public void testWithNonSpringTransactionFactory() throws Exception {
Environment original = sqlSessionFactory.getConfiguration().getEnvironment();
Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), dataSource);
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
try {
find(new SqlSessionTemplate(sqlSessionFactory));
assertCommit(); // SqlSessionTemplate autocommits
assertCommitSession();
assertSingleConnection();
assertExecuteCount(1);
} finally {
sqlSessionFactory.getConfiguration().setEnvironment(original);
}
}
示例15: sqlSessionTemplate
import org.mybatis.spring.SqlSessionTemplate; //导入依赖的package包/类
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
ExecutorType executorType = this.properties.getExecutorType();
if (executorType != null) {
return new SqlSessionTemplate(sqlSessionFactory, executorType);
} else {
return new SqlSessionTemplate(sqlSessionFactory);
}
}