springboot整合mybatisplus
一、介绍
① MyBatis Plus是基于MyBatis的增强工具,它提供了很多便捷的功能,减少了开发人员的工作量,使得开发更加高效。
② MyBatis Plus的主要特性如下:
-
CRUD操作:MyBatis Plus封装了常用的CRUD操作,几乎可以零SQL实现常规的数据访问操作,大大减少了工作量。
分页插件:MyBatis Plus提供了分页插件,支持多种分页方式,简化分页操作。
代码生成:MyBatis Plus提供了代码生成器,可以快速生成Mapper接口和Entity实体类的代码。
条件构造器:MyBatis
Plus提供了LambdaQueryWrapper,可以简化复杂查询的条件构造,可以使用Lambda表达式编写查询条件,提高代码可读性。自动填充:MyBatis Plus提供了自动填充功能,可以在插入和更新时自动填充指定字段,减少了重复的代码。
乐观锁插件:MyBatis
Plus提供了乐观锁插件,可以使用@Version注解实现MySQL、Oracle、PostgreSQL等数据库的乐观锁,并且支持自动填充版本号。性能分析插件:MyBatis Plus提供了性能分析插件,可以在开发阶段方便地定位SQL性能问题。
二、整合基本步骤
1.在pom.xml文件中添加mybatis-plus-boot-starter和mysql-connector-java依赖。
2.在application.yml或application.properties中配置数据源信息,例如:
spring.datasource.url=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
yml格式:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: password
driver-class-name: com.mysql.jdbc.Driver
application:
name: myapp
# MyBatis Plus配置
mybatis-plus:
# Mapper扫描路径
mapper-locations: classpath*:mapper/**/*.xml
type-aliases-package: com.example.demo.entity
# MyBatis打印SQL语句
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
level:
org.springframework: INFO
com.example.demo: DEBUG
3.创建一个MyBatis Plus的配置类,配置Mapper扫描路径等信息,例如:
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
4.创建实体类和Mapper接口,使用注解或XML定义SQL语句。
5.在Service中调用Mapper方法,例如:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> listUsers() {
return userMapper.selectList(null);
}
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public int addUser(User user) {
return userMapper.insert(user);
}
@Override
public int updateUser(User user) {
return userMapper.updateById(user);
}
@Override
public int deleteUser(Long id) {
return userMapper.deleteById(id);
}
}
三、常见配置
yml下:
# 数据源相关配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: password
driver-class-name: com.mysql.jdbc.Driver
# MyBatis Plus相关配置
mybatis-plus:
global-config:
db-config:
logic-delete-value: 0 # 逻辑删除值(1表示删除)
logic-not-delete-value: 1 # 逻辑未删除值(0表示未删除)
id-type: auto # 主键ID生成策略(支持auto、none、input、assign、uuid、id_worker)
field-strategy: not_empty # 标记非 NULL 属性是否参与插入或更新,默认为 FiledStrategy.NOT_NULL
update-strategy: not_null # 标记注解里的 update 是否生效。默认为 FieldStrategy.NOT_NULL。
insert-strategy: not_null # 标记注解里的 insert 是否生效。默认为 FieldStrategy.NOT_NULL。
banner: false # 关闭 mybatis-plus 的banner
mapper-locations: classpath*:/mapper/**/*Mapper.xml # Mapper映射文件的路径
type-aliases-package: com.example.entity # 实体类的包路径
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名转换(将下划线转换成驼峰命名方式)
cache-enabled: true # 开启二级缓存
其他整合步骤可以参考本合集的其他内容,谢谢。