本文整理汇总了Java中org.apache.ibatis.mapping.SqlCommandType类的典型用法代码示例。如果您正苦于以下问题:Java SqlCommandType类的具体用法?Java SqlCommandType怎么用?Java SqlCommandType使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SqlCommandType类属于org.apache.ibatis.mapping包,在下文中一共展示了SqlCommandType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: intercept
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
public Object intercept(Invocation invocation) throws Throwable {
/**
* 处理 DELETE UPDATE 语句
*/
MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
if (ms.getSqlCommandType() == SqlCommandType.DELETE || ms.getSqlCommandType() == SqlCommandType.UPDATE) {
Executor executor = (Executor) invocation.getTarget();
Configuration configuration = ms.getConfiguration();
Object parameter = invocation.getArgs()[1];
BoundSql boundSql = ms.getBoundSql(parameter);
Connection connection = executor.getTransaction().getConnection();
String databaseVersion = connection.getMetaData().getDatabaseProductVersion();
if (GlobalConfigUtils.getDbType(configuration).equals(DBType.MYSQL)
&& VersionUtils.compare(minMySQLVersion, databaseVersion)) {
logger.warn("Warn: Your mysql version needs to be greater than '5.6.3' to execute of Sql Explain!");
return invocation.proceed();
}
/**
* 执行 SQL 分析
*/
sqlExplain(configuration, ms, boundSql, connection, parameter);
}
return invocation.proceed();
}
示例2: onInterceptor
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的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;
}
示例3: prepareInsertAuthorMappedStatement
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
public static MappedStatement prepareInsertAuthorMappedStatement(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new StaticSqlSource(config,"INSERT INTO author (id,username,password,email,bio,favourite_section) values(?,?,?,?,?,?)"), SqlCommandType.INSERT)
.parameterMap(
new ParameterMap.Builder(
config, "defaultParameterMap", Author.class,
new ArrayList<ParameterMapping>() {
{
add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
}
}).build())
.cache(authorCache).build();
return ms;
}
示例4: prepareSelectAuthorViaOutParams
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
public static MappedStatement prepareSelectAuthorViaOutParams(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
MappedStatement ms = new MappedStatement.Builder(config, "selectAuthorViaOutParams", new StaticSqlSource(config, "{call selectAuthorViaOutParams(?,?,?,?,?)}"), SqlCommandType.SELECT)
.statementType(StatementType.CALLABLE)
.parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class,
new ArrayList<ParameterMapping>() {
{
add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).mode(ParameterMode.OUT).build());
add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).mode(ParameterMode.OUT).build());
add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).mode(ParameterMode.OUT).build());
add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).mode(ParameterMode.OUT).build());
}
}).build())
.resultMaps(new ArrayList<ResultMap>())
.cache(authorCache).build();
return ms;
}
示例5: addMappedStatement
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的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);
}
示例6: getSqlCommandType
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
private SqlCommandType getSqlCommandType(Method method) {
Class<? extends Annotation> type = getSqlAnnotationType(method);
if (type == null) {
type = getSqlProviderAnnotationType(method);
if (type == null) {
return SqlCommandType.UNKNOWN;
}
if (type == SelectProvider.class) {
type = Select.class;
} else if (type == InsertProvider.class) {
type = Insert.class;
} else if (type == UpdateProvider.class) {
type = Update.class;
} else if (type == DeleteProvider.class) {
type = Delete.class;
}
}
return SqlCommandType.valueOf(type.getSimpleName().toUpperCase(Locale.ENGLISH));
}
示例7: genKeyGenerator
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
/**
* <p>
* 自定义 KEY 生成器
* </p>
*/
public static KeyGenerator genKeyGenerator(TableInfo tableInfo, MapperBuilderAssistant builderAssistant,
String baseStatementId, LanguageDriver languageDriver) {
Incrementer incrementer = GlobalConfigUtils.getIncrementer(builderAssistant.getConfiguration());
if (null == incrementer) {
throw new MybatisPlusException("Error: not configure Incrementer implementation class.");
}
String id = baseStatementId + SelectKeyGenerator.SELECT_KEY_SUFFIX;
Class<?> resultTypeClass = tableInfo.getKeySequence().idClazz();
StatementType statementType = StatementType.PREPARED;
String keyProperty = tableInfo.getKeyProperty();
String keyColumn = tableInfo.getKeyColumn();
SqlSource sqlSource = languageDriver.createSqlSource(builderAssistant.getConfiguration(),
incrementer.getSequenceQuery(tableInfo.getKeySequence().value()), null);
builderAssistant.addMappedStatement(id, sqlSource, statementType, SqlCommandType.SELECT, null, null, null,
null, null, resultTypeClass, null, false, false, false,
new NoKeyGenerator(), keyProperty, keyColumn, null, languageDriver, null);
id = builderAssistant.applyCurrentNamespace(id, false);
MappedStatement keyStatement = builderAssistant.getConfiguration().getMappedStatement(id, false);
SelectKeyGenerator keyGenerator = new SelectKeyGenerator(keyStatement, true);
builderAssistant.getConfiguration().addKeyGenerator(id, keyGenerator);
return keyGenerator;
}
示例8: insert
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
public String insert(Class<?> entityType) {
// 命令名称
String statementId = buildStatementId(entityType, "insert");
if (configuration.hasStatement(statementId)) {
return statementId;
}
// 创建命令
String sql = new SqlBuilder(entityType).insert();
SqlSource sqlSource = new RawSqlSource(configuration, sql, null);
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, statementId, sqlSource,
SqlCommandType.INSERT);
// 判断是否需要自动生成主键
EntityMetadata metadata = EntityMetadata.get(entityType);
for (IdProperty idProperty : metadata.getIdProperties()) {
KeyGenerator keyGenerator = idProperty.getKeyGenerator();
if (keyGenerator == null) {
continue;
}
String propertyName = idProperty.getName();
String columnName = idProperty.getColumnName();
statementBuilder.keyProperty(propertyName).keyColumn(columnName).keyGenerator(keyGenerator);
}
MappedStatement statement = statementBuilder.build();
StatementResultMapHelper.addMappedStatement(configuration, statement);
return statementId;
}
示例9: build
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
/**
* @param configuration
* @param entity
*/
public static void build(Configuration configuration, LanguageDriver languageDriver,
EntityInfo entity) {
String[] names = GeneralSqlGenerator.methodDefines.updateName().split(",");
for (String name : names) {
String msId = entity.getMapperClass().getName() + "." + name;
EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
String sql = buildUpdateSql(entityMapper, name.endsWith("Selective"));
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql,
entity.getEntityClass());
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration,
msId, sqlSource, SqlCommandType.UPDATE);
MappedStatement statement = statementBuilder.build();
configuration.addMappedStatement(statement);
}
}
示例10: build
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
/**
* @param configuration
* @param entity
*/
public static void build(Configuration configuration, LanguageDriver languageDriver, EntityInfo entity) {
String msId = entity.getMapperClass().getName() + "." + GeneralSqlGenerator.methodDefines.deleteName();
// 从参数对象里提取注解信息
EntityMapper entityMapper = EntityHelper.getEntityMapper(entity.getEntityClass());
// 生成sql
String sql = buildDeleteSql(entityMapper);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, entity.getEntityClass());
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.DELETE);
MappedStatement statement = statementBuilder.build();
configuration.addMappedStatement(statement);
}
示例11: getDynamicDataSource
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的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;
}
示例12: intercept
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的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();
}
示例13: intercept
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object record = invocation.getArgs()[1];
if (record instanceof TimeTraceable) {
TimeTraceable timeTraceableRecord = (TimeTraceable) record;
LocalDateTime now = LocalDateTime.now();
if (mappedStatement.getSqlCommandType().equals(SqlCommandType.INSERT)) {
timeTraceableRecord.setCreateTime(now);
}
timeTraceableRecord.setUpdateTime(now);
} else if (record instanceof TimestampTraceable) {
TimestampTraceable timestampTraceableRecord = (TimestampTraceable) record;
long timestamp = Instant.now().getEpochSecond();
if (mappedStatement.getSqlCommandType().equals(SqlCommandType.INSERT)) {
timestampTraceableRecord.setCreateTime(timestamp);
}
timestampTraceableRecord.setUpdateTime(timestamp);
}
return invocation.proceed();
}
示例14: SqlCommand
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
String statementName = mapperInterface.getName() + "." + method.getName();
MappedStatement ms = null;
if (configuration.hasStatement(statementName)) {
ms = configuration.getMappedStatement(statementName);
} else if (!mapperInterface.equals(method.getDeclaringClass())) { // issue #35
String parentStatementName = method.getDeclaringClass().getName() + "." + method.getName();
if (configuration.hasStatement(parentStatementName)) {
ms = configuration.getMappedStatement(parentStatementName);
}
}
if (ms == null) {
if(method.getAnnotation(Flush.class) != null){
name = null;
type = SqlCommandType.FLUSH;
} else {
throw new BindingException("Invalid bound statement (not found): " + statementName);
}
} else {
name = ms.getId();
type = ms.getSqlCommandType();
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
}
}
}
示例15: prepareInsertAuthorMappedStatementWithAutoKey
import org.apache.ibatis.mapping.SqlCommandType; //导入依赖的package包/类
public static MappedStatement prepareInsertAuthorMappedStatementWithAutoKey(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new StaticSqlSource(config,"INSERT INTO author (username,password,email,bio,favourite_section) values(?,?,?,?,?)"), SqlCommandType.INSERT)
.parameterMap(
new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {
{
add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
}
}).build())
.cache(authorCache)
.keyGenerator(new Jdbc3KeyGenerator())
.keyProperty("id")
.build();
return ms;
}