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.
- Write the MyBatis configuration file
mybatis-config.xml
- To obtain
SqlSession
- To obtain the corresponding
MapperInterface
The query
More on this later in the series
- How does MyBatis read configuration files
- How to construct sqlSession
- How do I get MapperInterface
- How to query Sql
- How to Set parameters
- How is the result returned
- .
These questions accompany our development process, will be very useful, I hope friends can follow the article with in-depth study.
conclusion
- Mybatis is a persistence framework
- Mybatis architecture preview, MyBatis generally contains three layers, interface layer, core layer, base layer
- 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.