Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve to share Jdbc3KeyGenerator and NoKeyGenerator instance #882

Closed
kazuki43zoo opened this issue Jan 3, 2017 · 0 comments
Closed

Improve to share Jdbc3KeyGenerator and NoKeyGenerator instance #882

kazuki43zoo opened this issue Jan 3, 2017 · 0 comments
Assignees
Labels
enhancement Improve a feature or add a new feature
Milestone

Comments

@kazuki43zoo
Copy link
Member

The Jdbc3KeyGenerator and NoKeyGenerator can be share an instance because it hasn't state.

I suggest to share an instance as follow:

e.g) Jdbc3KeyGenerator

public class Jdbc3KeyGenerator implements KeyGenerator {
  public static final Jdbc3KeyGenerator INSTANCE = new Jdbc3KeyGenerator(); // Add constant
  // ...
}

e.g) MapperAnnotationBuilder

if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
  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 (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
  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() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
  } else {
    keyGenerator = options.useGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
    keyProperty = options.keyProperty();
    keyColumn = options.keyColumn();
  }
} else {
  keyGenerator = NoKeyGenerator.INSTANCE;
}
@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Jan 3, 2017
@kazuki43zoo kazuki43zoo self-assigned this Jan 3, 2017
@kazuki43zoo kazuki43zoo added this to the 3.4.3 milestone Jan 3, 2017
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

No branches or pull requests

1 participant