-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Allow omit the 'method' attribute when provider method name is same with mapper method #1279
Comments
I think this is useful. @harawata WDYT? |
@kazuki43zoo @harawata this is a good request but I think we can make it even better. |
Can this be achieved with the enhancement #1055 ? @SelectProvider(type = SqlProvider.class, method = "universalSelect")
List<AirSensor> listAirSensorsByDuidAndTimeRange(); Then in class SqlProvider {
String universalSelect(ProviderContext context) {
// look for a method with the same name, etc.
}
String listAirSensorsByDuidAndTimeRange() {
return "select * from ...";
}
} I'm not strongly opposed to the change, but there seems to be some overlap in these features. |
Thank you for your reply! @harawata Mapper's methods with and without params @SelectProvider(type = AirSensorSqlProvider.class, method = "universalSelect")
List<AirSensor> listAirSensorsByDuidAndTimeRange(@Param("duid") Long duid,
@Param("timeStart") Date timeStart, @Param("timeEnd") Date timeEnd);
@SelectProvider(type = AirSensorSqlProvider.class, method = "universalSelectNoParam")
List<AirSensor> listAllAirSensors(); Methods in Sqlprovider public String universalSelectNoParam(ProviderContext providerContext) {
String sql = "";
try {
Method mapperMethod = providerContext.getMapperMethod();
Method providerMethod = this.getClass().getMethod(mapperMethod.getName());
sql = (String) providerMethod.invoke(this);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
return sql;
}
public String universalSelect(ProviderContext providerContext, Map<String, Object> map) {
String sql = "";
String keyPrefix = "param";
Method mapperMethod = providerContext.getMapperMethod();
Object[] params = new Object[mapperMethod.getParameterCount()];
for (Map.Entry entry : map.entrySet()) {
String key = (String) entry.getKey();
// detect "param1", "param2" and so on, place them in right order
if (key.startsWith(keyPrefix)) {
params[Integer.parseInt(key.substring(keyPrefix.length())) - 1] = entry.getValue();
}
}
try {
Method providerMethod = this.getClass().getMethod(mapperMethod.getName(), mapperMethod.getParameterTypes());
// invoke method with parameters.
sql = (String) providerMethod.invoke(this, params);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
return sql;
} Those two |
Thank you, @picc-lu . You made a good point! Then, how about introducing a new interface like this? public interface SqlProviderMethodResolver {
Method resolve(ProviderContext context);
} If the class specified in This way, you may have to write the name matching rule by yourself, but it allows other users to write their own rules. @h3adache @kazuki43zoo |
@harawata I think it's good 👍 public interface SqlProviderMethodResolver {
default Method resolve(ProviderContext context) {
// providing default implementation?
}
} WDYT? |
I think it's a potentially good change but what I meant was similar to #1055 but having a simple "provide" (or "provideSql"?) as the default method name instead of doing any lookup for name based on classname or anything more complicated. Too simple? Am I missing anything? |
@h3adache , |
Related with mybatisgh-1279
…equence or its subclass See mybatisgh-1279
Allow omit a 'method' attribute on SqlProvider annotation
Related with mybatisgh-1279
…equence or its subclass See mybatisgh-1279
Allow omit a 'method' attribute on SqlProvider annotation
Example
@SelectProvider(type = SqlProvider.class
, method = "listAirSensorsByDuidAndTimeRange")List<AirSensor> listAirSensorsByDuidAndTimeRange();
class SqlProvider {
public static String listAirSensorsByDuidAndTimeRange() {
// return SQL;
}
}
When method name of mapper's and sqlprovider's is the same (in bold), method attribute can be ignored for easy development.
Typing that every time is pretty annoying.
#1283
Thank you!
The text was updated successfully, but these errors were encountered: