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

Spring Boot 2.x what are the ways to integrate Mybatis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what are the ways of integrating Mybatis with Spring Boot 2.x". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the ways of integrating Mybatis with Spring Boot 2.x?"

Preface

In the current environment, the use of Mybatis as the persistence layer framework still accounts for the vast majority, let's talk about several poses of using Mybatis.

Pose 1: zero configuration annotation development step 1: introduce dependencies

First, we need to add dependencies to the pom file:

Step 2: configuration file

Here we use yml to write. Compared with the properties file, yml looks more concise and elegant. It is important to note that the attribute driver-class-name is com.mysql.cj.jdbc.Driver instead of the previous com.mysql.jdbc.Driver. If we use the Driver in the previous package, the console will print out a line telling us to use the new ~

Step 3: specify MapperScan

First, we need to specify @ MapperScan in the startup class so that we don't have to add @ Mapper annotations to the Mapper interface one by one. The value here is the path of the package where your Mapper interface is located.

Step 4: write the Mapper interface

You can specify the SQL statement and the return value through the @ Select annotation and the @ Results annotation. Of course, other different operations also have different annotations, such as @ Insert, @ Update, @ Delete

Step 5: test program writing

Visit localhost:8081/user/findAll and we can see the results of the test:

Pose 2: XML

What we use most is this way. Let's take a look at how to achieve it. The first step and posture are repeated, so we won't elaborate any more here.

Step 1: write a configuration file

We need to add the path to mybatis-config.xml and the path to the file used for Mapper*.xml in the configuration file:

Step 2: write the Mapper interface and configuration file:

Mapper interface:

Mybatis-config.xml:

UserMapper.xml:

It should be noted here that the namespace in UserMapper.xml needs to be consistent with the path of the interface, otherwise an error will occur.

Test:

The program tested is the same as before. When we visit localhost:8082/user/findAll directly, we can see the successful results:

Pose 3: SqlSession

Use SqlSession pose and can be combined with the above two ways, emmm. It can be understood that the SQL is there, but it is divided into two different methods to call. Let's not talk about the rest of the code, but let's just look at how it is called:

As you can see, the parameter writes the path of the method to be called (in this case, corresponding to namespace). We can run the program below:

Pose 4: SqlSession-based upgrade

Here, we made an upgrade based on SqlSession. First, we wrote a BaseDao as a common interface. The interface is implemented through sqlSessionTemplate. Then we change the namespace of * Mapper.xml to * Mapper. Now, we can delete all Mapper interfaces directly and call them directly through dao.

Then let's start the program and visit

At this point, I believe you have a deeper understanding of "what is the way Spring Boot 2.x integrates Mybatis?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report