Writing SQL is a lot of work, not type safe (in the programming language static typing sense), often not re-usable, and hard to test. There are libraries that solve these parts for you (or at least help with them). For me the optimum is libraries that do only these parts but actively try to NOT introduce any new layer of abstraction over the database model itself. The problem is that in this whole ORM discussion, the…
I’ve had success in Java with Freemarker. It’s a templating engine so you’re just producing strings. But you can add typing; use variables, loops, and conditionals; and include other templates for sub query reuse. IntelliJ has a plug-in. It’s a nice compromise between crafting strings vs SQL DSL. As for the objects, you can get very far with everything being a Map until you really need to add a class or two. :)
class Foo {
Long id
String name
...
}
Foo foo = new Foo(db.firstRow("select * from foo limit 1"))
And it all just works if the database columns match the fields of Foo. And if you just do Foo foo = new Foo(db.firstRow("select name from foo limit 1"))
Then you get a `foo` with only the name populated, etc, and you got type safety, easy direct, efficient SQL queries and the ability to test your code without hitting the database, all without imposing any "leaky" abstractions that cause all the problems.