In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
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 Spring integration Mybatis ideas, the article is very detailed, has a certain reference value, interested friends must read it!
Introduce dependency
Spring
Myabtis
Mysql
Mybatsi-spring
How to integrate?
Spring:
The project management framework is mainly used to create, use and destroy component objects in the project.
Mybatis:
The persistence layer framework is mainly used to simplify the database access operation of the original jdbc technology.
Integration idea: take over the creation of core objects in the Mybatis framework through the Spring framework.
Who is the core object in the Mybatis framework?
SqlSession?
SqlSessionFactory?
SqlSessionFactoryBuilder?
Dao?
The core object must be: SqlSessionFactory.
The function of SqlSessionFactoryBuilder is to read the parsing configuration file = = [data source configuration, mapper file configuration] = = to create the SqlSessionFactory.
The creation of SqlSession depends on SqlSessionFactory.
= > SqlSessionFactory is the core object.
SM integration
Integration idea: take over the creation of the core SqlSessionFactory object in Mybatis through the Spring framework.
Is SqlSessionFactory a simple object or a complex object?
If it is a simple object
If it is a complex object
By looking at the source code, we can see that SqlSessionFactory is a complex object of interface type.
How to create it?
Is = Resources.getResourceAsStream ("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder (), build (is); 1) .SqlSessionFactory (Factory) Bean implements FactoryBean {SqlSessionFactory getObject () {is = Resources.getResourceAsStream ("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder (), build (is);} Class getClass () {return SqlSessionFactory.class;} boolean isSingleton () {return true } 2). Factory management SqlSessionFactory3). Factory gets SqlSessionFactory sf = context.getBean ("sqlSessionFactory")
After writing, we can find that this code is fixed, this project should be written, and other projects should be written as well.
So Mybatis officially wrote it for us and encapsulated this code: Mybatis-spring.jar.
A class is provided in the jar package: SqlSessionFactoryBean.
In the future, we just need to quote Mybatsi-spring to rely on it, and we don't have to write it ourselves.
It is worth noting that mybatis officially provides SqlSessionFactoryBean and is no longer using the mybaits main configuration file.
The core of the main configuration file is [data source] [registration of mapper files]
So we're going to inject the data source object, introduce the druid dependency, and inject the location of the mapper file. [DI thought, dependency injection]
. The above is all the contents of the article "sample Analysis of Spring integrating Mybatis ideas". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.