Java Code Examples for org.mybatis.spring.SqlSessionTemplate

The following examples show how to use org.mybatis.spring.SqlSessionTemplate. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static QueryLookupStrategy create(SqlSessionTemplate sqlSessionTemplate,
		Key key, QueryMethodEvaluationContextProvider evaluationContextProvider) {

	Assert.notNull(sqlSessionTemplate, "SqlSessionTemplate must not be null!");
	Assert.notNull(evaluationContextProvider,
			"EvaluationContextProvider must not be null!");

	switch (key != null ? key : Key.CREATE_IF_NOT_FOUND) {
	case CREATE:
		return new CreateQueryLookupStrategy(sqlSessionTemplate);
	case USE_DECLARED_QUERY:
		return new DeclaredQueryLookupStrategy(sqlSessionTemplate,
				evaluationContextProvider);
	case CREATE_IF_NOT_FOUND:
		return new CreateIfNotFoundQueryLookupStrategy(sqlSessionTemplate,
				new CreateQueryLookupStrategy(sqlSessionTemplate),
				new DeclaredQueryLookupStrategy(sqlSessionTemplate,
						evaluationContextProvider));
	default:
		throw new IllegalArgumentException(
				String.format("Unsupported query lookup strategy %s!", key));
	}
}
 
Example 2
Source Project: jstarcraft-core   Source File: MyBatisAccessor.java    License: Apache License 2.0 6 votes vote down vote up
public MyBatisAccessor(Collection<Class<?>> classes, SqlSessionTemplate template) {
	this.template = template;

	Configuration configuration = template.getConfiguration();
	for (Class clazz : classes) {
		if (!configuration.hasMapper(clazz)) {
			configuration.addMapper(clazz);
		}

		MyBatisMetadata metadata = new MyBatisMetadata(clazz);
		metadatas.put(metadata.getOrmClass(), metadata);

		String maximumIdSql = StringUtility.format(MAXIMUM_ID, metadata.getColumnName(metadata.getPrimaryName()));
		maximumIdSqls.put(metadata.getOrmClass(), maximumIdSql);

		String minimumIdSql = StringUtility.format(MINIMUM_ID, metadata.getColumnName(metadata.getPrimaryName()));
		minimumIdSqls.put(metadata.getOrmClass(), minimumIdSql);
	}
}
 
Example 3
Source Project: tsharding   Source File: MapperShardingInitializer.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example 4
Source Project: spring-data-mybatis   Source File: PartTreeMybatisQuery.java    License: Apache License 2.0 6 votes vote down vote up
PartTreeMybatisQuery(MybatisQueryMethod method,
		SqlSessionTemplate sqlSessionTemplate) {

	super(method, sqlSessionTemplate);

	Class<?> domainClass = method.getEntityInformation().getJavaType();
	this.parameters = method.getParameters();

	boolean recreationRequired = parameters.hasDynamicProjection()
			|| parameters.potentiallySortsDynamically();

	try {

		this.tree = new PartTree(method.getName(), domainClass);

	}
	catch (Exception o_O) {
		throw new IllegalArgumentException(
				String.format("Failed to create query for method %s! %s", method,
						o_O.getMessage()),
				o_O);
	}

	this.execution = createExecution();

}
 
Example 5
Source Project: pinpoint   Source File: SqlSessionTemplateITBase.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    Configuration configuration = mock(Configuration.class);
    TransactionFactory transactionFactory = mock(TransactionFactory.class);
    DataSource dataSource = mock(DataSource.class);
    Environment environment = new Environment("test", transactionFactory, dataSource);
    when(configuration.getEnvironment()).thenReturn(environment);
    when(this.sqlSessionFactory.getConfiguration()).thenReturn(configuration);
    when(this.sqlSessionFactory.openSession(EXECUTOR_TYPE)).thenReturn(this.sqlSessionProxy);
    this.sqlSessionTemplate = new SqlSessionTemplate(this.sqlSessionFactory, EXECUTOR_TYPE);
}
 
