导入Maven依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- mybatis-plus代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.2.0</version>
</dependency>
添加application.yml配置
mybatis-plus配置项
mybatis-plus:
# xml文件路径
mapper-locations: classpath:mapper/*.xml
# 实体类路径
type-aliases-package: com.数据库表对应的实体类的路径
configuration:
# 驼峰转换
map-underscore-to-camel-case: true
# 是否开启缓存
cache-enabled: false
# 打印sql
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 全局配置
global-config:
# 数据库相关配置
db-config:
# 主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", U UID:"全局唯一ID UUID";
id-type: AUTO
# 字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
field-strategy: NOT_NULL
# 驼峰下划线转换
column-underline: true
# 逻辑删除
logic-delete-value: 0
logic-not-delete-value: 1
# 数据库类型
db-type: mysql
banner: false
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: ***
password: ***
url: jdbc:mysql://ip:3306/库名?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
添加数据库对应实体类
@Data
@TableName("user")
public class User{
@TableId(value = "user_id", type = IdType.AUTO)
private Integer userId;
@TableField("user_name")
private String userName;
@TableField("pass_word")
private String passWord;
}
添加Mapper文件
@Mapper
public interface UserMapper extends BaseMapper<User> {
// 自定义分页
List<User> selectUserPage(@Param(Constants.WRAPPER) QueryWrapper qw);
}
添加Service接口
public interface IUserService extends IService<User>{
// 自定义分页
IPage<User> selectUserPage(IPage<User> page,QueryWrapper qw);
}
添加Service实现类
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService{
@Autowired
UserMapper userMapper;
@Override
public IPage<User> selectUserPage(IPage<User> page, QueryWrapper qw) {
return userMapper.selectPage(page,qw);
}
}
添加Controller
@GetMapping("/list")
@ApiOperationSupport(order = 1)
@ApiOperation("1.1 获取用户列表")
public IPage<User> list()
{
int num=1;int size=2;
IPage<User> page = new Page<>(num, size);
QueryWrapper<User> qw = new QueryWrapper<>();
IPage<User> pageInfo= userService.selectUserPage(page,qw);
return pageInfo;
}
要使用的mybatis-plus的分页功能还要增加一个配置类
@Configuration
public class MybatisPlusConfig {
/**
* 实现 IPage分页功能 写了此 分页才生效
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
备注,所有mybatis-plus的引用需要保持一直不然会存在冲突
com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(Ljava/lang/CharSequence;)
具体可以参考:(引用)https://blog.csdn.net/li_tie_liang/article/details/125671963