pom.xml:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
配置:
#mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/school?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dbcp2.min-idle=5
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=5
spring.datasource.dbcp2.max-wait-millis=200
#mybatis-plus
# 如果是放在src/main/java目錄下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目錄 classpath:/mapper/*Mapper.xml
mybatis-plus.mapper-locations=classpath:/com/springboot/study/mapper/*Mapper.xml
#實體掃描,多個package用逗號或者分號分隔
mybatis-plus.type-aliases-package=com.springboot.study.entity
#主鍵類型 0:"數據庫ID自增", 1:"用戶輸入ID",2:"全局唯一ID (數字類型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus.global-config.id-type=3
#字段策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷"
mybatis-plus.global-config.field-strategy=2
#駝峯下劃線轉換
mybatis-plus.global-config.db-column-underline=true
#mp2.3+ 全局表前綴 mp_
#mybatis-plus.global.table-prefix: mp_
#刷新mapper 調試神器
mybatis-plus.global-config.refresh-mapper=true
#數據庫大寫下劃線轉換
mybatis-plus.global-config.capital-mode=true
# Sequence序列接口實現類配置
mybatis-plus.global-config.key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
#邏輯刪除配置(下面3個配置)
mybatis-plus.global-config.logic-delete-value=1
mybatis-plus.global-config.logic-not-delete-value=0
mybatis-plus.global-config.sql-injector=com.baomidou.springboot.MyMetaObjectHandler
#配置返回數據庫(column下劃線命名&&返回java實體是駝峯命名),自動匹配無需as(沒開啓這個,SQL需要寫as: select user_id as userId)
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
#配置JdbcTypeForNull, oracle數據庫必須配置
mybatis-plus.configuration.jdbc-type-for-null=null
MybatisPlusConfig.java
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
// paginationInterceptor.setLimit(你的最大單頁限制數量,默認 500 條,小於 0 如 -1 不受限制);
return new PaginationInterceptor();
}
}
user表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`qq` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`creat_time` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
Entity
@Data
@TableName("user")
public class User {
//使用 @ApiModelProperty(hidden = true) 使swagger輸入參數示例不顯示此字段
@ApiModelProperty(hidden = true)
//數據庫主鍵自增
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private String name;
private String qq;
private Integer age;
@ApiModelProperty(hidden = true)
private Date creatTime;
}
需要進行批量操作可以繼承 mybatisplus 的 ServiceImpl
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService
Mapper沒有使用全局@MapperScan註解指向我們的mapper包的話, 我們就要爲每個業務mapper使用@Mapper將業務mapper注入到Spring容器中
@Mapper
public interface UserMapper extends BaseMapper<User>{
}