Integrate MyBatis-Plus (MP) with Integrated Native MyBatis (MB) in Spring Boot MicroservicesConfigurationThe main differences on. MyBatis-Plus is an enhancement based on MyBatis. It is compatible with all MyBatis configuration methods and provides simpler and more powerful configuration options.
Similarities:
Basic DataSource Configuration:
Exactly the same.
Whether using native MB or MP,or
The database connection information is configured in the file. Spring Boot automatically configures the data source (usually HikariCP connection pool).
spring: datasource: url: jdbc:mysql://localhost:3306/my_service_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: your_password driver-class-name: # HikariCP Pool settings (optional, Spring Boot has defaults) hikari: maximum-pool-size: 10 minimum-idle: 5 connection-timeout: 30000
Mapper Interface Scanning:
Exactly the same.Both need to be used@MapperScan
Annotation to tell Spring Boot to find the Mapper interface under which package path.
import ; import ; import ; @SpringBootApplication @MapperScan("") // Specify the package containing your Mapper interfaces public class YourServiceApplication { public static void main(String[] args) { (, args); } }
Type Aliases Package Scanning:
Basically the same.
Both are available-aliases-package
(Native MB) or-aliases-package
(MP Recommended) attributes specify the package where the entity class resides in order to use the abbreviation of the class name in XML or internally. MP is also compatible with native properties.
# Using MyBatis-Plus recommended property mybatis-plus: type-aliases-package: # Or using the native MyBatis property (MP is compatible) # mybatis: # type-aliases-package:
support (Support for
):
The compatibility is the same.Both support it-location
(Native MB) or-location
(MP Recommended) Specify traditionalFile path, used to perform some MyBatis core settings that cannot be configured through properties files (such as custom TypeHandler, ObjectFactory, plug-in, etc.). But in MP, due to its powerful configuration capabilities,
The dependency is usually reduced.
Differences:
Dependencies:
Native MB:Need to be introducedmybatis-spring-boot-starter
。
<dependency> <groupId></groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>...</version> <!-- Use compatible version --> </dependency>
MP:Need to be introducedmybatis-plus-boot-starter
。Notice:mybatis-plus-boot-starter
Already includedmybatis-spring-boot-starter
, so there is no need to repeatedly introduce the starter of native MB.
<dependency> <groupId></groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>...</version> <!-- Use compatible version, ., 3. --> </dependency>
Mapper XML file location (Mapper XML Location):
Native MB:Must pass-locations
The configuration item specifies the path to the Mapper XML file because the core SQL is written in XML.
mybatis: mapper-locations: classpath*:/mapper/**/*.xml
MP: ifYou also need to write custom SQL in XML files (MP fully supports mixed use), thenNeed tooConfiguration-locations
(Recommended) or-locations
. butifYou mainly rely on the universal mapper provided by MP (BaseMapper
) andWrapper
Conditional constructors are developed, for basic CRUD and many dynamic queries you mayunnecessaryWhen writing any XML file, this configuration item is not required at this time (but it is usually recommended to configure it for emergencies or to maintain consistency).
mybatis-plus: mapper-locations: classpath*:/mapper/**/*.xml # Configure if using custom XML SQL
Global Configuration:
Native MB:Most global configurations need to beSet in, or through limited
.*
Properties configuration (such asmap-underscore-to-camel-case
)。
MP:Provides a richer/properties
Configuration items to control global behavior, mainly through-config.*
and.*
。
mybatis-plus: configuration: map-underscore-to-camel-case: true # Same as -underscore-to-camel-case log-impl: # Configure logging implementation global-config: db-config: id-type: assign_id # Default primary key strategy (Snowflake) logic-delete-field: deleted # Global logic delete field name logic-delete-value: 1 # Logic deleted value logic-not-delete-value: 0 # Logic not deleted value banner: false # Disable MP banner on startup
MP'sglobal-config
Provides global default settings for MP-specific functions such as ID generation strategy, logical deletion, basic database configuration (table name/field name processing), Banner switch, etc.
Plugin Configuration:
-
Native MB:Plugins (such as the paging plugin PageHelper) usually require the introduction of dependencies separately and in
Or through Java
Bean
configurable and registered in a way. -
MP:Many commonly used plug-ins (paging, optimistic locking, multi-tenant, anti-table update, etc.) are built in, which can be used through
/properties
of.*
Configuration items are directly enabled and configured, making them more convenient and standardized.
mybatis-plus: plugins: # Pagination Plugin pagination: max-limit: 500 # Max items per page # Optimistic Locker Plugin optimistic-locker: enabled: true # Block Attack Plugin (Prevent full table update/delete) block-attack: enabled: true
Summarize:
Configuration items/aspects | Native MyBatis (MB) | MyBatis-Plus (MP) | Explanation of similarities and differences |
---|---|---|---|
Depend on Starter | mybatis-spring-boot-starter |
mybatis-plus-boot-starter (Including the former) |
different:Use different Starters |
Data source configuration | .* |
.* |
same:Managed by Spring Boot |
Mapper interface scanning | @MapperScan |
@MapperScan |
same:All need to specify the Mapper package path |
Type alias package | -aliases-package |
-aliases-package (Recommended, compatible with the former) |
Basically the same:Consistent functions, MP recommends using its namespace |
Mapper XML Location |
-locations (usually required) |
-locations (Recommended, required only when using XML) |
Similarities and differences:The attribute name recommendation is different, and the dependence on XML is lower than MP |
|
-location (More commonly used) |
-location (Recommended, compatible with the former, with reduced dependency) |
Similarities and differences:Both support, but MP provides more configuration items through properties, reducing the need for XML |
Global configuration | Mainly in XML or limited.*
|
More abundant-config.* and.* property |
different:MP's Properties configuration capabilities are stronger |
Plug-in configuration | Usually additional dependencies + XML/Bean configuration is required | Built-in commonly used plug-ins, can be used.* Properties Configuration |
different:MP greatly simplifies the configuration of commonly used plug-ins |
In general, in Spring Boot microservice, MyBatis-Plus greatly simplifies the integration and configuration of MyBatis through its Boot Starter and rich configuration properties, especially for global settings and the use of common plug-ins. It maintains compatibility with native MyBatis while providing a more consistent approach to Spring Boot's "convention over configuration" philosophy.
This is the end of this article about the different configurations of MyBatis-Plus and Integrated Native MyBatis in Spring Boot Microservices. For more related Spring Boot integration MyBatis-Plus and MyBatis content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!