brief introduction
MyBatis-Plus (MP) is a MyBatis enhancement tool. It is based on MyBatis only to enhance without changing. It is born to simplify development and improve efficiency.
Characteristic
- Non-intrusive: only enhance without change, introduce it will not affect existing projects, such as silky smooth
- Low loss: Automatically inject basic CURD at startup, without loss of performance and direct object-oriented operation
- Powerful CRUD operation: Built-in general Mapper, general Service, only through a small amount of configuration can achieve most CRUD operation of single form, more powerful conditional constructor, to meet all kinds of use needs
- Support for Lambda Formal Call: Through Lambda expression, it is convenient to write all kinds of query conditions without worrying about field errors.
- Support multiple databases: support MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQL Server 2005, SQL Server and other databases
- Support for automatic generation of primary keys: support up to four primary key strategies (including distributed unique ID generator - Sequence), free configuration, perfect solution to the primary key problem
- Support for XML Hot Loading: Mapper's corresponding XML supports hot loading, and for simple CRUD operations, it can even be started without XML.
- Supporting ActiveRecord Mode: Supporting ActiveRecord Formal Calls, Entity Classes can perform powerful CRUD operations simply by inheriting Model Classes
- Support for custom global generic operations: support for global generic method injection (Write once, use anywhere)
- Support for automatic keyword escalation: support database keywords (order, key...) Auto-escaping and customizing keywords
- Built-in Code Generator: Using code or Maven plug-in can quickly generate Mapper, Model, Service, Controller layer code, support template engine, and more customized configuration for you to use.
- 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 queries.
- Built-in Performance Analysis Plug-in: It can output Sql statements and their execution time. It is recommended that this function be enabled when developing tests to quickly pull out slow queries.
- Built-in global interception plug-in: provide full table delete, update operation intelligent analysis blocking, also can customize interception rules to prevent misoperation
- Built-in Sql injection stripper: support Sql injection stripping, effectively prevent Sql injection attacks
Configuration with SpringBook Project
Introduce the father project first
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </parent>
Introduce spring-boot-starter, spring-boot-starter-test, mybatis-plus-boot-starter, lombok, h2 dependencies:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies>
To configure
Adding H2-related configuration to applcation.yml file
spring: datasource: driver-class-name: org.h2.Driver schema: classpath:db/schema-h2.sql data: classpath:db/data-h2.sql url: jdbc:h2:mem:test username: root password: test
Scanning Mapper files with the @MapperScan annotation on the spring boot boot boot boot class
@SpringBootApplication @MapperScan("Your own mapper File address") public class Application { public static void main(String[] args) { SpringApplication.run(QuickStartApplication.class, args); } }
Code
Write the entity class user.java
@Data public class User { private Long id; private String name; private Integer age; private String email; }
Write userMapper.java
public interface UserMapper extends BaseMapper<User> { }
test
@RunWith(SpringRunner.class) @SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { System.out.println(("----- selectAll method test ------")); List<User> userList = userMapper.selectList(null); Assert.assertEquals(5, userList.size()); userList.forEach(System.out::println); } }
The selectList method in UserMapper has a parameter of MP's built-in conditional wrapper Wrapper, so there are no conditions without filling it in.