Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of mybatis-plus query Source Code

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of mybatis-plus query source code, the article is very detailed, has a certain reference value, interested friends must read it!

Configuration detail

Pom.xml

Dependency > com.baomidou mybatis-plus-boot-starter 3.4.1

Mapper

Public interface GenTableMapper extends BaseMapper {} test class @ RunWith (SpringRunner.class) @ SpringBootTest (classes = GendemoApplication.class) public class BlockqueueTestDemo {@ Autowired GenTableMapper genTableMapper; @ Test public void test () {List genTables = genTableMapper.selectList (new QueryWrapper ());}} debug process

1. Found that genTableMapper is a proxy object type.

two。 Enter the proxy object MybatisMapperProxy and call its invoke method, whose Class type is BaseMapper.selectList ()

3. Where the cachedInvoker () method returns a PlainMethodInvoker, which overrides the invoke () method of the MapperMethodInvoker interface

4. The execute () method of MybatisMapperMethod will eventually be called

Public class MybatisMapperMethod {public Object execute (SqlSession sqlSession, Object [] args) {Object result; switch (command.getType ()) {case INSERT: {Object param = method.convertArgsToSqlCommandParam (args); result = rowCountResult (sqlSession.insert (command.getName (), param)); break } case UPDATE: {Object param = method.convertArgsToSqlCommandParam (args); result = rowCountResult (sqlSession.update (command.getName (), param)); break;} case DELETE: {Object param = method.convertArgsToSqlCommandParam (args) Result = rowCountResult (sqlSession.delete (command.getName (), param)); break;} case SELECT: if (method.returnsVoid () & & method.hasResultHandler ()) {executeWithResultHandler (sqlSession, args); result = null } else if (method.returnsMany ()) {result = executeForMany (sqlSession, args);} else if (method.returnsMap ()) {result = executeForMap (sqlSession, args);} else if (method.returnsCursor ()) {result = executeForCursor (sqlSession, args) } else {Object param = method.convertArgsToSqlCommandParam (args); / / TODO if (IPage.class.isAssignableFrom (method.getReturnType () {result = executeForIPage (sqlSession, args) / / TODO has been changed here} else {result = sqlSession.selectOne (command.getName (), param) If (method.returnsOptional () & (result = = null | |! method.getReturnType () .equals (result.getClass () {result = Optional.ofNullable (result);} break Case FLUSH: result = sqlSession.flushStatements (); break; default: throw new BindingException ("Unknown execution method for:" + command.getName ()) } if (result = = null & & method.getReturnType (). IsPrimitive () &! method.returnsVoid ()) {throw new BindingException ("Mapper method'" + command.getName () + "attempted to return null from a method with a primitive return type (" + method.getReturnType () + ").);} return result;}}

5. This is judged to enter the executeForMany (sqlSession, args) method, and the method and parameters are displayed. The type of sqlSession is SqlSessionTemplate, so why pay attention to the type of sqlSession? Because SqlSession is an interface and there are many implementation classes, sometimes we don't know which implementation class's selectList () method is called. When we look at the type, we can enter the SqlSessionTemplate class, find selectList () and hit the breakpoint, and debug comes over.

6. Using the same method, the selectList () method of DefaultSqlSession is called again.

7. When you come to the selectList () method of DefaultSqlSession, you have already entered the source scope of mybatis. The type of executor is MybatisCachingExecutor

8. At this point, note that the handler of the MybatisCachingExecutor proxy class is a Plugin

9. Because I used the paging plug-in, I came to com.github.pagehelperPageInterceptor

10. The query is executed by MybatisCachingExecutor

11.MybatisCachingExecutor delegates BaseExecutor to execute the query

twelve。 Finally delegated to PreparedStatementHandler to handle it.

13. Finally, the result set is encapsulated by DefaultResultSetHandler

@ Override public List handleResultSets (Statement stmt) throws SQLException {ErrorContext.instance (). Activity ("handling results") .object (mappedStatement.getId ()); final List multipleResults = new ArrayList (); int resultSetCount = 0; ResultSetWrapper rsw = getFirstResultSet (stmt); List resultMaps = mappedStatement.getResultMaps (); int resultMapCount = resultMaps.size (); validateResultMapsCount (rsw, resultMapCount); while (rsw! = null & & resultMapCount > resultSetCount) {ResultMap resultMap = resultMaps.get (resultSetCount) HandleResultSet (rsw, resultMap, multipleResults, null); rsw = getNextResultSet (stmt); cleanUpAfterHandlingResultSet (); resultSetCount++;} String [] resultSets = mappedStatement.getResultSets (); if (resultSets! = null) {while (rsw! = null & resultSetCount < resultSets.length) {ResultMapping parentMapping = nextResultMaps.get (resultSetCount); if (parentMapping! = null) {String nestedResultMapId = parentMapping.getNestedResultMapId () ResultMap resultMap = configuration.getResultMap (nestedResultMapId); handleResultSet (rsw, resultMap, null, parentMapping);} rsw = getNextResultSet (stmt); cleanUpAfterHandlingResultSet (); resultSetCount++;} return collapseSingleResultList (multipleResults);} above is all the content of the article "sample Analysis of mybatis-plus query Source Code". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report