提示:以下是本篇文章正文內(nèi)容
一、MyBatis-Plus 是什么?
MyBatis-Plus官網(wǎng)介紹:MyBatis-Plus (opens new window)(簡稱 MP)是一個(gè) MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。
MyBatis-Plus封裝了單表的crud操作,減少基礎(chǔ)代碼編寫,提高開發(fā)效率。
支持自動(dòng)分頁、邏輯刪除、自動(dòng)填充、多數(shù)據(jù)源等功能。
支持的數(shù)據(jù)庫
MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,F(xiàn)irebird,Cubrid,Goldilocks,csiidb,informix,TDengine,redshift,達(dá)夢數(shù)據(jù)庫,虛谷數(shù)據(jù)庫,人大金倉數(shù)據(jù)庫,南大通用(華庫)數(shù)據(jù)庫,南大通用數(shù)據(jù)庫,神通數(shù)據(jù)庫,瀚高數(shù)據(jù)庫,優(yōu)炫數(shù)據(jù)庫,星瑞格數(shù)據(jù)庫
二、使用步驟
1.引入相關(guān)依賴
<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>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.16</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.application.yml配置
配置如下:
#mybatis
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
#實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔
typeAliasesPackage: com.xxx.*.entity
global-config:
#數(shù)據(jù)庫相關(guān)配置
db-config:
#主鍵類型 AUTO:"數(shù)據(jù)庫ID自增", INPUT:"用戶輸入ID", ID_WORKER:"全局唯一ID (數(shù)字類型唯一ID)", UUID:"全局唯一ID UUID";
id-type: AUTO
logic-delete-value: -1
logic-not-delete-value: 0
banner: false
#原生配置
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
jdbc-type-for-null: 'null'
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxx:3306/db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: xxx
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
#Oracle需要打開注釋
#validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
stat-view-servlet:
enabled: true
url-pattern: /druid/*
#login-username: admin
#login-password: admin
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: false
wall:
config:
multi-statement-allow: true
3.基礎(chǔ)使用
編寫實(shí)體類 User.java
@Data
@TableName("`user`")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
編寫 Mapper 包下的 UserMapper接口
public interface UserMapper extends BaseMapper<User> {
}
開始使用
public class UserService {
@Autowired
private UserMapper userMapper;
private void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
}
總結(jié)
通過以上幾個(gè)簡單的步驟,我們就實(shí)現(xiàn)了 User 表的 CRUD 功能,甚至連 XML 文件都不用編寫!
從以上步驟中,我們可以看到集成MyBatis-Plus非常的簡單,只需要引入 starter 工程,并配置 mapper 掃描路徑即可。文章來源:http://www.zghlxwxcb.cn/news/detail-737004.html
以上就是SpringBoot整合MyBatis-Plus的方式,希望對你有用文章來源地址http://www.zghlxwxcb.cn/news/detail-737004.html
到了這里,關(guān)于SpringBoot整合MyBatis-Plus,趕緊整過來!的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!