MyBatis Plus is an enhancement tool for MyBatis. It only enhances MyBatis without changing it. It is born to simplify development and improve efficiency. MyBatis Plus provides Spring Boot support. The new MyBatis Plus 3.0 version is based on JDK8 and provides lambda form calls.
Introducing Maven dependencies
- MyBatis Plus framework
<! -- spring jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${version}</version>
</dependency>
<! -- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${version}</version>
</dependency>
Copy the code
- JDBC connection pool and database driver
<! -- HIkari JDBC connection pool -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${version}</version>
</dependency>
<! Mysql connection driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${version}</version>
</dependency>
Copy the code
configuration
- Edit application.yml to configure the data source
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver # database connection driver
username: ${MYSQL_USERNAME:engrz} Database connection user name
password: ${MYSQL_PASSWORD:passwd2021} Database connection password
url: jdbc:mysql://${MYSQL_HOST:mysqlhost}:${MYSQL_PORT:3306}/database? characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allow Database connection url
Copy the code
Spring Boot supports hikari, DBCP2, and druid. To set connection pool parameters, see Spring Boot Data Properties.
- Configure MyBatis Plus in application.yml:
# mybaits - plus configuration
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
global-config:
banner: false
db-config:
id-type: auto
table-underline: true
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
map-underscore-to-camel-case: true
Copy the code
Use the @mapperscan annotation in the project entry:
@SpringBootApplication
@MapperScan("com.engrz.demo.mybatisplus")
public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
MapperScan specifies the packet path to scan.
MyBatis Plus is developed by Chinese, the official document is very comprehensive in Chinese, so I will not repeat it here, click here to direct the official guide, and some practice and advanced usage of MyBatis Plus will be written later.
All are “Siege Lion · Zheng” unless noted. Link to this article: engr-z.com/118.html