Spring boot integration mybatis + mybatis plus example code

Spring boot is a new thing for me. During the learning process, I found that it is still easy to get started. Spring data JPA will be used by default when spring boot is not configured. This thing can be said to be a very concise tool, but I still prefer to use mybatis. There is no best tool, only one suitable for me.

Speaking of mybatis, recently there is a very useful tool - mybatis plus (official website). Now the updated version is 2.1.2, and this version is also used here. My favorite functions are code generator and condition constructor, so that it can be easier to develop.

There is a whole example of spring boot on the official website of mybatisplus. I followed it. As a result, the program didn't run. Later, I learned that the H2 database used in the demo is not the same as mysql. Therefore, if you want to integrate mybatisplus, you can avoid looking at the official website and avoid detours.

The following is the integration process

1. First, get all the required jar files, POM XML needs the following

pom. XML (incomplete)

2. Add mybatis related configurations, such as account, password, etc. Here I use application YML to match.

application. yml

The configuration is similar to that we used to configure with mybatis, but there is no XML configuration file for spring boot. Pay attention to the contents of the scarlet letter. It's basically no problem.

3. Mybatis plus configuration file ----- mybatisplusconfig. First, the file path is described in the figure above. Mybatisplusconfig is placed in the config folder, and the XML file is placed in mapper under resources.

Then comes the content of mybatisplusconfig

MybatisProperties. java

Here is the complete configuration file. It should be noted that the imported package should not be misquoted!

4. Also start the scanning of Dao. It is very simple to add @ mapperscan ("com. TDx. Account_service. Dao *") to the startup file. The following is complete

At this point, the configuration is completed, and the project can be run after running.

I think the most interesting part of mybatis plus should be the code generator. The following is the use process of the code generator

pom. Add something to the XML

1. Configuration file for code generator

MybatisPlusConfig. java

There are still a lot of changes in the file. The most important thing is the connection information of MySQL. There is no reason why you can connect if your account and password are wrong. Secondly, set the path of the template file you generated. The path I generated here can be seen in the figure above, which is on COM tdx. account_ Under service, note that the XML file should be placed under resources, otherwise it is recognized, saying that this method cannot be found

The files generated according to the code template on the official website are basically white. Mybatis plus integrates public methods, and many common tools can be referenced. Here I provide the modified controller java. VM files, mainly according to my own habits

controller. java. vm

In addition to this file, other code templates are based on the official website. Here, three external files are referenced, including abstractcontroller java (controller base class), datatablesjson.java and jsonresult.java, and then these files refer to other files. I'm confused. How can there be so many files? It seems that if all of them are pasted, it will take up more space, so I choose to believe in the code cloud. If necessary, please download the code cloud according to the fewer files and the code cloud address. It should be noted that my template is directly replaced Mybatis plus The templates directory in jar.

Finally, it's the test process

The following code segments are placed in the controller file, and then start the program, corresponding port and request method. The test needs to operate according to its own entity class, so it is for reference only.

I think the most interesting thing about so many tests is the conditional constructor, which is more complete on the official website, and I write it here according to my own needs.

Finally, thank you for watching. I'm not experienced in blogging. Please forgive me for my bad writing. If I can bring you help, please give me a praise. If I don't understand, or if I write something wrong, please put it forward. Thank you! I also hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>