r/java • u/catmewo • Aug 31 '26
LarkBatis: A build-time MyBatis compiled to plain Java
https://larkbatis.github.io/latest/My team is moving away from reflection and runtime proxies for cloud-native setups. MyBatis is heavily used in our stack, so I rewrote its core as an annotation processor: LarkBatis.
It resolves SQL, dynamic parameters (#{}), type handlers, and result mapping at compile time—generating plain Java code. At runtime, it executes direct JDBC calls with zero proxying, zero OGNL, and zero reflection.
Programming model: Standard MyBatis (mapper interfaces, XML, dynamic SQL).
Runtime: ~1,500 LOC, JDK 17+, JDBC-only dependency (GraalVM native-image works out of the box - I haven't test).
6
u/fuckedupkid_yo Aug 31 '26
This is nice, finally an alternative to jooq (not that it's bad, but i know even partial migration to it is a risk a lot of teams would not take)
I know at least one of my seniors who would absolutely love this (dude lives and breathes mybatis 😂)
6
u/repeating_bears Aug 31 '26
Nice. I've thought for years that Java relies too much on reflection. I like codegen from an annotation processor as an alternative
I do a lot of java codegen and I'd strongly recommend to ditch javapoet. I started out with that and it seems like the default choice, but I don't think it really deserves it's popularity
A template language works better because the template code you write and the output code that's generated is much more similar than the indirection of a fluent API/DSL thing. I settled on Velocity because IntelliJ's support for it is pretty good. You get syntax highlighting, method calls in templates show up when you do "find usages", etc
The main thing Velocity is missing is something to manage imports, avoid name clashes, etc. I wrote a lib to do that, wasn't difficult. I meant to open source it at some point but it would need some polish
2
u/TomKavees Aug 31 '26
The repo does not have license information, but assuming it would be actually open source, what are your thoughts on moving this under the main MyBatis project?
3
u/woj-tek Aug 31 '26
Hmm… it kinda looks like JDBI? How does it compare?
1
u/bowbahdoe Aug 31 '26
Can this find setters in a superclass? I mostly ask to know if it would be compatible with github.com/bowbahdoe/magic-bean
(It will be a bit until i get around to testing it)
1
u/kaumein Sep 02 '26
Really cool project. I’ve been working on a very similar idea with kaumei-jdbc (https://kaumei-jdbc.kaumei.io), but from the other direction.
LarkBatis starts with the MyBatis programming model and compiles it down to plain JDBC.
With kaumei-jdbc, I started from plain JDBC itself and built around compile-time code generation, type safety, statically generated conversion, no reflection, no runtime proxies, and a very small runtime.
So there’s quite a bit of overlap in the underlying idea, but the starting point and focus are different.
I especially want to take a closer look at how you handle generated keys — there are a few ideas there that look interesting for my own implementation.
Nice to see more work in this direction.
PS: kaumei-jdbc 0.2.0 just landed
1
u/TheBanditoz Sep 04 '26
This project looks interesting! I've used MyBatis extensively in a past life, but I'm curious what drove you to work on this library? Have you found significant time spent in the MyBatis runtime itself when profiling?
1
u/vips7L Aug 31 '26 edited Aug 31 '26
“Works with native image but we haven’t ran a build or tested it” OK let’s actually test the claims we (or your LLM) makes.
Compile time everything is great. For those that like ORMs Ebean has a similar feature: https://ebean.io/docs/query/query-beans
-1
u/Round_Patient_7420 Aug 31 '26
Nice!! It seems similar to Spring Data.