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 MyBatis-Plus customizes SQL

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "MyBatis-Plus how to customize SQL", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "MyBatis-Plus how to customize SQL" this article.

1. Write XML1.1 directory structure under src/main/resource directory

We put UserMapper.xml in the mapper directory under resource, and my entire project directory is as follows

1.2 write Mapper interfaces corresponding to entity classes

Tip: we have customized the getAll () method here to get information about all users.

Package com.yixin.mapper; import com.yixin.pojo.User;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import org.springframework.stereotype.Repository; import java.util.List; / *

* Mapper API *

* * @ author yixin * @ since 2022-01-17 * / @ Repositorypublic interface UserMapper extends BaseMapper {public List getAll ();} 1.3 write UserMapper.xml select * from user 1.4 scan the location of our xml configuration in the configuration file.

Application.properties:

Mybatis-plus.mapper-locations=classpath:/mapper/**.xml1.5 Test import org.springframework.boot.test.context.SpringBootTest; import java.io.InputStream;import java.util.ArrayList;import java.util.List; @ SpringBootTestclass AutoApplicationTests {@ Autowired private UserMapper userMapper; @ Test void test () {List userList=userMapper.getAll (); System.out.println (userList);}}

Results:

It can be found that our message has appeared successfully!

2. Write XML2.1 directory structure under src/main/java directory

TIp: create a directory in the mapeer directory under java to store our xml configuration, xml package

2.2 write Mapper interface package com.yixin.mapper; import com.yixin.pojo.User;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import org.springframework.stereotype.Repository; import java.util.List; / * corresponding to entity class

* Mapper API *

* * @ author yixin * @ since 2022-01-17 * / @ Repositorypublic interface UserMapper extends BaseMapper {public List getAll ();} 2.3 write UserMapper.xml select * from user 2.4 write configuration files

Application.properties:

Mybatis-plus.mapper-locations=classpath:com/yixin/mapper/xml/UserMapper.xml2.5? Configure pom.xml (important)?

This step is very important because if you run it without configuring pom.xml, the following error will occur.

Why?

This is because maven does not package resources such as xml of the src/main/java directory into the class folder by default, but simply ignores it. If you don't believe me, let's take a look at the target directory.

You can see that our xml files are not packaged here.

Solution: configure pom.xml

We just need to add the following configuration to pom and tell idea to package the xml under src/main/java into the class folder when compiling, so that no exception will be reported.

Src/main/java * * / * .xml 2.6Test import org.springframework.boot.test.context.SpringBootTest; import java.io.InputStream;import java.util.ArrayList;import java.util.List; @ SpringBootTestclass AutoApplicationTests {@ Autowired private UserMapper userMapper; @ Test void test () {List userList=userMapper.getAll () System.out.println (userList);}}

Results:

It can be found that our database information has appeared successfully!

Let's take a look at the following target directory:

You can find that our xml configuration has been imported, if it is still not found, it is recommended to delete the target directory, and then rerun, you can regenerate the target directory, at this time you can find your xml file.

III. The difference between classpath and classpath*

Classpath: will only look under your class path

Classpath*: contains not only the class path, but also the jar file (class path) to find

Generally speaking, classpath* is needed for file configuration in the project development of large companies, because third-party jar is usually used, so in most cases you will need to find the configuration files of these jar.

The above is all the content of this article "how to customize SQL by MyBatis-Plus". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.

Share To

Development

Wechat

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

12
Report