本文整理汇总了Java中org.apache.ibatis.mapping.MappedStatement类的典型用法代码示例。如果您正苦于以下问题:Java MappedStatement类的具体用法?Java MappedStatement怎么用?Java MappedStatement使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MappedStatement类属于org.apache.ibatis.mapping包,在下文中一共展示了MappedStatement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onInterceptor
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
@Override
public Object onInterceptor(Invocation invocation) throws Throwable {
Object[] objects = invocation.getArgs();
MappedStatement ms = (MappedStatement) objects[0];
//已指定强制使用
if (DataSourceContextHolder.get().isForceUseMaster()) {
logger.debug("Method[{}] force use Master..", ms.getId());
return null;
}
//读方法
if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
//!selectKey 为自增id查询主键(SELECT LAST_INSERT_ID() )方法,使用主库
if (!ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
DataSourceContextHolder.get().useSlave(true);
logger.debug("Method[{} use Slave Strategy..", ms.getId());
}
} else {
logger.debug("Method[{}] use Master Strategy..", ms.getId());
DataSourceContextHolder.get().useSlave(false);
}
return null;
}
示例2: genSql
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
private void genSql(Invocation invocation, SplitRequest splitRequest) {
try {
genParams(invocation, splitRequest);
BoundSql boundSql = ((MappedStatement)invocation.getArgs()[0]).getBoundSql(invocation.getArgs()[1]);
Statement statement = CCJSqlParserUtil.parse(boundSql.getSql());
String sql = parseSql(statement);
FieldUtil.setValue("sql", BoundSql.class, boundSql, sql);
MappedStatement mappedStatement = copyMappedStatement((MappedStatement)invocation.getArgs()[0], boundSql);
invocation.getArgs()[0] = mappedStatement;
} catch (OgnlException ognl) {
logger.error("ognl exception", ognl);
} catch (JSQLParserException e) {
logger.error("jSqlParser exception", e);
} catch (Exception e1) {
logger.error("error happend", e1);
}
}
示例3: getMappedStatement
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
@Override
public MappedStatement getMappedStatement(String id) {
Exception exception = null;
MappedStatement mappedStatement = null;
for (SqlSessionFactory sqlSessionFactory : getSqlSessionFactories()) {
try {
mappedStatement = sqlSessionFactory.getConfiguration().getMappedStatement(id);
} catch (Exception e) {
// ignore exception
exception = e;
}
if (mappedStatement != null) {
return mappedStatement;
}
}
throw new BindingException("Invalid bound statement (not found): " + id, exception);
}
示例4: copyFromMappedStatement
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
private MappedStatement copyFromMappedStatement(MappedStatement ms,
SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(
ms.getConfiguration(), ms.getId(), newSqlSource,
ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (null != ms.getKeyProperties()) {
for (String keyProperty : ms.getKeyProperties()) {
builder.keyProperty(keyProperty);
}
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.cache(ms.getCache());
return builder.build();
}
示例5: intercept
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO Auto-generated method stub
if (invocation.getTarget() instanceof RoutingStatementHandler) {
StatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
String mapperId = mappedStatement.getId();
String mapperClassName = mapperId.substring(0, mapperId.lastIndexOf("."));
Class<?> mapperClass = Class.forName(mapperClassName);
if (isDaoMapper(mapperClass)&&mapperId.endsWith(".insertUseReturnGeneratedKeys")) {
BaseStatementHandler baseStatementHandler = (BaseStatementHandler) metaStatementHandler.getValue("delegate");
if ("query".equals(invocation.getMethod().getName())) {
return handPreparedStatementHandlerQuery(invocation, baseStatementHandler);
} else if ("prepare".equals(invocation.getMethod().getName())) {
return handPreparedStatementHandlerPrepare(invocation, baseStatementHandler);
}
}
}
return invocation.proceed();
}
示例6: addMappedStatement
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
public MappedStatement addMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
SqlCommandType sqlCommandType, Class<?> parameterClass, String resultMap, Class<?> resultType,
KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
String statementName = mapperClass.getName() + "." + id;
if (configuration.hasStatement(statementName)) {
System.err.println("{" + statementName
+ "} Has been loaded by XML or SqlProvider, ignoring the injection of the SQL.");
return null;
}
/* 缓存逻辑处理 */
boolean isSelect = false;
if (sqlCommandType == SqlCommandType.SELECT) {
isSelect = true;
}
return builderAssistant.addMappedStatement(id, sqlSource, StatementType.PREPARED, sqlCommandType, null, null, null,
parameterClass, resultMap, resultType, null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn,
configuration.getDatabaseId(), languageDriver, null);
}
示例7: intercept
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
/**
* 拦截后要执行的方法
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
Page<?> page = PageThreadLocal.getThreadLocalPage();
if (page == null) {
return invocation.proceed();
}
PageThreadLocal.removeThreadLocalPage();
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
// 通过反射获取到当前RoutingStatementHandler对象的delegate属性
StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
BoundSql boundSql = delegate.getBoundSql();
MappedStatement mappedStatement = (MappedStatement) ReflectUtil.getFieldValue(delegate, "mappedStatement");
// 获取当前要执行的Sql语句,也就是我们直接在Mapper映射语句中写的Sql语句
String sql = boundSql.getSql();
// 是否查询总页数和总数据 默认为TRUE
if (page.getTotalFlag()) {
// 给当前的page参数对象设置总记录数
this.setTotalRecord(page, mappedStatement, boundSql, sql);
}
String pageSql = this.getPageSql(sql, page);
// 利用反射设置当前BoundSql对应的sql属性为我们建立好的分页Sql语句
ReflectUtil.setFieldValue(boundSql, "sql", pageSql);
return invocation.proceed();
}
示例8: intercept
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
Author author = authorAware.getAuthor();
if (null == author) {
return invocation.proceed();
}
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object record = invocation.getArgs()[1];
if (record instanceof AuthorTraceable) {
AuthorTraceable authorTraceableRecord = (AuthorTraceable) record;
if (mappedStatement.getSqlCommandType().equals(SqlCommandType.INSERT)) {
authorTraceableRecord.setCreateUser(author.getAuthorId());
}
authorTraceableRecord.setUpdateUser(author.getAuthorId());
}
return invocation.proceed();
}
示例9: insertList
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
/**
* 批量插入
*
* @param ms
*/
public String insertList(MappedStatement ms) {
final Class<?> entityClass = getEntityClass(ms);
//开始拼sql
StringBuilder sql = new StringBuilder();
sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.insertColumns(entityClass, true, false, false));
sql.append(" VALUES ");
sql.append("<foreach collection=\"list\" item=\"record\" separator=\",\" >");
sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
//获取全部列
Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
//当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
for (EntityColumn column : columnList) {
if (!column.isId() && column.isInsertable()) {
sql.append(column.getColumnHolder("record") + ",");
}
}
sql.append("</trim>");
sql.append("</foreach>");
return sql.toString();
}
示例10: deleteByIds
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
/**
* 根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段
*
* @param ms
* @return
*/
public String deleteByIds(MappedStatement ms) {
final Class<?> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
if (columnList.size() == 1) {
EntityColumn column = columnList.iterator().next();
sql.append(" where ");
sql.append(column.getColumn());
sql.append(" in (${_parameter})");
} else {
throw new RuntimeException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
}
return sql.toString();
}
示例11: selectByIds
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
/**
* 根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段
*
* @param ms
* @return
*/
public String selectByIds(MappedStatement ms) {
final Class<?> entityClass = getEntityClass(ms);
//将返回值修改为实体类型
setResultType(ms, entityClass);
StringBuilder sql = new StringBuilder();
sql.append(SqlHelper.selectAllColumns(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
if (columnList.size() == 1) {
EntityColumn column = columnList.iterator().next();
sql.append(" where ");
sql.append(column.getColumn());
sql.append(" in (${_parameter})");
} else {
throw new RuntimeException("继承 selectByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
}
return sql.toString();
}
示例12: executeQueryCount
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
private Long executeQueryCount(Executor executor, MappedStatement countMs, Object parameter,
BoundSql boundSql, RowBounds rowBounds,
ResultHandler resultHandler) throws IllegalAccessException,
SQLException {
CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT,
boundSql);
String orignSql = boundSql.getSql().replaceAll(";$", "");
// count sql
String countSql = PageSqlUtils.getCountSql(orignSql);
BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql,
boundSql.getParameterMappings(), parameter);
// 执行 count 查询
Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT,
resultHandler, countKey, countBoundSql);
Long count = (Long) ((List) countResultList).get(0);
return count;
}
示例13: getDynamicDataSource
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
/**
* 获得动态数据源
*
* @param ms MappedStatement
* @param parameterObject Parameter
* @return DynamicDataSourceGlobal
*/
private DynamicDataSourceGlobal getDynamicDataSource(MappedStatement ms, Object parameterObject) {
DynamicDataSourceGlobal dynamicDataSourceGlobal;
//读方法
if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
//!selectKey 为自增id查询主键(SELECT LAST_INSERT_ID() )方法,使用主库
if (ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
} else {
BoundSql boundSql = ms.getSqlSource().getBoundSql(parameterObject);
String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", " ");
if (sql.matches(REGEX)) {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
} else {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.READ;
}
}
} else {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
}
return dynamicDataSourceGlobal;
}
示例14: processConfiguration
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
/**
* 配置指定的接口
*
* @param configuration
* @param mapperInterface
*/
public void processConfiguration(Configuration configuration, Class<?> mapperInterface) {
String prefix;
if (mapperInterface != null) {
prefix = mapperInterface.getCanonicalName();
} else {
prefix = "";
}
for (Object object : new ArrayList<Object>(configuration.getMappedStatements())) {
if (object instanceof MappedStatement) {
MappedStatement ms = (MappedStatement) object;
if (ms.getId().startsWith(prefix) && isMapperMethod(ms.getId())) {
if (ms.getSqlSource() instanceof ProviderSqlSource) {
setSqlSource(ms);
}
}
}
}
}
示例15: intercept
import org.apache.ibatis.mapping.MappedStatement; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
//如果没有事务
if (!synchronizationActive) {
Object[] objects = invocation.getArgs();
MappedStatement ms = (MappedStatement) objects[0];
DynamicDataSourceGlobal dynamicDataSourceGlobal;
if ((dynamicDataSourceGlobal = CACHE_MAP.get(ms.getId())) == null) {
dynamicDataSourceGlobal = getDynamicDataSource(ms, objects[1]);
LOGGER.warn("设置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(), dynamicDataSourceGlobal.name(), ms.getSqlCommandType().name());
CACHE_MAP.put(ms.getId(), dynamicDataSourceGlobal);
}
DynamicDataSourceHolder.putDataSource(dynamicDataSourceGlobal);
}
return invocation.proceed();
}