use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.
the class UpdateWithVersionProvider method updateByPrimaryKeyWithVersion.
public String updateByPrimaryKeyWithVersion(Object example) throws Exception {
EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
Set<ColumnMetadata> columns = entityMapper.getColumns();
SQL sql = new SQL().UPDATE(entityMapper.getTable().getName());
StringBuilder setBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
// 主键
ColumnMetadata idColumn = entityMapper.getIdColumn();
appendWhere(whereBuilder, idColumn);
Object value;
for (ColumnMetadata column : columns) {
if (column.isId() || !column.isUpdatable())
continue;
value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
if (value == null)
continue;
if (column.isVersionField()) {
appendWhere(whereBuilder, column);
// 乐观锁新值
value = getNewVersionVal(column.getJavaType(), value);
appendUpdateSet(setBuilder, column, value);
} else {
appendUpdateSet(setBuilder, column);
}
}
sql.SET(setBuilder.toString());
sql.WHERE(whereBuilder.toString());
return sql.toString();
}
use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.
the class CountByExampleProvider method countByExample.
public String countByExample(Object example) throws Exception {
EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
Set<ColumnMetadata> columns = entityMapper.getColumns();
SQL sql = new SQL().SELECT("COUNT(1)").FROM(entityMapper.getTable().getName());
Object value;
StringBuilder whereBuilder = new StringBuilder();
for (ColumnMetadata column : columns) {
value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
if (value == null)
continue;
appendWhere(whereBuilder, column);
}
if (whereBuilder.length() == 0)
throw new IllegalArgumentException("至少包含一个查询条件");
sql.WHERE(whereBuilder.toString());
return sql.toString();
}
use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.
the class SelectByExampleProvider method selectByExample.
public String selectByExample(Object example) throws Exception {
EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
Set<ColumnMetadata> columns = entityMapper.getColumns();
SQL sql = new SQL().SELECT("*").FROM(entityMapper.getTable().getName());
Object value;
StringBuilder whereBuilder = new StringBuilder();
for (ColumnMetadata column : columns) {
value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
if (value == null)
continue;
appendWhere(whereBuilder, column);
}
if (whereBuilder.length() == 0)
throw new IllegalArgumentException("至少包含一个查询条件");
//
// if(DbType.MYSQL.name().equalsIgnoreCase(MybatisConfigs.getDbType("default"))){
// whereBuilder.append(" LIMIT 20000");
// }
sql.WHERE(whereBuilder.toString());
return sql.toString();
}
use of com.mendmix.mybatis.metadata.EntityMetadata in project jeesuite-libs by vakinge.
the class AbstractMethodBuilder method build.
public void build(Configuration configuration, LanguageDriver languageDriver, MapperMetadata mapperMeta) {
for (String name : methodNames()) {
String msId = mapperMeta.getMapperClass().getName() + "." + name;
// 从参数对象里提取注解信息
EntityMetadata entityMapper = mapperMeta.getEntityMetadata();
// 生成sql
String sql = buildSQL(entityMapper, name.endsWith("Selective"));
if (scriptWrapper()) {
sql = String.format(SqlTemplate.SCRIPT_TEMAPLATE, sql);
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, mapperMeta.getEntityClass());
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, msId, sqlSource, sqlCommandType());
// 主键策略
if (sqlCommandType() == SqlCommandType.INSERT) {
KeyGenerator keyGenerator = entityMapper.autoId() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
//
statementBuilder.keyGenerator(keyGenerator).keyProperty(//
entityMapper.getIdColumn().getProperty()).keyColumn(entityMapper.getIdColumn().getColumn());
}
MappedStatement statement = statementBuilder.build();
//
setResultType(configuration, statement, mapperMeta.getEntityClass());
configuration.addMappedStatement(statement);
}
}
Aggregations