club.chlab.mybatis.provider.InsertEntityProvider Maven / Gradle / Ivy
The newest version!
package club.chlab.mybatis.provider;
import java.lang.reflect.Field;
import org.apache.ibatis.jdbc.SQL;
import club.chlab.mybatis.annotations.Column;
/**
* mybatis insert entity provider
*
* @author jch
*
*/
public class InsertEntityProvider extends BaseProvider {
/**
* insert entity
*
* @author jch
* @param entity
* @return
*/
public String insert(Object entity) {
Class> cls = entity.getClass();
SQL sql = new SQL();
sql.INSERT_INTO(getTableName(entity));
Field[] fields = cls.getDeclaredFields();
if (null != entity) {
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
field.setAccessible(true);
try {
Column annotation = field.getAnnotation(Column.class);
String col = getColName(field);
if (null != annotation && annotation.primary() && !isEmpty(annotation.primarysql())) {
sql.VALUES(col, annotation.primarysql());
} else {
sql.VALUES(col, "#{"+field.getName()+"}");
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
return sql.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy