use of com.qiuyj.mybatis.PropertyColumnMapping 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 com.qiuyj.mybatis.PropertyColumnMapping 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);
}
}
}
use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class MysqlSqlBuilder method batchInsert.
@Override
public Object batchInsert(SqlInfo sqlInfo, Object args) {
List list = (List) ParameterResolver.resolveParameter(args).getParameterValues()[0];
if (list.isEmpty()) {
throw new IllegalArgumentException("Method batchInsert parameter can not be an empty list");
} else {
// 由于批量增加的sql可能会很长,所以这里给一个较大的初始化值
StringBuilder sqlBuilder = new StringBuilder(256);
sqlBuilder.append("INSERT INTO ");
sqlBuilder.append(sqlInfo.getTableName());
StringJoiner joiner = new StringJoiner(",", "(", ")");
for (String column : sqlInfo.getAllColumnsWithoutAlias()) {
joiner.add(column);
}
sqlBuilder.append(joiner.toString());
sqlBuilder.append(" VALUES ");
StringJoiner grammaJoiner = new StringJoiner(",");
StringJoiner valueJoiner = new StringJoiner(",", "(", ")");
List<ParameterMapping> singleInstanceParameterMapping = new ArrayList<>(sqlInfo.getFieldCount());
BatchInsertTypeHandler typeHandler = new BatchInsertTypeHandler(sqlInfo);
ParameterMapping canonic = new ParameterMapping.Builder(sqlInfo.getConfiguration(), "list", typeHandler).build();
for (PropertyColumnMapping propertyColumnMapping : sqlInfo.getPropertyColumnMappings()) {
valueJoiner.add(AbstractCommonSqlBuilder.PREPARE_FLAG);
singleInstanceParameterMapping.add(canonic);
}
List<ParameterMapping> allParameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
String value = valueJoiner.toString();
for (int i = 0; i < list.size(); i++) {
grammaJoiner.add(value);
allParameterMappings.addAll(singleInstanceParameterMapping);
}
sqlBuilder.append(grammaJoiner.toString());
return new StaticSqlSource(sqlInfo.getConfiguration(), sqlBuilder.toString(), allParameterMappings);
}
}
use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class BatchInsertTypeHandler method setNonNullParameter.
@SuppressWarnings("unchecked")
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) throws SQLException {
// 首先判断当前占位符是属于List集合的第几个元素
int idx = i - 1;
int instanceIdx = idx / sqlInfo.getFieldCount();
// 得到当前设置的值是实体类里面属性的第几个位置
int parameterIdx = idx % sqlInfo.getFieldCount();
// 得到当前设置的实体类
Object obj = parameter.get(instanceIdx);
// 得到对应实体类的反射对象(MetaObject)方便后面获取值
MetaObject objMeta = sqlInfo.getConfiguration().newMetaObject(obj);
PropertyColumnMapping pcm = sqlInfo.getPropertyColumnMappings().get(parameterIdx);
Object value = objMeta.getValue(pcm.getJavaClassPropertyName());
/*
* 由于构建这个框架的mysql驱动版本较高,直接使用的是java8构建的
* 所以mysql驱动原生支持java8的时间日期api的setObject方法参数
* 但是考虑到有些人的mysql驱动不会这么高,所以这里也一样
* 对于java8的时间日期的值显示调用对应的TypeHandler处理
*/
if (pcm.getTypeHandler() instanceof EnumOrdinalTypeHandler || pcm.getTypeHandler() instanceof EnumTypeHandler || isJava8DateTimeApi(value)) {
pcm.getTypeHandler().setParameter(ps, i, value, pcm.getJdbcType());
} else // 否则直接setObject即可
{
ps.setObject(i, value);
}
}
Aggregations