Mybatis simple configuration introduction

Mybatis simple configuration introduction

This article describes the configuration based on XML, not the Mybatis configuration of annotations. It is recommended to use XML configuration in complex SQL situations.

1. Write POJO, JavaBean, whose purpose is to map query results in the database to JavaBeen;

2. Configure Mapper interface corresponding to POJO: there are various methods in it, corresponding to the query statement in mapper.xml;

3. Configure the xml mapping corresponding to POJO: write cache, Sql query, etc.;

4. Configure the main Mybatis configuration file of mybatis-config.xml: configure the data source, mapper.xml, that is, configure the pom.xml in the Maven framework

ps: The above four items have no fixed order

Configuration summary

1. All Mybatis configuration is to create SqlSession for SQL query;

2. In the final analysis, various configuration mappings are shielded in the program code, and only the Mapper interface is explicitly called. Then the interface implementation class is obtained through SqlSession.getMapper();

3. Then the mapper interface implementation class is obtained through mybatis-config.xml–>SqlSessionFactory–>SqlSessionFactory–>SqlSession–>mapper;

SqlSession brief introduction

SqlSession simple principle introduction

SqlSession provides select/insert/update/delete methods. In the old version, these methods using the SqlSession interface are used, but the new version of Mybatis will recommend the use of the Mapper interface method.

The mapper is actually a dynamic proxy object. After entering the execute method of MapperMethod, you can easily find the delete, update, query, and select methods of SqlSession. From the bottom of the implementation: through dynamic proxy technology, let the interface run, and then adopt the command mode Finally, the SqlSession interface method (the getMapper() method waits until the Mapper) is used to execute SQL queries (that is, the bottom layer of the Mapper interface method is implemented by the SqlSession interface method).
  Reprinted from the blog garden JJian

Guess you like

Origin blog.csdn.net/weixin_43850343/article/details/112590992