List of usage examples for org.apache.ibatis.executor.keygen NoKeyGenerator NoKeyGenerator
NoKeyGenerator
From source file:cn.org.awcp.core.mybatis.mapper.MapperTemplate.java
License:Open Source License
/** * SelectKey - ?mysqlOracle?//from w w w . jav a 2s. c om * * @param ms * @param column */ protected void newSelectKeyMappedStatement(MappedStatement ms, EntityHelper.EntityColumn column) { String keyId = ms.getId() + SelectKeyGenerator.SELECT_KEY_SUFFIX; if (ms.getConfiguration().hasKeyGenerator(keyId)) { return; } Class<?> entityClass = getSelectReturnType(ms); // defaults Configuration configuration = ms.getConfiguration(); KeyGenerator keyGenerator = null; Boolean executeBefore = getBEFORE(); String IDENTITY = (column.getGenerator() == null || column.getGenerator().equals("")) ? getIDENTITY() : column.getGenerator(); if (IDENTITY.equalsIgnoreCase("JDBC")) { keyGenerator = new Jdbc3KeyGenerator(); } else { SqlSource sqlSource = new RawSqlSource(configuration, IDENTITY, entityClass); MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, keyId, sqlSource, SqlCommandType.SELECT); statementBuilder.resource(ms.getResource()); statementBuilder.fetchSize(null); statementBuilder.statementType(StatementType.STATEMENT); statementBuilder.keyGenerator(new NoKeyGenerator()); statementBuilder.keyProperty(column.getProperty()); statementBuilder.keyColumn(null); statementBuilder.databaseId(null); statementBuilder.lang(configuration.getDefaultScriptingLanuageInstance()); statementBuilder.resultOrdered(false); statementBuilder.resulSets(null); statementBuilder.timeout(configuration.getDefaultStatementTimeout()); List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>(); ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(configuration, statementBuilder.id() + "-Inline", entityClass, parameterMappings); statementBuilder.parameterMap(inlineParameterMapBuilder.build()); List<ResultMap> resultMaps = new ArrayList<ResultMap>(); ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(configuration, statementBuilder.id() + "-Inline", column.getJavaType(), new ArrayList<ResultMapping>(), null); resultMaps.add(inlineResultMapBuilder.build()); statementBuilder.resultMaps(resultMaps); statementBuilder.resultSetType(null); statementBuilder.flushCacheRequired(false); statementBuilder.useCache(false); statementBuilder.cache(null); MappedStatement statement = statementBuilder.build(); configuration.addMappedStatement(statement); MappedStatement keyStatement = configuration.getMappedStatement(keyId, false); keyGenerator = new SelectKeyGenerator(keyStatement, executeBefore); configuration.addKeyGenerator(keyId, keyGenerator); } // keyGenerator try { MetaObject msObject = forObject(ms); msObject.setValue("keyGenerator", keyGenerator); msObject.setValue("keyProperties", new String[] { column.getProperty() }); } catch (Exception e) { // ignore } }
From source file:com.baomidou.mybatisplus.mapper.AutoSqlInjector.java
License:Apache License
/** * <p>/* ww w. jav a 2s. com*/ * ? SQL ? * </p> * * @param selective ?? * @param mapperClass * @param modelClass * @param table */ protected void injectInsertOneSql(boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table) { /* * INSERT INTO table <trim prefix="(" suffix=")" suffixOverrides=","> * <if test="xx != null">xx,</if> </trim> <trim prefix="values (" * suffix=")" suffixOverrides=","> <if test="xx != null">#{xx},</if> * </trim> */ KeyGenerator keyGenerator = new NoKeyGenerator(); StringBuilder fieldBuilder = new StringBuilder(); StringBuilder placeholderBuilder = new StringBuilder(); SqlMethod sqlMethod = selective ? SqlMethod.INSERT_ONE : SqlMethod.INSERT_ONE_ALL_COLUMN; fieldBuilder.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n"); placeholderBuilder.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n"); String keyProperty = null; String keyColumn = null; // ??,??? if (StringUtils.isNotEmpty(table.getKeyProperty())) { if (table.getIdType() == IdType.AUTO) { /* */ keyGenerator = new Jdbc3KeyGenerator(); keyProperty = table.getKeyProperty(); keyColumn = table.getKeyColumn(); } else { if (null != table.getKeySequence()) { keyGenerator = TableInfoHelper.genKeyGenerator(table, builderAssistant, sqlMethod.getMethod(), languageDriver); keyProperty = table.getKeyProperty(); keyColumn = table.getKeyColumn(); fieldBuilder.append(table.getKeyColumn()).append(","); placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},"); } else { /* ID */ fieldBuilder.append(table.getKeyColumn()).append(","); // placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},"); } } } List<TableFieldInfo> fieldList = table.getFieldList(); for (TableFieldInfo fieldInfo : fieldList) { if (selective) { fieldBuilder.append(convertIfTagIgnored(fieldInfo, false)); fieldBuilder.append(fieldInfo.getColumn()).append(","); fieldBuilder.append(convertIfTagIgnored(fieldInfo, true)); placeholderBuilder.append(convertIfTagIgnored(fieldInfo, false)); placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},"); placeholderBuilder.append(convertIfTagIgnored(fieldInfo, true)); } else { fieldBuilder.append(fieldInfo.getColumn()).append(","); placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},"); } } fieldBuilder.append("\n</trim>"); placeholderBuilder.append("\n</trim>"); String sql = String.format(sqlMethod.getSql(), table.getTableName(), fieldBuilder.toString(), placeholderBuilder.toString()); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator, keyProperty, keyColumn); }
From source file:com.baomidou.mybatisplus.mapper.AutoSqlInjector.java
License:Apache License
public MappedStatement addSelectMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource, Class<?> resultType, TableInfo table) { if (null != table) { String resultMap = table.getResultMap(); if (null != resultMap) { /* resultMap */ return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, resultMap, null, new NoKeyGenerator(), null, null); }/*from ww w . j a v a 2 s .c o m*/ } /* */ return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, null, resultType, new NoKeyGenerator(), null, null); }
From source file:com.baomidou.mybatisplus.mapper.AutoSqlInjector.java
License:Apache License
public MappedStatement addDeleteMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource) { return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.DELETE, null, null, Integer.class, new NoKeyGenerator(), null, null); }
From source file:com.baomidou.mybatisplus.mapper.AutoSqlInjector.java
License:Apache License
public MappedStatement addUpdateMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id, SqlSource sqlSource) {//from w w w . j a v a 2s.c o m return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.UPDATE, modelClass, null, Integer.class, new NoKeyGenerator(), null, null); }
From source file:com.baomidou.mybatisplus.toolkit.TableInfoHelper.java
License:Apache License
public static KeyGenerator genKeyGenerator(TableInfo tableInfo, MapperBuilderAssistant builderAssistant, String baseStatementId, LanguageDriver languageDriver) { DBType dbType = GlobalConfiguration.getDbType(builderAssistant.getConfiguration()); if (dbType != DBType.ORACLE) throw new IllegalArgumentException("??Oracle?"); String id = baseStatementId + SelectKeyGenerator.SELECT_KEY_SUFFIX; Class<?> resultTypeClass = tableInfo.getKeySequence().idClazz(); Class<?> parameterTypeClass = null; StatementType statementType = StatementType.PREPARED; String keyProperty = tableInfo.getKeyProperty(); String keyColumn = tableInfo.getKeyColumn(); boolean executeBefore = true; boolean useCache = false; KeyGenerator keyGenerator = new NoKeyGenerator(); Integer fetchSize = null;//from w ww . j a va 2 s .co m Integer timeout = null; boolean flushCache = false; String parameterMap = null; String resultMap = null; ResultSetType resultSetTypeEnum = null; //??ORACLE???? String sql = "select " + tableInfo.getKeySequence().value() + ".nextval from dual"; SqlSource sqlSource = languageDriver.createSqlSource(builderAssistant.getConfiguration(), sql.trim(), null); SqlCommandType sqlCommandType = SqlCommandType.SELECT; builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, false, keyGenerator, keyProperty, keyColumn, null, languageDriver, null); id = builderAssistant.applyCurrentNamespace(id, false); MappedStatement keyStatement = builderAssistant.getConfiguration().getMappedStatement(id, false); SelectKeyGenerator answer = new SelectKeyGenerator(keyStatement, executeBefore); builderAssistant.getConfiguration().addKeyGenerator(id, answer); return answer; }
From source file:com.github.abel533.mapper.MapperTemplate.java
License:Open Source License
/** * SelectKey - ?mysqlOracle?// w ww . java 2s . c o m * * @param ms * @param column */ protected void newSelectKeyMappedStatement(MappedStatement ms, EntityHelper.EntityColumn column) { String keyId = ms.getId() + SelectKeyGenerator.SELECT_KEY_SUFFIX; if (ms.getConfiguration().hasKeyGenerator(keyId)) { return; } Class<?> entityClass = getSelectReturnType(ms); //defaults Configuration configuration = ms.getConfiguration(); KeyGenerator keyGenerator = new NoKeyGenerator(); Boolean executeBefore = getBEFORE(); String IDENTITY = (column.getGenerator() == null || column.getGenerator().equals("")) ? getIDENTITY() : column.getGenerator(); SqlSource sqlSource = new RawSqlSource(configuration, IDENTITY, entityClass); MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, keyId, sqlSource, SqlCommandType.SELECT); statementBuilder.resource(ms.getResource()); statementBuilder.fetchSize(null); statementBuilder.statementType(StatementType.STATEMENT); statementBuilder.keyGenerator(keyGenerator); statementBuilder.keyProperty(column.getProperty()); statementBuilder.keyColumn(null); statementBuilder.databaseId(null); statementBuilder.lang(configuration.getDefaultScriptingLanuageInstance()); statementBuilder.resultOrdered(false); statementBuilder.resulSets(null); statementBuilder.timeout(configuration.getDefaultStatementTimeout()); List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>(); ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(configuration, statementBuilder.id() + "-Inline", entityClass, parameterMappings); statementBuilder.parameterMap(inlineParameterMapBuilder.build()); List<ResultMap> resultMaps = new ArrayList<ResultMap>(); ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(configuration, statementBuilder.id() + "-Inline", int.class, new ArrayList<ResultMapping>(), null); resultMaps.add(inlineResultMapBuilder.build()); statementBuilder.resultMaps(resultMaps); statementBuilder.resultSetType(null); statementBuilder.flushCacheRequired(false); statementBuilder.useCache(false); statementBuilder.cache(null); MappedStatement statement = statementBuilder.build(); configuration.addMappedStatement(statement); MappedStatement keyStatement = configuration.getMappedStatement(keyId, false); configuration.addKeyGenerator(keyId, new SelectKeyGenerator(keyStatement, executeBefore)); //keyGenerator try { MetaObject msObject = forObject(ms); msObject.setValue("keyGenerator", configuration.getKeyGenerator(keyId)); } catch (Exception e) { //ignore } }
From source file:com.github.abel533.mapperhelper.MapperTemplate.java
License:Open Source License
/** * SelectKey - ?mysqlOracle?/*from w w w .j av a 2 s . c o m*/ * * @param ms * @param column */ protected void newSelectKeyMappedStatement(MappedStatement ms, EntityHelper.EntityColumn column) { String keyId = ms.getId() + SelectKeyGenerator.SELECT_KEY_SUFFIX; if (ms.getConfiguration().hasKeyGenerator(keyId)) { return; } Class<?> entityClass = getSelectReturnType(ms); //defaults Configuration configuration = ms.getConfiguration(); KeyGenerator keyGenerator = null; Boolean executeBefore = getBEFORE(); String IDENTITY = (column.getGenerator() == null || column.getGenerator().equals("")) ? getIDENTITY() : column.getGenerator(); if (IDENTITY.equalsIgnoreCase("JDBC")) { keyGenerator = new Jdbc3KeyGenerator(); } else { SqlSource sqlSource = new RawSqlSource(configuration, IDENTITY, entityClass); MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, keyId, sqlSource, SqlCommandType.SELECT); statementBuilder.resource(ms.getResource()); statementBuilder.fetchSize(null); statementBuilder.statementType(StatementType.STATEMENT); statementBuilder.keyGenerator(new NoKeyGenerator()); statementBuilder.keyProperty(column.getProperty()); statementBuilder.keyColumn(null); statementBuilder.databaseId(null); statementBuilder.lang(configuration.getDefaultScriptingLanuageInstance()); statementBuilder.resultOrdered(false); statementBuilder.resulSets(null); statementBuilder.timeout(configuration.getDefaultStatementTimeout()); List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>(); ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(configuration, statementBuilder.id() + "-Inline", entityClass, parameterMappings); statementBuilder.parameterMap(inlineParameterMapBuilder.build()); List<ResultMap> resultMaps = new ArrayList<ResultMap>(); ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(configuration, statementBuilder.id() + "-Inline", column.getJavaType(), new ArrayList<ResultMapping>(), null); resultMaps.add(inlineResultMapBuilder.build()); statementBuilder.resultMaps(resultMaps); statementBuilder.resultSetType(null); statementBuilder.flushCacheRequired(false); statementBuilder.useCache(false); statementBuilder.cache(null); MappedStatement statement = statementBuilder.build(); configuration.addMappedStatement(statement); MappedStatement keyStatement = configuration.getMappedStatement(keyId, false); keyGenerator = new SelectKeyGenerator(keyStatement, executeBefore); configuration.addKeyGenerator(keyId, keyGenerator); } //keyGenerator try { MetaObject msObject = forObject(ms); msObject.setValue("keyGenerator", keyGenerator); msObject.setValue("keyProperties", new String[] { column.getProperty() }); } catch (Exception e) { //ignore } }
From source file:com.hand.hap.mybatis.mapperhelper.MapperTemplate.java
License:Open Source License
/** * SelectKey/* w ww. j a v a2 s . c o m*/ * * @param ms * @param column */ protected void newSelectKeyMappedStatement(MappedStatement ms, EntityColumn column) { String keyId = ms.getId() + SelectKeyGenerator.SELECT_KEY_SUFFIX; if (ms.getConfiguration().hasKeyGenerator(keyId)) { return; } Class<?> entityClass = getEntityClass(ms); //defaults Configuration configuration = ms.getConfiguration(); KeyGenerator keyGenerator; Boolean executeBefore = isBEFORE(); // mod by jessen String generator = column.getGenerator() == null ? null : column.getGenerator(); String IDENTITY = ("IDENTITY".equals(generator) || StringUtil.isEmpty(generator)) ? getIDENTITY() : generator; if (IDENTITY.equalsIgnoreCase("JDBC")) { keyGenerator = new Jdbc3KeyGenerator(); } else { if ("SEQUENCE".equalsIgnoreCase(IDENTITY)) { // add by jessen, sql for selectKey IDENTITY = "SELECT " + getSeqNextVal(column) + " FROM DUAL"; } SqlSource sqlSource = new RawSqlSource(configuration, IDENTITY, entityClass); MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, keyId, sqlSource, SqlCommandType.SELECT); statementBuilder.resource(ms.getResource()); statementBuilder.fetchSize(null); statementBuilder.statementType(StatementType.STATEMENT); statementBuilder.keyGenerator(new NoKeyGenerator()); statementBuilder.keyProperty(column.getProperty()); statementBuilder.keyColumn(null); statementBuilder.databaseId(null); statementBuilder.lang(configuration.getDefaultScriptingLanuageInstance()); statementBuilder.resultOrdered(false); statementBuilder.resulSets(null); statementBuilder.timeout(configuration.getDefaultStatementTimeout()); List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>(); ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(configuration, statementBuilder.id() + "-Inline", entityClass, parameterMappings); statementBuilder.parameterMap(inlineParameterMapBuilder.build()); List<ResultMap> resultMaps = new ArrayList<ResultMap>(); ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(configuration, statementBuilder.id() + "-Inline", column.getJavaType(), new ArrayList<ResultMapping>(), null); resultMaps.add(inlineResultMapBuilder.build()); statementBuilder.resultMaps(resultMaps); statementBuilder.resultSetType(null); statementBuilder.flushCacheRequired(false); statementBuilder.useCache(false); statementBuilder.cache(null); MappedStatement statement = statementBuilder.build(); try { configuration.addMappedStatement(statement); } catch (Exception e) { //ignore } MappedStatement keyStatement = configuration.getMappedStatement(keyId, false); keyGenerator = new SelectKeyGenerator(keyStatement, executeBefore); try { configuration.addKeyGenerator(keyId, keyGenerator); } catch (Exception e) { //ignore } } //keyGenerator try { MetaObject msObject = SystemMetaObject.forObject(ms); msObject.setValue("keyGenerator", keyGenerator); msObject.setValue("keyProperties", column.getTable().getKeyProperties()); msObject.setValue("keyColumns", column.getTable().getKeyColumns()); } catch (Exception e) { //ignore } }
From source file:com.mybatisX.core.MybatisMapperAnnotationBuilder.java
License:Apache License
void parseStatement(Method method) { Class<?> parameterTypeClass = getParameterType(method); LanguageDriver languageDriver = getLanguageDriver(method); SqlSource sqlSource = getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver); if (sqlSource != null) { Options options = method.getAnnotation(Options.class); final String mappedStatementId = type.getName() + "." + method.getName(); Integer fetchSize = null; Integer timeout = null;/*from w w w. j a va 2 s . c o m*/ StatementType statementType = StatementType.PREPARED; ResultSetType resultSetType = ResultSetType.FORWARD_ONLY; SqlCommandType sqlCommandType = getSqlCommandType(method); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; boolean flushCache = !isSelect; boolean useCache = isSelect; KeyGenerator keyGenerator; String keyProperty = "id"; String keyColumn = null; if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) { // first check for SelectKey annotation - that overrides // everything else SelectKey selectKey = method.getAnnotation(SelectKey.class); if (selectKey != null) { keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method), languageDriver); keyProperty = selectKey.keyProperty(); } else if (options == null) { keyGenerator = configuration.isUseGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator(); } else { keyGenerator = options.useGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator(); keyProperty = options.keyProperty(); keyColumn = options.keyColumn(); } } else { keyGenerator = new NoKeyGenerator(); } if (options != null) { if (FlushCachePolicy.TRUE.equals(options.flushCache())) { flushCache = true; } else if (FlushCachePolicy.FALSE.equals(options.flushCache())) { flushCache = false; } useCache = options.useCache(); fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null; // issue // #348 timeout = options.timeout() > -1 ? options.timeout() : null; statementType = options.statementType(); resultSetType = options.resultSetType(); } String resultMapId = null; ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class); if (resultMapAnnotation != null) { String[] resultMaps = resultMapAnnotation.value(); StringBuilder sb = new StringBuilder(); for (String resultMap : resultMaps) { if (sb.length() > 0) { sb.append(","); } sb.append(resultMap); } resultMapId = sb.toString(); } else if (isSelect) { resultMapId = parseResultMap(method); } assistant.addMappedStatement(mappedStatementId, sqlSource, statementType, sqlCommandType, fetchSize, timeout, // ParameterMapID null, parameterTypeClass, resultMapId, getReturnType(method), resultSetType, flushCache, useCache, // TODO gcode issue #577 false, keyGenerator, keyProperty, keyColumn, // DatabaseID null, languageDriver, // ResultSets options != null ? nullOrEmpty(options.resultSets()) : null); } }