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

How to register and obtain Mapper in MyBatis

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "MyBatis how to register and obtain Mapper", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "MyBatis how to register and obtain Mapper" this article.

1. Build environment 1.1 pom.xml mysql mysql-connector-java com.baomidou mybatis-plus-boot-starter 1.2 BlogMapper.javapublic interface BlogMapper {List selectBlog (String id);} 1.3 BlogMapper.xml select * from t_blog where id= # {id}

BlogMapper.xml is placed in the resource directory under the same path as the BlogMapper.java package path

1.4 MyBatisDemo.javapublic class MyBatisDemo {public static void main (String [] args) {/ / create data source DataSource dataSource = getDataSource (); TransactionFactory transactionFactory = new JdbcTransactionFactory (); / / create sql runtime environment Environment environment = new Environment ("development", transactionFactory, dataSource); / / create all configurations of mybatis Configuration configuration = new Configuration (environment) / / register mapper configuration.addMapper (BlogMapper.class); / / configuration.addInterceptor (new PaginationInterceptor ()); / / create a sql session factory based on configuration SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (configuration); SqlSession sqlSession = sqlSessionFactory.openSession (); BlogMapper mapper = sqlSession.getMapper (BlogMapper.class); System.out.println (mapper.selectBlog) } private static DataSource getDataSource () {DruidDataSource druidDataSource = new DruidDataSource (); druidDataSource.setUrl ("jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&serverTimezone=Asia/Shanghai"); druidDataSource.setUsername ("root"); druidDataSource.setPassword ("root"); return druidDataSource;} II. AddMapper detailed analysis of 2.1MapperRegistry

This block is to determine whether the mapper.xml has been parsed, which is done in parser.parse ();. Let's see.

LoadXmlResource (); according to the way xml parses each mapper interface, put the resulting MapperStatement into the configuration, and then record that the namespace representation of the xml has been processed. Specific call chain:

LoadXmlResource ()-> xmlParser.parse ()-> configurationElement (parser.evalNode ("/ mapper"))-> buildStatementFromContext (context.evalNodes ("select | insert | update | delete")-> buildStatementFromContext (list, null)-> statementParser.parseStatementNode ()-> builderAssistant.addMappedStatement- > configuration.addMappedStatement (statement)

ParseStatement (method); the method of parsing each mapper interface based on annotations, so xml and annotations can be used at the same time. But if the same method is used at the same time, an error will be reported.

2.2 MapperProxyFactory

What is put into knownMappers is MapperProxyFactory, which is a factory of Mapper proxies, which provides newInstance methods and produces a proxy class (that is, the proxy implementation class of the BlogMapper interface). Calling all methods of BlogMapper will be executed in the invoke method of MapperProxy

Third, detailed analysis of getMapper

GetMapper calls MapperRegistry's getMapper to get the proxy factory from knownMappers, and then calls the newInstance method to generate a proxy class MapperProxy.

3.1 MapperProxy

When mapper.selectBlog ("001") is executed, the invoke method of MapperProxy is called

The corresponding MapperMethod is generated according to method (selectBlog), and the MapperMethod is put into the local cache.

MapperMethod.execute (sqlSession, args); executes the real sql logic.

3.2 MapperMethod

The construction method of MapperMethod, according to the interface information, method information, configuration information to get SqlCommand (sql name, type), method (method signature), to facilitate subsequent execution of commands, processing result sets and so on.

The above is all the contents of the article "how to register and obtain Mapper for MyBatis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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