use of org.apache.ibatis.jdbc.SQL in project mybatis-3 by mybatis.
the class OurSqlBuilder method buildSelectByIdAndNameMultipleParamAndProviderContext.
public String buildSelectByIdAndNameMultipleParamAndProviderContext(final Integer id, final String name, ProviderContext context) {
final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
return new SQL() {
{
SELECT("*");
FROM(tableName);
if (id != null) {
WHERE("id = #{param1}");
}
if (name != null) {
WHERE("name like #{param2} || '%'");
}
if (!containsLogicalDelete) {
WHERE("logical_delete = false");
}
}
}.toString();
}
use of org.apache.ibatis.jdbc.SQL in project mybatis-3 by mybatis.
the class OurSqlBuilder method buildSelectByIdProviderContextOnly.
public String buildSelectByIdProviderContextOnly(ProviderContext context) {
final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
return new SQL() {
{
SELECT("*");
FROM(tableName);
WHERE("id = #{id}");
if (!containsLogicalDelete) {
WHERE("logical_delete = ${Constants.LOGICAL_DELETE_OFF}");
}
}
}.toString();
}
use of org.apache.ibatis.jdbc.SQL in project mybatis-3 by mybatis.
the class OurSqlBuilder method buildGetUsersByNameWithParamNameQueryUsingMap.
public String buildGetUsersByNameWithParamNameQueryUsingMap(Map<String, Object> params) {
final String name = String.class.cast(params.get("name"));
final String orderByColumn = String.class.cast(params.get("orderByColumn"));
return new SQL() {
{
SELECT("*");
FROM("users");
if (name != null) {
WHERE("name like #{param1} || '%'");
}
ORDER_BY(orderByColumn);
}
}.toString();
}
use of org.apache.ibatis.jdbc.SQL in project qiuyj-code by qiuyuanjun.
the class AbstractCommonSqlBuilder method insert.
@Override
public Object insert(SqlInfo sqlInfo, Object args) {
String[] prepareColumnValues = new String[sqlInfo.getFieldCount()];
Arrays.fill(prepareColumnValues, PREPARE_FLAG);
String sql = new SQL().INSERT_INTO(sqlInfo.getTableName()).INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias()).INTO_VALUES(prepareColumnValues).toString();
List<ParameterMapping> parameterMappings = new ArrayList<>(sqlInfo.getFieldCount());
TypeHandlerRegistry reg = sqlInfo.getConfiguration().getTypeHandlerRegistry();
ParameterMapping.Builder parameterBuilder;
for (PropertyColumnMapping mapping : sqlInfo.getPropertyColumnMappings()) {
parameterBuilder = new ParameterMapping.Builder(sqlInfo.getConfiguration(), mapping.getJavaClassPropertyName(), mapping.getTypeHandler());
parameterMappings.add(parameterBuilder.build());
}
return new StaticSqlSource(sqlInfo.getConfiguration(), sql, parameterMappings);
}
use of org.apache.ibatis.jdbc.SQL in project qiuyj-code by qiuyuanjun.
the class AbstractCommonSqlBuilder method update.
@Override
public Object update(SqlInfo sqlInfo, Object args) {
checkPrimaryKey(sqlInfo);
checkBeanType(sqlInfo.getBeanType(), args);
BeanExampleResolver exampleResolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
if (!exampleResolver.hasPrimaryKeyAndNotDefault()) {
throw new NoPrimaryKeyException("Primary key is default value.");
} else {
List<PropertyColumnMapping> nonNullColumns = exampleResolver.getWithoutPrimaryKey();
if (nonNullColumns.isEmpty()) {
throw new IllegalStateException("Please update at least one column");
} else {
List<ParameterMapping> parameterMappings = new ArrayList<>(nonNullColumns.size() + 1);
SQL sql = new SQL();
sql.UPDATE(sqlInfo.getTableName());
for (PropertyColumnMapping pcm : nonNullColumns) {
sql.SET(pcm.getDatabaseColumnName() + " = ?");
parameterMappings.add(new ParameterMapping.Builder(sqlInfo.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
}
sql.WHERE(sqlInfo.getPrimaryKey().getDatabaseColumnName() + " = ?");
parameterMappings.add(new ParameterMapping.Builder(sqlInfo.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).build());
return new StaticSqlSource(sqlInfo.getConfiguration(), sql.toString(), parameterMappings);
}
}
}
Aggregations