preface

I have been working for three years. Every time I want to settle something, I give up because of my serious procrastination. This time to set their own goals, the first series of articles written, the follow-up will have their own manual manual a Mybatis framework to confirm their own source reading effect.

What is Mybatis?

Mybatis

MyBatis is a first class Persistence Framework with support for custom SQL, stored Procedures and advanced Mappings.Copy the code

Mybatis is a class persistence framework that supports custom SQL, stored procedures, and advanced mapping.

In fact, just like we use the same, write an interface, and then map to the mapper file, and then you can query the database, this write is Mybatis framework for us to do.

Mybatis architecture

Architecture diagram of Mybatis (picture from website) :

The architecture is particularly clear, with three main layers.

Interface layer: Defines cruD interfaces for use by the framework

Core layer: The framework’s main processing logic, divided into four parts

Parse configuration files -> Parameter processing -> Execute SQL -> Result mapping

Base layer: contains some common components, logging, caching, data sources, and so on

Mybatis Helloword

Let’s take a look at the simplest use of Mybatis:

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
  new SqlSessionFactoryBuilder().build(inputStream);
try (SqlSession session = sqlSessionFactory.openSession()) {
  Blog blog = session.selectOne(
    "org.mybatis.example.BlogMapper.selectBlog".101);
}
Copy the code

It only takes three steps to use.

  1. Write the MyBatis configuration filemybatis-config.xml
  2. To obtainSqlSession
  3. To obtain the correspondingMapperInterfaceThe query

More on this later in the series

  1. How does MyBatis read configuration files
  2. How to construct sqlSession
  3. How do I get MapperInterface
  4. How to query Sql
  5. How to Set parameters
  6. How is the result returned
  7. .

These questions accompany our development process, will be very useful, I hope friends can follow the article with in-depth study.

conclusion

  1. Mybatis is a persistence framework
  2. Mybatis architecture preview, MyBatis generally contains three layers, interface layer, core layer, base layer
  3. Mybatis HelloWord preview, understand how to use it in the actual code process, and bring out the questions that may be questioned, together with the in-depth article of this series

I believe that after all there will be some harvest, I am a programmer Song, life, code more than.