Example 6
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 7
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
	ExecutorType executorType = this.properties.getExecutorType();
	if (executorType != null) {
		return new SqlSessionTemplate(sqlSessionFactory, executorType);
	} else {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
 
Example 8
@Override
protected RepositoryQuery resolveQuery(MybatisQueryMethod method,
		SqlSessionTemplate sqlSessionTemplate, NamedQueries namedQueries) {

	if (method.isAnnotatedQuery()) {
		return new SimpleMybatisQuery(method, sqlSessionTemplate);
	}

	throw new IllegalStateException(String.format(
			"Did neither find a NamedQuery nor an annotated query for method %s!",
			method));
}
 
Example 9
Source Project: genericdao   Source File: GenericJpaRepositoryFactory.java    License: Artistic License 2.0 5 votes vote down vote up
public GenericJpaRepositoryFactory(EntityManager entityManager , SqlSessionTemplate sqlSessionTemplate) {
	super(entityManager) ;
	//设置当前类的实体管理器
	this.entityManager = entityManager ;
	//设置sqlSessionTemplate,线程安全
	this.sqlSessionTemplate = sqlSessionTemplate ;

	this.extractor = PersistenceProvider.fromEntityManager(entityManager);
}
 
Example 10
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 11
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 12
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 13
public SimpleMybatisRepository(SqlSessionTemplate sqlSessionTemplate,
		RepositoryInformation repositoryInformation,
		MybatisEntityInformation<T, ID> entityInformation) {

	super(sqlSessionTemplate);

	Assert.notNull(entityInformation, "EntityInformation must not be null!");

	this.namespace = repositoryInformation.getRepositoryInterface().getName();
	this.entityInformation = entityInformation;
}
 
Example 14
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 15
@Bean(DATABASE_PREFIX + "SqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier(DATABASE_PREFIX + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
 
Example 16
Source Project: spring-data-mybatis   Source File: SimpleMybatisQuery.java    License: Apache License 2.0 5 votes vote down vote up
protected SimpleMybatisQuery(MybatisQueryMethod method,
		SqlSessionTemplate sqlSessionTemplate) {
	super(method, sqlSessionTemplate);

	commandType = SELECT;

	if (StringUtils.hasText(method.getAnnotatedQuery())) {
		String sql = method.getAnnotatedQuery();
		stringQuery = new StringQuery(sql);

		if (method.isModifyingQuery()) {
			SqlCommandType type = method.getModifyingType();
			if (null == type) {
				// auto judge
				if (sql.toLowerCase().startsWith("insert")) {
					commandType = INSERT;
				}
				else if (sql.toLowerCase().startsWith("update")) {
					commandType = UPDATE;
				}
				else if (sql.toLowerCase().startsWith("delete")) {
					commandType = DELETE;
				}
				else {
					commandType = UNKNOWN;
				}
			}
			else {
				commandType = type;
			}
		}

	}
	this.execution = createExecution();

}
 
Example 17
Source Project: genericdao   Source File: MybatisRepositoryQuery.java    License: Artistic License 2.0 5 votes vote down vote up
public MybatisRepositoryQuery(SqlSessionTemplate sqlSessionTemplate ,
                              Method method, RepositoryMetadata repositoryMetadata){
    this.sqlSessionTemplate = sqlSessionTemplate ;
    this.method = method ;
    this.repositoryMetadata = repositoryMetadata ;

    log.info("{}的领域类{}",repositoryMetadata.getRepositoryInterface().getName() , repositoryMetadata.getDomainType() );

    if(!mybatisMapperMap.containsKey(getMapperKey())){
        Object mapper = sqlSessionTemplate.getMapper(repositoryMetadata.getRepositoryInterface()) ;
        mybatisMapperMap.put(getMapperKey() , mapper) ;
    }

}
 
Example 18
Source Project: Mapper   Source File: MapperAutoConfiguration.java    License: MIT License 5 votes vote down vote up
@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);
    }
}
 
Example 19
Source Project: mapper-boot-starter   Source File: MapperAutoConfiguration.java    License: MIT License 5 votes vote down vote up
@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);
    }
}
 
Example 20
Source Project: saluki   Source File: MybatisConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    if (sqlSessionFactory == null) {
        return null;
    }
    return new SqlSessionTemplate(sqlSessionFactory);
}
 
Example 21
Source Project: aaden-pay   Source File: MyBatisDaoImpl.java    License: Apache License 2.0 5 votes vote down vote up
public Connection getConnection() {
	SqlSessionTemplate st = (SqlSessionTemplate) getSqlSession();
	Connection connection = SqlSessionUtils
			.getSqlSession(st.getSqlSessionFactory(), st.getExecutorType(), st.getPersistenceExceptionTranslator())
			.getConnection();
	return connection;
	// return getSqlSession().getConnection();
}
 
Example 22
Source Project: EasyReport   Source File: DataSourceConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Primary
@Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") final
                                             SqlSessionFactory sqlSessionFactory)
    throws Exception {
    return this.createSqlSessionTemplate(sqlSessionFactory);
}
 
Example 23
Source Project: Resource   Source File: DataSource2Config.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Bean(name = "db2SqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(
    @Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory)
    throws Exception
{
    return new SqlSessionTemplate(sqlSessionFactory);
}
 
Example 24
Source Project: mapper-boot-starter   Source File: MapperAutoConfiguration.java    License: MIT License 5 votes vote down vote up
@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);
    }
}
 
Example 25
Source Project: erp-framework   Source File: FramedbConfig.java    License: MIT License 4 votes vote down vote up
@Bean(name = "framedbSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("framedbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}
 
Example 26
protected SqlSessionRepositorySupport(SqlSessionTemplate sqlSessionTemplate) {
	Assert.notNull(sqlSessionTemplate, "SqlSessionTemplate must not be null!");
	this.sqlSession = sqlSessionTemplate;
}
 
Example 27
public CreateQueryLookupStrategy(SqlSessionTemplate sqlSessionTemplate) {
	super(sqlSessionTemplate);
}
 
Example 28
@Autowired
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
    this.sqlSessionTemplate = sqlSessionTemplate;
}
 
Example 29
Source Project: pmq   Source File: MySqlDatasourceConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(name = "mysqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(
        @Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}
 
Example 30
Source Project: pmq   Source File: InMemoryDbDatasourceConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(name = "inMemoryDbSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(
        @Qualifier("inMemoryDbSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}