In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to integrate and use Spring Boot + Mybatis-Plus. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Mybatis-Plus (MP for short) is an enhancement tool of Mybatis, which is only enhanced but not changed on the basis of Mybatis, in order to simplify development and improve efficiency.
To put it simply, Mybatis-Plus is an enhanced toolkit for Mybatis that simplifies CRUD operations, provides a code generator, and a powerful conditional constructor. At the same time, there are a number of practical plug-ins: standard paging plug-in, performance analysis plug-in, global interception plug-in and so on. So that in the development process, the basic paradigm code is solved in one sentence, saving a lot of repetitive operations!
First, characteristics:
Non-intrusive: Mybatis-Plus extends on the basis of Mybatis, only enhancements and no changes, the introduction of Mybatis-Plus will not have any impact on your existing Mybatis architecture, and MP supports all Mybatis native features
Less dependency: rely only on Mybatis and Mybatis-Spring
Low loss: basic CURD will be automatically injected at startup, performance is basically lossless, and direct object-oriented operation
Prevention of Sql injection: built-in Sql injection stripper to effectively prevent Sql injection attacks
General CRUD operation: built-in general Mapper and general Service, most CRUD operations in a single table can be realized only through a small amount of configuration, and there are more powerful conditional constructors to meet all kinds of usage requirements.
Multiple primary key policies: support up to 4 primary key policies (including distributed unique ID generator), freely configurable to solve primary key problems perfectly
Support for hot loading: the XML corresponding to Mapper supports hot loading, and for simple CRUD operations, it can even start without XML
Support ActiveRecord: support ActiveRecord calls, entity classes only need to inherit the Model class to achieve basic CRUD operations
Support for code generation: use code or Maven plug-ins to quickly generate Mapper, Model, Service, Controller layer code, support template engine, and more custom configurations for you to use (P.S. More powerful than Mybatis's official Generator! )
Support custom global general operations: support global general method injection (Write once, use anywhere)
Support automatic escape of keywords: support automatic escape of database keywords (order, key.), and customize keywords.
Built-in paging plug-in: based on Mybatis physical paging, developers do not need to care about specific operations. After configuring the plug-in, writing paging is equivalent to ordinary List query.
Built-in performance analysis plug-in: it can output Sql statements and its execution time. It is recommended to enable this function when developing tests, which can effectively solve slow queries.
Built-in global intercept plug-in: provides intelligent analysis and blocking of full table delete and update operations to prevent misoperation
Second, the principle of architecture:
3. SpringBoot integration:
Springboot: 2.1.5.RELEASE
Mybatis-plus: 3.1.2
Special note: Mybatis and Mybatis-Spring rely on do not join the project configuration, so as not to cause version conflicts! Mybatis-Plus will automatically help you maintain!
Org.springframework.boot spring-boot-starter version > 2.1.5.RELEASE com.baomidou mybatis-plus-boot-starter 3.1.2 mysql mysql-connector-java 6.0.6 org.springframework.boot spring-boot-starter-test test
3. Prepare a sql script to create the t_sys_log table with the following structure information:
CREATE TABLE `taccounsyslog` (`faccounlog_ id`log` int (11) NOT NULL AUTO_INCREMENT COMMENT 'log id', `faccounoptiontype` varchar (50) DEFAULT NULL COMMENT' operation type', `faccounoptionperson` varchar (50) DEFAULT NULL COMMENT 'operator', `faccounoptioncontent`varchar 'operation content', `faccounoptionip` varchar (15) DEFAULT NULL COMMENT 'operation IP' `fSecretoption_ status`char (1) DEFAULT NULL COMMENT 'operation status (0: success 1: failed)', `faccounerrorinfo`text COMMENT 'exception information', `faccounoptiontime`timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'operation time', PRIMARY KEY (`fsuccesslogid`) ENGINE=InnoDB AUTO_INCREMENT=68 DEFAULT CHARSET=utf8
3. Configuration
Add the following configuration data source information to the configuration file application.properties:
Mysql-connector-java version 6.0.6 is currently used. It is officially recommended that driver classes use com.mysql.cj.jdbc.Driver, and previous versions use com.mysql.jdbc.Driver.
Spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql:// service IP:3306/ library name? characterEncoding=utf8spring.datasource.username= library user name spring.datasource.password= library password
4. test
After the above preparation and configuration, the Spring Boot and Mybatis-Plus environment has been integrated. Use junit to test the data source and connection:
@ RunWith (SpringRunner.class) @ SpringBootTestpublic class DemoApplicationTests {/ / automatic injection of data source instance @ Autowired private DataSource dataSource; @ Testpublic void contextLoads () throws SQLException {System.out.println ("="); System.out.println (dataSource); System.out.println (dataSource.getConnection ());}}
When you execute the junit test above, you can see the console input log results, printing the Mybatis-Plus flag banner and the data source and connection object respectively.
At this point, some companions should wonder why MyBatis-Plus has not been configured, so why has it been integrated? This is because after Spring Boot introduces the mybatis-plus-boot-starter dependency Jar package, it is left to Spring Boot to start the automatic configuration.
After reading the above, do you have any further understanding of how to integrate and use Spring Boot + Mybatis-Plus? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.