All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cool.scx.jdbc.bean_builder.NormalBeanBuilder Maven / Gradle / Ivy

There is a newer version: 2.7.4
Show newest version
package cool.scx.jdbc.bean_builder;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.function.Function;

import static cool.scx.util.reflect.ConstructorUtils.findNoArgsConstructor;

/**
 * 

BeanBuilder interface.

* * @author scx567888 * @version 0.2.1 */ final class NormalBeanBuilder extends BeanBuilder { private final Constructor constructor; private final FieldSetter[] fieldSetters; public NormalBeanBuilder(Class type, Function columnNameMapping) { this.constructor = findNoArgsConstructor(type); this.constructor.setAccessible(true); this.fieldSetters = FieldSetter.ofArray(type, columnNameMapping); } public NormalBeanBuilder(Class type) { this(type, Field::getName); } @Override public T createBean(ResultSet rs, int[] indexInfo) throws SQLException { T t = newInstance(); for (int i = 0; i < fieldSetters.length; i = i + 1) { if (indexInfo[i] != -1) {// -1 需要跳过 var o = fieldSetters[i].typeHandler().getObject(rs, indexInfo[i]); if (o != null) {// 为空我们就跳过了 try { fieldSetters[i].javaField().set(t, o); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } return t; } @Override public FieldSetter[] fieldSetters() { return fieldSetters; } private T newInstance() { try { return this.constructor.newInstance(); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy