mybatis: MyBatis on Spring Boot 4.2.0 - annotation and XML mappers, result maps, dynamic SQL, PageHelper, @MybatisTest, seven real transcripts

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3obqqePfZ9m2Jk5Ub
This commit is contained in:
Ankur Mhatre
2026-11-26 18:16:47 +05:30
co-authored by Claude Sonnet 5.5
parent fc8bc933d5
commit 8d8c28e2e5
35 changed files with 1332 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
## findById(7)
==> Preparing: SELECT id, name, price, stock, active, category_id FROM product WHERE id = ?
==> Parameters: 7(Long)
<== Total: 1
result: Product[7, Product 07, 27.50, stock=9, active=true, categoryId=2]
## findById(999) (no such row, returns Optional)
==> Preparing: SELECT id, name, price, stock, active, category_id FROM product WHERE id = ?
==> Parameters: 999(Long)
<== Total: 0
result: Optional.empty
## findAll()
==> Preparing: SELECT id, name, price, stock, active, category_id FROM product ORDER BY id
==> Parameters:
<== Total: 45
rows: 45
## topPriced(3) -> List<ProductSummary> (a record)
==> Preparing: SELECT id, name, price FROM product WHERE active = TRUE ORDER BY price DESC LIMIT ?
==> Parameters: 3(Integer)
<== Total: 3
ProductSummary[id=45, name=Product 45, price=122.50]
ProductSummary[id=44, name=Product 44, price=120.00]
ProductSummary[id=43, name=Product 43, price=117.50]
## insert (id before: null)
==> Preparing: INSERT INTO product(name, price, stock, active, category_id) VALUES (?, ?, ?, ?, ?)
==> Parameters: Desk lamp(String), 39.90(BigDecimal), 12(Integer), true(Boolean), 2(Long)
<== Updates: 1
rows affected: 1
id after (written back by useGeneratedKeys): 46
## decrementStock(id=1, qty=3) (stock of product 1 is 7)
==> Preparing: UPDATE product SET stock = stock - ? WHERE id = ? AND stock >= ?
==> Parameters: 3(Integer), 1(Long), 3(Integer)
<== Updates: 1
rows affected: 1
## decrementStock(id=1, qty=500) (guard in WHERE: stock >= qty)
==> Preparing: UPDATE product SET stock = stock - ? WHERE id = ? AND stock >= ?
==> Parameters: 500(Integer), 1(Long), 500(Integer)
<== Updates: 0
rows affected: 0
## columns aliased to label / cost (id keeps its name)
with @Results: Product[7, Product 07, 27.50, stock=null, active=null, categoryId=null]
without @Results: Product[7, null, null, stock=null, active=null, categoryId=null]
## delete(1)
==> Preparing: DELETE FROM product WHERE id = ?
==> Parameters: 1(Long)
<== Updates: 1
rows affected: 1
findById(1) inside the same test transaction: Optional.empty
## a later test sees the original data again (rollback)
findAll().size() = 45, findById(1) present = true
+35
View File
@@ -0,0 +1,35 @@
## findWithCategory(7) -> association fills product.category from joined columns
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id , c.id AS c_id, c.name AS c_name FROM product p JOIN category c ON c.id = p.category_id WHERE p.id = ?
==> Parameters: 7(Long)
<== Total: 1
result: Product[7, Product 07, 27.50, stock=9, active=true, categoryId=2, category=Gadgets]
## allWithProductsJoin() -> one statement, nested result map folds rows into objects
==> Preparing: SELECT c.id AS c_id, c.name AS c_name, p.id AS p_id, p.name AS p_name, p.price AS p_price, p.stock AS p_stock FROM category c LEFT JOIN product p ON p.category_id = c.id AND p.active = TRUE ORDER BY c.id, p.id
==> Parameters:
<== Total: 41
Category[1, Books, products=14]
Category[2, Gadgets, products=13]
Category[3, Kitchen, products=14]
statements executed: 1
## allWithProductsNestedSelect() -> one query, then one more per category
==> Preparing: SELECT id, name FROM category ORDER BY id
==> Parameters:
====> Preparing: SELECT id, name, price, stock, active, category_id FROM product WHERE category_id = ? AND active = TRUE ORDER BY id
====> Parameters: 1(Long)
<==== Total: 14
====> Preparing: SELECT id, name, price, stock, active, category_id FROM product WHERE category_id = ? AND active = TRUE ORDER BY id
====> Parameters: 2(Long)
<==== Total: 13
====> Preparing: SELECT id, name, price, stock, active, category_id FROM product WHERE category_id = ? AND active = TRUE ORDER BY id
====> Parameters: 3(Long)
<==== Total: 14
<== Total: 3
Category[1, Books, products=14]
Category[2, Gadgets, products=13]
Category[3, Kitchen, products=14]
statements executed: 4
## both strategies return the same product ids per category
identical: true
+59
View File
@@ -0,0 +1,59 @@
## no filters: <where> disappears completely
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p ORDER BY p.id
==> Parameters:
<== Total: 45
## only active = true: <where> appears, no stray AND
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.active = ? ORDER BY p.id
==> Parameters: true(Boolean)
<== Total: 41
## name contains '1', price between 20 and 60
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.name LIKE ? AND p.price >= ? AND p.price <= ? ORDER BY p.id
==> Parameters: %1%(String), 20(BigDecimal), 60(BigDecimal)
<== Total: 10
## categories 1 and 3, sorted by price desc (choose/when)
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.category_id IN (?,?) ORDER BY p.price DESC, p.id
==> Parameters: 1(Long), 3(Long)
<== Total: 30
## empty category list is ignored, not turned into IN () (sorted by name so it is a new statement, not a cache hit)
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p ORDER BY p.name, p.id
==> Parameters:
<== Total: 45
## findByIds([2, 4, 6]) -> foreach
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.id IN ( ? , ? , ? ) ORDER BY p.id
==> Parameters: 2(Long), 4(Long), 6(Long)
<== Total: 3
## findByIds([]) -> foreach over an empty list produces 'IN' with nothing after it
exception: BadSqlGrammarException
root cause: JdbcSQLSyntaxErrorException - Syntax error in SQL statement "SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p\000a WHERE p.id IN\000a \000a [*]ORDER BY p.id"; expected "("; SQL statement:
## updateSelective with only stock set -> <set> writes just that column
==> Preparing: UPDATE product SET stock = ? WHERE id = ?
==> Parameters: 99(Integer), 2(Long)
<== Updates: 1
## updateSelective with nothing set -> <set> has no content, the SQL is broken
root cause: JdbcSQLSyntaxErrorException - Syntax error in SQL statement "UPDATE product\000a \000a [*]WHERE id = ?"; expected "., AS, SET"; SQL statement:
## insertAll -> one INSERT with two value tuples
==> Preparing: INSERT INTO product(name, price, stock, active, category_id) VALUES (?, ?, ?, ?, ?) , (?, ?, ?, ?, ?)
==> Parameters: A(String), 1.00(BigDecimal), 1(Integer), true(Boolean), 1(Long), B(String), 2.00(BigDecimal), 2(Integer), true(Boolean), 1(Long)
<== Updates: 2
generated ids written back: [46, 47]
## ${q} with q = zzz' OR '1'='1' OR name LIKE '
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.name LIKE '%zzz' OR '1'='1' OR name LIKE '%' ORDER BY p.id
==> Parameters:
<== Total: 45
rows returned: 45
## #{q} with q = zzz' OR '1'='1' OR name LIKE '
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.name LIKE CONCAT('%', ?, '%') ORDER BY p.id
==> Parameters: zzz' OR '1'='1' OR name LIKE '(String)
<== Total: 0
rows returned: 0
+66
View File
@@ -0,0 +1,66 @@
## service.page(none, pageNum=2, pageSize=10) -> PageHelper rewrote the query and added a count query
==> Preparing: SELECT count(0) FROM product p
==> Parameters:
<== Total: 1
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p ORDER BY p.id LIMIT ? OFFSET ?
==> Parameters: 10(Integer), 10(Long)
<== Total: 10
pageNum=2 pageSize=10 size=10 total=45 pages=5
hasPreviousPage=true hasNextPage=true
ids on this page: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
## filter + sort + page: the dynamic SQL and the paging compose
==> Preparing: SELECT count(0) FROM product p WHERE p.active = ? AND p.category_id IN (?)
==> Parameters: true(Boolean), 2(Long)
<== Total: 1
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.active = ? AND p.category_id IN (?) ORDER BY p.price DESC, p.id LIMIT ?
==> Parameters: true(Boolean), 2(Long), 5(Integer)
<== Total: 5
total=13 pages=3 ids: [43, 37, 34, 31, 28]
## last page and a page past the end (reasonable=false)
page 5: size=5 hasNextPage=false
page 9: size=0 total=45 list=Page{count=true, pageNum=9, pageSize=10, startRow=80, endRow=90, total=45, pages=5, reasonable=false, pageSizeZero=false}[]
## TRAP: startPage(1, 5), then an unrelated query, then the search you meant to page
unrelated query:
==> Preparing: SELECT count(0) FROM product p WHERE p.id IN (?)
==> Parameters: 1(Long)
<== Total: 1
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.id IN ( ? ) ORDER BY p.id LIMIT ?
==> Parameters: 1(Long), 5(Integer)
<== Total: 1
the search you meant to page:
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p ORDER BY p.id
==> Parameters:
<== Total: 45
rows returned by the search: 45 (expected 5)
## TRAP: startPage(1, 5) and no query afterwards
ThreadLocal page still set on this thread: true (pageSize=5)
next unrelated query on the same thread returned 5 rows instead of 7
==> Preparing: SELECT count(0) FROM product p WHERE p.id IN (?, ?, ?, ?, ?, ?, ?)
==> Parameters: 1(Long), 2(Long), 3(Long), 4(Long), 5(Long), 6(Long), 7(Long)
<== Total: 1
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p WHERE p.id IN ( ? , ? , ? , ? , ? , ? , ? ) ORDER BY p.id LIMIT ?
==> Parameters: 1(Long), 2(Long), 3(Long), 4(Long), 5(Long), 6(Long), 7(Long), 5(Integer)
<== Total: 5
after PageHelper.clearPage(): ThreadLocal page = null
## RowBounds(offset=10, limit=5) with PageHelper on the classpath: PageHelper turns it into LIMIT/OFFSET
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p ORDER BY p.id LIMIT ? OFFSET ?
==> Parameters: 5(Integer), 10(Long)
<== Total: 5
ids returned: [11, 12, 13, 14, 15]
## hand-written LIMIT/OFFSET (no plugin)
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p ORDER BY p.id LIMIT ? OFFSET ?
==> Parameters: 5(Integer), 10(Integer)
<== Total: 5
ids returned: [11, 12, 13, 14, 15]
## the lambda form when something fails: is a page left parked on the thread?
A) lambda throws before any query: caught boom
ThreadLocal page afterwards: still set, pageSize=5
B) query fails inside the lambda: caught BadSqlGrammarException
ThreadLocal page afterwards: null
+13
View File
@@ -0,0 +1,13 @@
## beans in the @MybatisTest slice
mapper beans: [categoryMapper, productMapper, productXmlMapper]
ProductService (a @Service) present: false
SqlSessionFactory present: true
mapped statements registered: [com.ankurm.mybatis.mapper.ProductXmlMapper.search]
map-underscore-to-camel-case (from application.properties): true
plugins (interceptors) registered: []
DataSource URL: jdbc:h2:mem:<generated>
## RowBounds(offset=10, limit=5) with plain MyBatis: no LIMIT in the SQL, MyBatis skips rows on the client
==> Preparing: SELECT p.id, p.name, p.price, p.stock, p.active, p.category_id FROM product p ORDER BY p.id
==> Parameters:
ids returned: [11, 12, 13, 14, 15]
@@ -0,0 +1,13 @@
## versions on the classpath (jar file names)
Spring Boot: 4.2.0
Spring Framework: 7.1.0
MyBatis: mybatis-3.5.19.jar
MyBatis-Spring: mybatis-spring-4.1.0.jar
Boot autoconfigure: mybatis-spring-boot-autoconfigure-4.1.0.jar
PageHelper: pagehelper-6.1.1.jar
PageHelper starter autoconfigure: pagehelper-spring-boot-autoconfigure-4.1.1.jar
Java: 25.0.5.1+2-LTS
H2: 2.5.252 (2026-09-23)
## plugins in the full @SpringBootTest context
plugins (interceptors) registered: [com.github.pagehelper.PageInterceptor]
+3
View File
@@ -0,0 +1,3 @@
## mybatis.configuration.map-underscore-to-camel-case=false (MyBatis's own default)
columns in the SELECT: id, name, price, stock, active, category_id
result: Product[7, Product 07, 27.50, stock=9, active=true, categoryId=null]