Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.mybatis.mapper.example.ExampleProvider Maven / Gradle / Ivy
package io.mybatis.mapper.example;
import io.mybatis.provider.EntityTable;
import io.mybatis.provider.SqlScript;
import org.apache.ibatis.builder.annotation.ProviderContext;
import java.util.stream.Collectors;
public class ExampleProvider {
private static final String EXAMPLE_SET_CLAUSE_INNER_WHEN =
" " +
" \n" +
" \n" +
" \n" +
" ${setValue.condition},\n" +
" \n" +
" \n" +
" ${setValue.condition} = ${setValue.variables('setValue.value')},\n" +
" \n" +
" \n" +
" \n" +
" " ;
private static final String EXAMPLE_WHERE_CLAUSE_INNER_WHEN =
" \n" +
" AND ${criterion.condition}\n" +
" \n" +
" \n" +
" AND ${criterion.condition} ${criterion.variables('criterion.value')}\n" +
" \n" +
" \n" +
" AND ${criterion.condition} ${criterion.variables('criterion.value')} AND\n" +
" ${criterion.variables('criterion.secondValue')}\n" +
" \n" +
" \n" +
" AND ${criterion.condition}\n" +
" \n" +
" ${criterion.variables('listItem')}\n" +
" \n" +
" \n" ;
public static final String EXAMPLE_WHERE_CLAUSE = " \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
EXAMPLE_WHERE_CLAUSE_INNER_WHEN +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
EXAMPLE_WHERE_CLAUSE_INNER_WHEN +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" ;
public static final String UPDATE_BY_EXAMPLE_WHERE_CLAUSE = " \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
EXAMPLE_WHERE_CLAUSE_INNER_WHEN +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
EXAMPLE_WHERE_CLAUSE_INNER_WHEN +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" ;
public static String deleteByExample (ProviderContext providerContext) {
return SqlScript.caching(providerContext, (entity, util) ->
util.ifTest("startSql != null and startSql != ''" , () -> "${startSql}" )
+ "DELETE FROM " + entity.tableName()
+ util.parameterNotNull("Example cannot be null" )
+ (entity.getPropBoolean("deleteByExample.allowEmpty" , true ) ?
"" : util.variableIsFalse("_parameter.isEmpty()" , "Example Criteria cannot be empty" ))
+ EXAMPLE_WHERE_CLAUSE
+ util.ifTest("endSql != null and endSql != ''" , () -> "${endSql}" ));
}
public static String updateByExample (ProviderContext providerContext) {
return SqlScript.caching(providerContext, new SqlScript() {
@Override
public String getSql (EntityTable entity) {
return ifTest("example.startSql != null and example.startSql != ''" , () -> "${example.startSql}" )
+ "UPDATE " + entity.tableName()
+ set(() -> entity.updateColumns().stream().map(
column -> column.columnEqualsProperty("entity." )).collect(Collectors.joining("," )))
+ variableNotNull("example" , "Example cannot be null" )
+ (entity.getPropBoolean("updateByExample.allowEmpty" , true ) ?
"" : variableIsFalse("example.isEmpty()" , "Example Criteria cannot be empty" ))
+ UPDATE_BY_EXAMPLE_WHERE_CLAUSE
+ ifTest("example.endSql != null and example.endSql != ''" , () -> "${example.endSql}" );
}
});
}
public static String updateByExampleSetValues (ProviderContext providerContext) {
return SqlScript.caching(providerContext, new SqlScript() {
@Override
public String getSql (EntityTable entity) {
return ifTest("example.startSql != null and example.startSql != ''" , () -> "${example.startSql}" )
+ variableNotEmpty("example.setValues" , "Example setValues cannot be empty" )
+ "UPDATE " + entity.tableName()
+ EXAMPLE_SET_CLAUSE_INNER_WHEN
+ variableNotNull("example" , "Example cannot be null" )
+ (entity.getPropBoolean("updateByExample.allowEmpty" , true ) ?
"" : variableIsFalse("example.isEmpty()" , "Example Criteria cannot be empty" ))
+ UPDATE_BY_EXAMPLE_WHERE_CLAUSE
+ ifTest("example.endSql != null and example.endSql != ''" , () -> "${example.endSql}" );
}
});
}
public static String updateByExampleSelective (ProviderContext providerContext) {
return SqlScript.caching(providerContext, new SqlScript() {
@Override
public String getSql (EntityTable entity) {
return ifTest("example.startSql != null and example.startSql != ''" , () -> "${example.startSql}" )
+ "UPDATE " + entity.tableName()
+ set(() -> entity.updateColumns().stream().map(
column -> ifTest(column.notNullTest("entity." ),
() -> column.columnEqualsProperty("entity." ) + "," )).collect(Collectors.joining(LF)))
+ variableNotNull("example" , "Example cannot be null" )
+ (entity.getPropBoolean("updateByExampleSelective.allowEmpty" , true ) ?
"" : variableIsFalse("example.isEmpty()" , "Example Criteria cannot be empty" ))
+ UPDATE_BY_EXAMPLE_WHERE_CLAUSE
+ ifTest("example.endSql != null and example.endSql != ''" , () -> "${example.endSql}" );
}
});
}
public static String selectByExample (ProviderContext providerContext) {
return SqlScript.caching(providerContext, new SqlScript() {
@Override
public String getSql (EntityTable entity) {
return ifTest("startSql != null and startSql != ''" , () -> "${startSql}" )
+ "SELECT "
+ ifTest("distinct" , () -> "distinct " )
+ ifTest("selectColumns != null and selectColumns != ''" , () -> "${selectColumns}" )
+ ifTest("selectColumns == null or selectColumns == ''" , entity::baseColumnAsPropertyList)
+ " FROM " + entity.tableName()
+ ifParameterNotNull(() -> EXAMPLE_WHERE_CLAUSE)
+ ifTest("orderByClause != null" , () -> " ORDER BY ${orderByClause}" )
+ ifTest("orderByClause == null" , () -> entity.orderByColumn().orElse("" ))
+ ifTest("endSql != null and endSql != ''" , () -> "${endSql}" );
}
});
}
public static String countByExample (ProviderContext providerContext) {
return SqlScript.caching(providerContext, new SqlScript() {
@Override
public String getSql (EntityTable entity) {
return ifTest("startSql != null and startSql != ''" , () -> "${startSql}" )
+ "SELECT COUNT("
+ ifTest("distinct" , () -> "distinct " )
+ ifTest("simpleSelectColumns != null and simpleSelectColumns != ''" , () -> "${simpleSelectColumns}" )
+ ifTest("simpleSelectColumns == null or simpleSelectColumns == ''" , () -> "*" )
+ ") FROM "
+ entity.tableName()
+ ifParameterNotNull(() -> EXAMPLE_WHERE_CLAUSE)
+ ifTest("endSql != null and endSql != ''" , () -> "${endSql}" );
}
});
}
}