use of com.qiuyj.mybatis.BeanExampleResolver in project qiuyj-code by qiuyuanjun.
the class SqlProvider method update.
public ReturnValueWrapper update(MappedStatement ms, 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(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
}
sql.WHERE(sqlInfo.getPrimaryKey().getDatabaseColumnName() + " = ?");
parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).build());
return new ReturnValueWrapper(new StaticTextSqlNode(sql.toString()), parameterMappings);
}
}
}
use of com.qiuyj.mybatis.BeanExampleResolver in project qiuyj-code by qiuyuanjun.
the class SqlProvider method selectList.
public ReturnValueWrapper selectList(MappedStatement ms, SqlInfo sqlInfo, Object args) {
checkBeanType(sqlInfo.getBeanType(), args);
BeanExampleResolver resolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
List<PropertyColumnMapping> exampleSelectList = resolver.selectExample();
if (exampleSelectList.isEmpty()) {
throw new IllegalStateException("Please specify at least one condition");
} else {
SQL sql = new SQL().SELECT(sqlInfo.getAllColumnsWithAlias()).FROM(sqlInfo.getTableName());
List<ParameterMapping> parameterMappings = new ArrayList<>(exampleSelectList.size());
for (PropertyColumnMapping pcm : exampleSelectList) {
parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
sql.WHERE(pcm.getDatabaseColumnName() + " = ?");
}
return new ReturnValueWrapper(new StaticTextSqlNode(sql.toString()), parameterMappings);
}
}
Aggregations