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 integrate Mybatis into Spring

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

Share

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

This article is about how to integrate Mybatis into Spring. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Basic introduction

To put it bluntly, the so-called Spring integration Mybatis is to hand over the loading process of mybatis to Spring, no longer need to load configuration tools and other operations, while the specific dao layer operation is still using mybatis to operate the database.

1.1 steps for using mybatis:

1. The first step is to write a mybatis-config.xml core configuration file that configures basic environment support: data source, driver, url, username, password...

two。 Then write the mybatisUtil utility class, first load the mybatis-config.xml resource Resource in the form of an IO stream, then create a SqlSessionFactory factory through the SqlSessionFactoryBuilder factory builder, and encapsulate the SqlSessionFactory factory as a singleton factory; finally, only throw a SqlSession session to get the interface!

3. Finally, the dao layer obtains the Mapper mapper through SqlSession to execute the SQL!

1.2 Spring Integration mybatis steps:

First, configure the mybatis-config.xml environment: data source, driver, url, username, password. These basic configurations are handed over to Spring's core configuration file application.xml file! Create a bean object dataSource to replace Resource.

Then through the dataSource object, create the bean object SqlSessionFactory, and then the SqlSessionFactory factory will be hosted by Spring! (it is recommended to configure it as a singleton in accordance with the custom of mybatis)

Then inject the Configration configuration in the mybatis-config.xml configuration file into SqlSessionFactory,mybatis-config.xml in the form of dependency injection and everything will take effect in Spring.

Finally, the acquisition of SqlSession is also handed over to Spring hosting, and the SqlSessionFactory factory is injected into the SqlSession dependency in the form of constructor injection!

After that, all the dao layer operations get the SqlSession in the form of getBean, and then perform the mybatis use steps above in SQL; comparison. The integration is basically to put the loading of mybatis in charge of Spring, that's all.

Second, mybatis core configuration III. Spring core configuration IV. Database and corresponding POJO entity classes

Package com.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@AllArgsConstructor@NoArgsConstructor@Datapublic class User {private int id; private String username; private String password;} V, dao layer 5.1 UserMapper interface package com.dao;import com.pojo.User;import java.util.List;public interface UserMapper {public List getUserList ();} 5.2 UserMapper.xml configuration select * from user 5.3 Interface implementation Class (1)

This implementation class is not required when using mybatis alone, and the database operations done by the impl implementation class are handed over to the service layer for execution. Now you need this implementation class, which internally aggregates the SqlSessionTemplate object (equivalent to SqlSession), then gets the mapper through reflection in the implementation method, and then executes SQL!

Import com.pojo.User;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import java.util.List;@Repository ("userMapperImpl") public class UserMapperImpl implements UserMapper {@ Autowired private SqlSessionTemplate sqlSession; @ Override public List getUserList () {UserMapper mapper = sqlSession.getMapper (UserMapper.class); List list = mapper.getUserList (); return list;}} 5.4 interface implementation class (2)

In the latest integration of Spring-Mybatis, the SqlSessionDaoSupport abstract class is provided to replace the internal aggregate SqlSessionTemplate object, which only needs to be inherited. Although there is no apparent need for dependency injection, the parent class does need dependency injection for a SqlSessionFactory factory!

Import com.pojo.User;import org.apache.ibatis.session.SqlSession;import org.mybatis.spring.support.SqlSessionDaoSupport;import java.util.List;public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {@ Override public List getUserList () {SqlSession sqlSession = getSqlSession (); UserMapper mapper = sqlSession.getMapper (UserMapper.class); List list = mapper.getUserList (); return list Test @ Testpublic void test1 () {ApplicationContext context = new ClassPathXmlApplicationContext ("application.xml"); UserMapper userMapperImpl = context.getBean ("userMapperImpl", UserMapper.class); List list = userMapperImpl.getUserList (); for (User user: list) {System.out.println (user.toString ());}} Thank you for reading! This is the end of the article on "how to integrate Mybatis into Spring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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