简体   繁体   中英

myBatis using mapper method VS session method

Learning myBatis now, found there are two ways execute sql

one is using session.method(), the other is using mapper.method().

finding that the 2nd way(mapper.method) is actually achieved by using proxy and indeed the session methods.

can some help explain why using the mapper.method() way which cost so much efforts rather than directly using the session.method()?

Tks

In mybatis interfaces could be used as mappers which somewhat acts as proxy . One of The advantage of using interface for mapping is we get code completion while coding in IDE like eclipse . Another use is your code is modularized and backtracking will be easy in this way

If you Map your sql in xml file and use session object to execute it you have to remember the sql statement id's

How ever writing complex queries for search is convenient to write in xml rater than in java mapper interfaces as annotations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM