use of org.apache.ibatis.session.Configuration in project mybatis.flying by limeng32.
the class AutoMapperInterceptor method intercept.
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = getRealObj(statementHandler);
String originalSql = (String) metaStatementHandler.getValue(DELEGATE_BOUNDSQL_SQL);
Configuration configuration = (Configuration) metaStatementHandler.getValue(DELEGATE_CONFIGURATION);
Object parameterObject = metaStatementHandler.getValue(DELEGATE_BOUNDSQL_PARAMETEROBJECT);
FlyingModel flyingModel = CookOriginalSql.fetchFlyingFeature(originalSql);
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue(DELEGATE_MAPPEDSTATEMENT);
if (flyingModel.isHasFlyingFeature()) {
if ((flyingModel.getDataSourceId() != null) && !((Connection) invocation.getArgs()[0]).getCatalog().equalsIgnoreCase(flyingModel.getConnectionCatalog())) {
ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
if (applicationContext != null) {
DataSource dataSource = (DataSource) applicationContext.getBean(flyingModel.getDataSourceId());
if (dataSource == null) {
throw new AutoMapperException(AutoMapperExceptionEnum.cannotFindAssignedDataSourceInContext.description());
}
Connection connection = ((SmartDataSource) (applicationContext.getBean(flyingModel.getDataSourceId()))).getConnection();
invocation.getArgs()[0] = connection;
} else {
throw new AutoMapperException(AutoMapperExceptionEnum.cannotFindApplicationContextProvider.description());
}
}
String newSql = "";
switch(flyingModel.getActionType()) {
case count:
newSql = SqlBuilder.buildCountSql(parameterObject);
break;
case delete:
newSql = SqlBuilder.buildDeleteSql(parameterObject);
break;
case insert:
newSql = SqlBuilder.buildInsertSql(parameterObject, flyingModel);
break;
case select:
newSql = SqlBuilder.buildSelectSql(mappedStatement.getResultMaps().get(0).getType(), flyingModel);
break;
case selectAll:
newSql = SqlBuilder.buildSelectAllSql(parameterObject, flyingModel);
break;
case selectOne:
newSql = SqlBuilder.buildSelectOneSql(parameterObject, flyingModel);
break;
case update:
newSql = SqlBuilder.buildUpdateSql(parameterObject, flyingModel);
break;
case updatePersistent:
newSql = SqlBuilder.buildUpdatePersistentSql(parameterObject, flyingModel);
break;
default:
break;
}
logger.warn(new StringBuffer("Auto generated sql:").append(newSql).toString());
SqlSource sqlSource = buildSqlSource(configuration, newSql, parameterObject.getClass());
List<ParameterMapping> parameterMappings = sqlSource.getBoundSql(parameterObject).getParameterMappings();
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_SQL, sqlSource.getBoundSql(parameterObject).getSql());
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_PARAMETERMAPPINGS, parameterMappings);
}
/* 开始处理分页问题 */
if (invocation.getTarget() instanceof RoutingStatementHandler) {
BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, DELEGATE);
mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, MAPPEDSTATEMENT);
BoundSql boundSql = delegate.getBoundSql();
if (parameterObject == null) {
throw new AutoMapperException(AutoMapperExceptionEnum.parameterObjectIsNull);
} else if (parameterObject instanceof Conditionable) {
Conditionable condition = (Conditionable) parameterObject;
String sql = boundSql.getSql();
if (condition.getLimiter() != null) {
Connection connection = (Connection) invocation.getArgs()[0];
String countSql = new StringBuffer("select count(0) from (").append(sql).append(") myCount").toString();
PreparedStatement countStmt = connection.prepareStatement(countSql);
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject);
setParameters(countStmt, mappedStatement, countBS, parameterObject);
ResultSet rs = countStmt.executeQuery();
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
rs.close();
countStmt.close();
condition.getLimiter().setTotalCount(count);
}
String pageSql = generatePageSql(sql, condition);
ReflectHelper.setValueByFieldName(boundSql, SQL, pageSql);
} else {
}
}
/* 调用原始statementHandler的prepare方法完成原本的逻辑 */
statementHandler = (StatementHandler) metaStatementHandler.getOriginalObject();
statementHandler.prepare((Connection) invocation.getArgs()[0], mappedStatement.getTimeout());
/* 传递给下一个拦截器处理 */
return invocation.proceed();
}
use of org.apache.ibatis.session.Configuration in project JessMA by ldcsaa.
the class MyBatisSessionMgr method buildSessionFactory.
/** 创建 {@link SqlSessionFactory} */
private void buildSessionFactory() {
synchronized (this) {
if (sessionFactory == null) {
try {
Reader reader = Resources.getResourceAsReader(configFile);
sessionFactory = new SqlSessionFactoryBuilder().build(reader, environment);
if (GeneralHelper.isStrNotEmpty(pattern)) {
Set<String> packages = PackageHelper.getPackages(pattern);
for (String pkg : packages) {
Set<Class<?>> entities = PackageHelper.getClasses(pkg, false, new ClassFilter() {
@Override
public boolean accept(Class<?> clazz) {
if (!BeanHelper.isPublicInterface(clazz))
return false;
return true;
}
});
Configuration cfg = sessionFactory.getConfiguration();
for (Class<?> clazz : entities) {
if (!cfg.hasMapper(clazz))
cfg.addMapper(clazz);
}
}
}
} catch (IOException e) {
throw new SqlSessionException(e);
}
}
}
}
use of org.apache.ibatis.session.Configuration in project sonarqube by SonarSource.
the class MyBatisTest method shouldConfigureMyBatis.
@Test
public void shouldConfigureMyBatis() {
underTest.start();
Configuration conf = underTest.getSessionFactory().getConfiguration();
assertThat(conf.isUseGeneratedKeys(), Is.is(true));
assertThat(conf.hasMapper(RuleMapper.class), Is.is(true));
assertThat(conf.isLazyLoadingEnabled(), Is.is(false));
}
use of org.apache.ibatis.session.Configuration in project mybatis-3 by mybatis.
the class BatchExecutor method doUpdate.
@Override
public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
final Configuration configuration = ms.getConfiguration();
final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);
final BoundSql boundSql = handler.getBoundSql();
final String sql = boundSql.getSql();
final Statement stmt;
if (sql.equals(currentSql) && ms.equals(currentStatement)) {
int last = statementList.size() - 1;
stmt = statementList.get(last);
applyTransactionTimeout(stmt);
//fix Issues 322
handler.parameterize(stmt);
BatchResult batchResult = batchResultList.get(last);
batchResult.addParameterObject(parameterObject);
} else {
Connection connection = getConnection(ms.getStatementLog());
stmt = handler.prepare(connection, transaction.getTimeout());
//fix Issues 322
handler.parameterize(stmt);
currentSql = sql;
currentStatement = ms;
statementList.add(stmt);
batchResultList.add(new BatchResult(ms, sql, parameterObject));
}
// handler.parameterize(stmt);
handler.batch(stmt);
return BATCH_UPDATE_RETURN_VALUE;
}
use of org.apache.ibatis.session.Configuration in project mybatis-3 by mybatis.
the class BatchExecutor method doQuery.
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
flushStatements();
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
Connection connection = getConnection(ms.getStatementLog());
stmt = handler.prepare(connection, transaction.getTimeout());
handler.parameterize(stmt);
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
Aggregations