What would be the closest equivalent in Java to a Micro ORM such as Dapper, PetaPoco, Massive or CodingHorror?
I recommend Spring JDBC templates. While it's not a "true" ORM, it's a pleasure to use where Hibernate seems to be an overkill.
sql2o seems like a Dapper alternative - thin wrapper around JDBC
String sql =
"SELECT id, category, duedate " +
"FROM tasks " +
"WHERE category = :category";
Sql2o sql2o = new Sql2o(DB_URL, USER, PASS);
List<Task> tasks = sql2o.createQuery(sql)
.addParameter("category", "foo")
.executeAndFetch(Task.class);
github - https://github.com/aaberg/sql2o
site - http://www.sql2o.org/
Here's a list of tools that "ease the pain" when interacting with simple JDBC:
- Spring's JdbcTemplate
- Apache DbUtils
- JDBI
- sql2o
- persism
And here's a list of tools that go a bit beyond simple JDBC, i.e. provide some ORM / ActiveRecord facilities
- jOOQ (This one probably doesn't qualify as micro-ORM)
- JaQu
- ActiveJDBC (This one is more of an ActiveRecord API, than an ORM)
- MyBatis (This one focuses on SQL templating, but also has some mapping features)
- EBean
Another interesting light ORM is JDBI. Here is Five minute intro
It has two alternative APIs:
Fluent API
DBI dbi = new DBI(ds);
Handle h = dbi.open();
String name = h.createQuery("select name from something where id = :id")
.bind("id", 1)
.map(StringMapper.FIRST)
.first();
and SQL Object API where SQL statements are mapped to methods with declarative interfaces like this:
public interface MyDAO
{
@SqlUpdate("create table something (id int primary key, name varchar(100))")
void createSomethingTable();
}
DBI dbi = new DBI(ds);
MyDAO dao = dbi.open(MyDAO.class);
dao.createSomethingTable();
Also checkout SimpleFlatMapper
It's a performant simple ResultSet to Object mapper. It just plug on top of jdbc and gives far better performance than Hibernate Ibatis or even sql2o. It easily integrate JdbcTemplate and provides constructor, setter and field injection.
This one doesn't seem to be mentioned here yet: dalesbred
Similar to sql2o and dapper...
来源:https://stackoverflow.com/questions/6494938/java-micro-orm-equivalent