use of com.qiuyj.mybatis.SqlInfo in project qiuyj-code by qiuyuanjun.
the class AbstractSqlGeneratorEngine method addSqlInfos.
public void addSqlInfos(Set<Class<? extends Mapper<?, ?>>> mapperClassSet) {
if (Objects.nonNull(mapperClassSet) && mapperClassSet.size() > 0) {
if (Objects.isNull(sqlInfos)) {
sqlInfos = new HashMap<>(mapperClassSet.size());
}
for (Class<? extends Mapper<?, ?>> mapperClass : mapperClassSet) {
// 由于此时还无法获取到Mybatis的Configuration对象,所以这里暂时设置null
// 后面运行阶段,一定要重新设置configuraiton为null的sqlInfo
SqlInfo currSqlInfo = new SqlInfo(mapperClass, chain, null);
addSqlInfo(mapperClass, currSqlInfo);
// 这里暂时无法获取resultMap,由于configuration为null
// 所以获取对应的resultMap将放在运行的时候动态获取
// if (currSqlInfo.hasEnumField()) {
// initResultMap(currSqlInfo, mapperClass);
// }
}
}
}
use of com.qiuyj.mybatis.SqlInfo in project qiuyj-code by qiuyuanjun.
the class AbstractSqlGeneratorEngine method addSqlInfos.
@SuppressWarnings("unchecked")
public void addSqlInfos(Set<Class<?>> mapperClassSet) {
if (Objects.nonNull(mapperClassSet) && mapperClassSet.size() > 0) {
if (Objects.isNull(sqlInfos)) {
sqlInfos = new HashMap<>(mapperClassSet.size());
}
for (Class<?> mapperClass : mapperClassSet) {
// 由于此时还无法获取到Mybatis的Configuration对象,所以这里暂时设置null
// 后面运行阶段,一定要重新设置configuraiton为null的sqlInfo
SqlInfo currSqlInfo = new SqlInfo((Class<? extends Mapper<?, ?>>) mapperClass, chain, null);
addSqlInfo((Class<? extends Mapper<?, ?>>) mapperClass, currSqlInfo);
// 这里暂时无法获取resultMap,由于configuration为null
// 所以获取对应的resultMap将放在运行的时候动态获取
// if (currSqlInfo.hasEnumField()) {
// initResultMap(currSqlInfo, mapperClass);
// }
}
}
}
use of com.qiuyj.mybatis.SqlInfo in project qiuyj-code by qiuyuanjun.
the class AbstractSqlGeneratorEngine method generateSql.
@Override
public void generateSql(MappedStatement ms, Class<? extends Mapper<?, ?>> mapperClass, Method mapperMethod, Object args) {
// 得到当前实体类的映射数据库的信息
SqlInfo sqlInfo = getSqlInfo(mapperClass);
// 得到返回值
Object returnValue = getReturnValue(sqlInfo, ms, mapperMethod, args);
if (Objects.nonNull(returnValue)) {
// 根据返回值得到对应的SqlSource
SqlSource sqlSource = generateSqlSource(sqlInfo, returnValue, args);
MetaObject msMetaObject = ms.getConfiguration().newMetaObject(ms);
// 重新设置sqlSource,这里全部使用MapperSqlSource,方便后面判断
msMetaObject.setValue("sqlSource", new MapperSqlSource(sqlSource));
// 如果类型是Select的,那么还需要设置resultMap
if (sqlInfo.hasEnumField() && ms.getSqlCommandType() == SqlCommandType.SELECT) {
msMetaObject.setValue("resultMaps", getResultMap(mapperClass));
} else // 当然,前提是当前的主键标注了@Sequence注解
if (!ms.getConfiguration().isUseGeneratedKeys() && Objects.nonNull(sqlInfo.getSequenceName()) && ms.getSqlCommandType() == SqlCommandType.INSERT) {
generateSequenceKey(ms, msMetaObject, sqlInfo);
}
}
}
use of com.qiuyj.mybatis.SqlInfo 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里面的属性的typehandler
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);
}
}
}
}
Aggregations