问题
Is there Java library like Spring JDBC Template, with same quality of code and documentation and similar data access exceptions hierarchy, but without dependancies on other Spring modules (core/beans/context modules according to http://mvnrepository.com/artifact/org.springframework/spring-jdbc/3.0.6.RELEASE)?
回答1:
Spring-jdbc has direct dependency to the following libraries: spring-core
, spring-beans
and spring-tx
. The rest of the dependencies are optional, so you don't need them actually.
If these dependencies are already a lot for you, then you might have a look at Apache DbUtils.
回答2:
A minor correction to the accepted answer. As far as I can tell there is at least one more jar required. It comes into play exactly when data access exception hierarchy is accessed (an exception is thrown by underlying database driver and it's wrapped into Spring exception). The jar is spring-asm (byte-code manipulation). I discovered it accidentally when I had a problem in my SQL query and stack trace showed something like MissingClassException. (I would to prefer just to add a comment to the answer, but it looks I'm not eligible yet).
回答3:
There is another alternative way very close to JdbcTemplate
, you can use the library sql2o which has just slf4j
and guava
dependencies. See below a simple example from their web site. Also, as a bonus you still get better performance as you can see on this benchmark (disclosure: I'm NOT a member of sql2o
project, I'm just using it in a project).
public class Task{
private int id;
private String category;
private Date dueDate;
// getters and setters here
}
Sql2o sql2o = new Sql2o(DB_URL, USER, PASS);
String sql =
"SELECT id, category, duedate " +
"FROM tasks " +
"WHERE category = :category";
try(Connection con = sql2o.open()) {
List<Task> tasks = con.createQuery(sql)
.addParameter("category", "foo")
.executeAndFetch(Task.class);
}
来源:https://stackoverflow.com/questions/8415139/spring-jdbc-template-without-spring