use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class AbstractSqlGeneratorEngine method analysis.
@Override
public void analysis(Class<? extends Mapper<?, ?>> actualMapperClass, Configuration configuration) {
if (Objects.isNull(sqlInfos)) {
sqlInfos = new HashMap<>();
}
if (!sqlInfos.containsKey(actualMapperClass)) {
synchronized (sqlInfoWriteLock) {
if (!sqlInfos.containsKey(actualMapperClass)) {
/*
* 这里生成SqlInfo会非常的耗时
* 所以这里一定要将结果缓存起来
*/
SqlInfo mapperSqlInfo = new SqlInfo(actualMapperClass, chain, configuration);
sqlInfos.put(actualMapperClass, mapperSqlInfo);
}
}
}
/*
* 如果当前解析的mapperClass所对应的实体类有枚举类型
* 那么就解析所有属性生成对应的ResultMap
*/
SqlInfo currentSqlInfo = getSqlInfo(actualMapperClass);
// 这里判断下,sqlinfo里面的Configuration是否为null,如果为null,那么需要设置
if (Objects.isNull(currentSqlInfo.getConfiguration())) {
currentSqlInfo.setConfiguration(configuration);
// 此时需要设置sqlInfo里面的
if (Objects.isNull(currentSqlInfo.getPrimaryKey().getTypeHandler())) {
currentSqlInfo.getPrimaryKey().setTypeHandler(configuration.getTypeHandlerRegistry().getTypeHandler(currentSqlInfo.getPrimaryKey().getTargetClass()));
}
for (PropertyColumnMapping pcm : currentSqlInfo.getPropertyColumnMappings()) {
if (Objects.isNull(pcm.getTypeHandler())) {
pcm.setTypeHandler(configuration.getTypeHandlerRegistry().getTypeHandler(pcm.getTargetClass()));
}
}
}
if (currentSqlInfo.hasEnumField() && !resultMaps.containsKey(actualMapperClass)) {
synchronized (resultMapWriteLock) {
if (!resultMaps.containsKey(actualMapperClass)) {
initResultMap(currentSqlInfo, actualMapperClass);
}
}
}
}
use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class AbstractSqlGeneratorEngine method initResultMap.
private void initResultMap(SqlInfo sqlInfo, Class<? extends Mapper<?, ?>> mapperClass) {
List<ResultMapping> resultMappings = new ArrayList<>(sqlInfo.getFieldCount());
for (PropertyColumnMapping pcm : sqlInfo.getWithoutPrimaryKey()) {
resultMappings.add(new ResultMapping.Builder(sqlInfo.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getJavaClassPropertyName(), pcm.getTypeHandler()).build());
}
resultMappings.add(0, new ResultMapping.Builder(sqlInfo.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).flags(Collections.singletonList(ResultFlag.ID)).build());
resultMaps.put(mapperClass, new ResultMap.Builder(sqlInfo.getConfiguration(), "mapper-" + mapperClass.getName(), sqlInfo.getBeanType(), resultMappings).build());
}
use of com.qiuyj.mybatis.PropertyColumnMapping 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.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class SqlProvider method insert.
public ReturnValueWrapper insert(MappedStatement ms, SqlInfo sqlInfo) {
// 不建议以下这种方式创建SQL,这样会增加编译之后的字节码文件数量
// 因为这样会创建一个匿名内部类
/*SQL sql = new SQL() {
{
INSERT_INTO(sqlInfo.getTableName());
INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias());
String[] prepareColumnValues = new String[sqlInfo.getFiledCount()];
Arrays.fill(prepareColumnValues, PREPARE_FLAG);
INTO_VALUES(prepareColumnValues);
}
};*/
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 = ms.getConfiguration().getTypeHandlerRegistry();
ParameterMapping.Builder parameterBuilder;
for (PropertyColumnMapping mapping : sqlInfo.getPropertyColumnMappings()) {
parameterBuilder = new ParameterMapping.Builder(ms.getConfiguration(), mapping.getJavaClassPropertyName(), mapping.getTypeHandler());
parameterMappings.add(parameterBuilder.build());
}
return new ReturnValueWrapper(new StaticTextSqlNode(sql), parameterMappings);
}
use of com.qiuyj.mybatis.PropertyColumnMapping 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