|
1 | 1 | # Java repository layer frameworks comparison |
2 | 2 |
|
3 | | -Comparison of usage of non-JPA SQL mapping (persistence) frameworks for Java (Jooq, Spring JDBCTemplate etc.). |
| 3 | +Comparison of usage of non-JPA SQL mapping (persistence) frameworks for Java (Jooq, Spring JDBCTemplate, MyBatis etc.). |
4 | 4 |
|
5 | | -We are not comparing performance (that'll be maybe added later), but rather how are these frameworks used for everyday tasks. |
| 5 | +We are not comparing performance, but rather how are these frameworks used for everyday tasks. |
6 | 6 |
|
7 | | -We prepared some common scenarios, which you typically need to implement data-centric application, and then we implemented these scenarios using various non-JPA DB layer frameworks. |
| 7 | +We prepared some common scenarios, which you typically need to implement data-centric application, and then we implemented these scenarios using various non-JPA DB layer frameworks. This project should serve |
| 8 | +- as point of reference when deciding for SQL mapping framework |
| 9 | +- as a template of common framework usage scenarios (see scenarios below) |
| 10 | +- to document best practices of such common usages (**comments are welcomed!**) |
| 11 | + |
| 12 | +**Use code in the repository as you like (MIT License)** |
8 | 13 |
|
9 | 14 | ## Frameworks compared |
10 | 15 |
|
11 | 16 | * Spring JDBCTemplate (see [implementation](src/main/java/com/clevergang/dbtests/repository/impl/jdbctemplate/JDBCDataRepositoryImpl.java)) |
12 | 17 | * jOOQ (see [implementation](src/main/java/com/clevergang/dbtests/repository/impl/jooq/JooqDataRepositoryImpl.java)) |
| 18 | +* MyBatis (see [implementation](src/main/java/com/clevergang/dbtests/repository/impl/mybatis/MyBatisDataRepositoryImpl.java) and [mapper](src/main/resources/mybatis/mappers/DataRepositoryMapper.xml)) |
13 | 19 |
|
14 | | -We tried to find optimal (== most readable) implementation in every framework, but comments are welcomed! There are lot of comments explaining why we chose to such implementation and some FIXMEs on places which we do not like, but which cannot be implemented differently or which we do not know how to improve... |
| 20 | +We tried to find optimal (== most readable) implementation in every framework, but comments are welcomed! There are lot of comments explaining why we chose to such implementation and some FIXMEs on places which we do not like, but which cannot be implemented differently or which we have troubles to improve... |
15 | 21 |
|
16 | 22 | ## Scenarios implemented |
17 | 23 |
|
@@ -49,11 +55,47 @@ Well, we were trying to "stick with standard" in our projects so we used JPA in |
49 | 55 |
|
50 | 56 | So we dropped JPA completely, started using JDBCTemplate and discovered that we can deliver apps sooner (which was kind of surprising), they are a lot faster (thanks to effective use of DB) and much more robust... This was really relaxing and we do not plan to return to JPA at all... (yes, even for CRUD applications!) |
51 | 57 |
|
52 | | -This project aims to explore other options in the SQL mapping area than just JDBCTemplate. It should serve us |
53 | | - |
54 | | -- as point of reference when deciding for SQL mapping framework |
55 | | -- as a template of common DB usage scenarios |
56 | | -- to document best practices of such common usages (**comments are welcomed!**) |
57 | | - |
58 | | - |
59 | | -**Use code in the repository as you like (MIT License)** |
| 58 | +This project aims to explore other options in the SQL mapping area than just JDBCTemplate. |
| 59 | + |
| 60 | +## Conclusions/Notes |
| 61 | + |
| 62 | +I was able to implement all of the scenarios with all of the tested frameworks - |
| 63 | +with only difference in how comfortable or inconvenient it was. Please note that following remarks |
| 64 | +are very subjective and does not have to necessarily apply to you. |
| 65 | + |
| 66 | +#### What would I choose |
| 67 | + |
| 68 | +1. If project manager is ok with additional cost of a licence or the project uses one of open source databases (like PostgreSQL) then definitely go with **jOOQ**. |
| 69 | +2. If project uses Oracle, DB2, MSSQL or any other commercial database and additional cost for jOOQ licence is not acceptable, then go with **JDBCTemplate** |
| 70 | + |
| 71 | +#### Subjective pros/cons of each framework |
| 72 | + |
| 73 | +**JDBC Template** |
| 74 | +* Pros |
| 75 | + * Feels like you are very close to JDBC itself |
| 76 | + * Implemented all of the scenarios without bigger issues - there were no hidden surprises |
| 77 | + * Very easy batch operations |
| 78 | + * Easy setup |
| 79 | +* Cons |
| 80 | + * Methods in JDBCDataRepositoryImpl are not much readable - that's because you have to inline SQL in Java code. It would have been better if Java supported multiline strings. |
| 81 | + * Debug logging could be better |
| 82 | + |
| 83 | +**jOOQ** |
| 84 | +* Pros |
| 85 | + * Very fluent, very easy to write new queries, code is very readable |
| 86 | + * Once setup it's very easy to use, excellent for simple queries |
| 87 | + * Awesome logger debug output |
| 88 | +* Cons |
| 89 | + * Paid licence for certain databases - it'll be difficult to persuade managers that it's worth it :) |
| 90 | + * Not so much usable for big queries - it's better to use native SQL (see scenario 9.) |
| 91 | + * Weird syntax for performing batch operations (in case that you do not use UpdatableRecord). But it's not a big deal... |
| 92 | + |
| 93 | + |
| 94 | +**MyBatis** |
| 95 | +* Pros |
| 96 | + * Writing SQL statements in XML mapper file feels good - it's easy to work with parameters. |
| 97 | +* Cons |
| 98 | + * quite a lot of files for single DAO imlementation (MyBatisDataRepositoryImpl, DataRepositoryMapper and DataRepositoryMapper.xml), though navigation is not such a big deal |
| 99 | + * at version 3.4.0 unable to work with Java8 DateTime types (LocalDate etc.), support possible through 3rd party library (mybatis-types), see build.gradle and <typeHandlers> configuration in mybatis-config.xml |
| 100 | + * can't run batch and non-batch operations in single SqlSession, but have to create completely new SqlSession instead (see configuration in DbTestsApplication class). Surprisingly, this does not necessarily mean that the batch and non-batch operations will be executed in different transactions (as we would expect), so at the end this is not a total drawback, but just inconvenience |
| 101 | + * expected that localCacheScope=STATEMENT is default MyBatis behavior, which is not... I know this is questionable drawback, but it was kind of surprise for me, see mybatis-config.xml |
0 commit comments