当前位置: 首页>>代码示例>>Java>>正文


Java SqlSessionTemplate类代码示例

本文整理汇总了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));
    }
}
 
开发者ID:hatunet,项目名称:spring-data-mybatis,代码行数:21,代码来源:MybatisQueryLookupStrategy.java

示例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.
}
 
开发者ID:hatunet,项目名称:spring-data-mybatis,代码行数:17,代码来源:PartTreeMybatisQuery.java

示例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);
}
 
开发者ID:bwajtr,项目名称:java-persistence-frameworks-comparison,代码行数:20,代码来源:DbTestsApplication.java

示例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();
}
 
开发者ID:baihui212,项目名称:tsharding,代码行数:20,代码来源:MapperShardingInitializer.java

示例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);
}
 
开发者ID:mybatis,项目名称:spring-boot-starter,代码行数:19,代码来源:MybatisAutoConfigurationTest.java

示例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");
}
 
开发者ID:mybatis,项目名称:spring-boot-starter,代码行数:19,代码来源:MybatisAutoConfigurationTest.java

示例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);
}
 
开发者ID:mybatis,项目名称:spring-boot-starter,代码行数:19,代码来源:MybatisAutoConfigurationTest.java

示例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");
}
 
开发者ID:mybatis,项目名称:spring-boot-starter,代码行数:21,代码来源:MybatisAutoConfigurationTest.java

示例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");
  
}
 
开发者ID:lindzh,项目名称:mybatis-spring-1.2.2,代码行数:21,代码来源:MapperScanTest.java

示例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);
}
 
开发者ID:lindzh,项目名称:mybatis-spring-1.2.2,代码行数:18,代码来源:MapperFactoryBeanTest.java

示例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();
  }
}
 
开发者ID:lindzh,项目名称:mybatis-spring-1.2.2,代码行数:23,代码来源:MapperFactoryBeanTest.java

示例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);
  }
}
 
开发者ID:lindzh,项目名称:mybatis-spring-1.2.2,代码行数:18,代码来源:MapperFactoryBeanTest.java

示例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);
}
 
开发者ID:u2ware,项目名称:springfield,代码行数:26,代码来源:SpringfieldQueryTemplateTest.java

示例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);
  }
}
 
开发者ID:zyong2004,项目名称:mybatis-spring,代码行数:18,代码来源:MapperFactoryBeanTest.java

示例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);
    }
}
 
开发者ID:baomidou,项目名称:mybatisplus-boot-starter,代码行数:11,代码来源:MybatisPlusAutoConfiguration.java


注:本文中的org.mybatis.spring.SqlSessionTemplate类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。