Giter VIP home page Giter VIP logo

bean-loader's Introduction

BeanLoader will attempt to automatically map column names to bean accessors via the four main methods:

  • first
  • each
  • collect
  • map
public static <T> T first(Class<T> type, PreparedStatement preparedStatement)
public static <T> Iterable<T> each(Class<T> type, PreparedStatement preparedStatement)
public static <T, C extends Collection<T>> C collect(Class<T> type, C collection, PreparedStatement preparedStatement)
public static <K, V extends KeyedBean<K>, M extends Map<K, V>> M map(Class<V> type, M map, PreparedStatement preparedStatement)

It will correctly handle Enums, Dates, primitives and any Object with a constructor that takes a String.
Additional TypeLoaders can be added to expand this behaviour, and the UTC DateFormat used to process Dates can be set manually (which you may have to do if you are not using MySQL).

Some examples:


PreparedStatement preparedStatement = connection.prepareStatement(“SELECT * FROM people”);

List people = BeanLoader.collect(Person.class, new ArrayList(), preparedStatement);

and:

for (Person person : BeanLoader.each(Person.class, preparedStatement))
{
    System.out.println(person.getName());
}

and maps:


public class Person implements BeanLoader.KeyedBean {
private int id;
public Integer getKey() { return this.getId(); }
public int getId() { return this.id; }
public void setId(int id) { this.id = id; }

private String name; …

}

SortedMap<Integer, Person> people = BeanLoader.map(Person.class, new TreeMap<Integer, Person>(), preparedStatement);

for legacy mappings or joins, look to SQL and AS:

public class Comment {
    private String commentBody;
    public String getCommentBody() { return this.commentBody; }
    public void setCommentBody(String commentBody) { this.commentBody = commentBody; }

    private int parentId;
    ...
}

// here's where the magic happens:
String statement = "SELECT comments.body AS commentBody, comments.parent_id AS parentId, FROM posts, comments";

...

NOTE: Due to the heavy use of Generics, this’ll require Java 1.5 or greater.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.