Skip to content

Deserialize data from JDBC ResultSet as java bean.

License

Notifications You must be signed in to change notification settings

Yeamy/SQLDataSet

Repository files navigation

SQLDataSet

English | 中文

A simple orm, deserialize data from JDBC ResultSet as java bean.

For Android SQLite also see SQLiteDataSet

 implementation 'io.github.yeamy:sqldataset:2.0'

1. Annotation

publicclassFruit{@DsColumn("Name") publicStringname; // the column in database is "Name"publicStringfullName; // the column in database can be "fullName" or "full_name"@DsIgnorepublicStringcount; // ignore this fieldpublicFruitTypetype; // the name of the column is the same as the field// regist as custom type (see DsAdapter)publicSkinskin; // this field no DsColumn treat as extra type }

2. DsReader

Generally, using DsReader is an easy and fast way.

Statementstmt = ...; // the sourceStringsql = "SELECT ..."; // the sqlFruitapple = DsReader.read(stmt, sql, Fruit.class); // read oneArrayList<Fruit> list = DsReader.eadArray(stmt, sql, Fruit.class);

3. DsFieldFactory<T>

In order to deserialize custom field type, you may define a DsFieldFactory.

DsReader.register(MyField.class, newDsFieldReader<MyField>(){@OverridepublicMyFieldread(ResultSetrs, intcolumnIndex) throwsSQLException, ReflectiveOperationException{returnnewMyType(rs.getString(columnIndex))} ); DsReader.read(stmt, Stringsql, MyType.class); // orDsReader.with(MyField.class, newDsFieldReader<MyField>(){@OverridepublicMyFieldread(ResultSetrs, intcolumnIndex) throwsSQLException, ReflectiveOperationException{returnnewMyType(rs.getString(columnIndex))} ).read(stmt, Stringsql, MyType.class);

4. DsObserver

If you want to do anything after the Bean read, you can implement DsObserver.class, and do it in onDsFinish().

publicclassVegetablesimplementsDsObserver{@DsColumn("Name") publicStringname; ... @OverridepublicvoidonDsFinish(){} }

5. Extra Field

Data come from same row of ResultSet can deserialize into an extra field.

source table:

UserNameProvinceCityName...
NikeGuangdongShantou...
...

Usually, deserialize like this:

public class User{@DsColumn("UserName") public String name; @DsColumn("Province") public String province; @DsColumn("CityName") public String city; ... } 

to package province and city into same field location, see below:

publicclassUser{@DsColumn("UserName") publicStringname; ... // NOTICE:must not declare annotation with DsColumn nor DsIgnore.publicCitylocation} publicclassCity{@DsColumn("Province") publicStringprovince; @DsColumn("CityName") publicStringcity; ... }

About

Deserialize data from JDBC ResultSet as java bean.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages