Springboot 集成 mybatis-plus
导入jar包
maven的pom文件
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version> 3.0.7.1</version>
</dependency>
或者
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version> 3.0.7.1</version>
</dependency>
注意事项
-
两个选择导一个就好,不然会发生冲突,会使用不了mybatis-plus自带的CURD
-
在pom.xml中导入mybatis-plus的jar包时,mybatis-plus的j位置要放在mybatis-springboot的上面,mybatis-springboot的位置最好放在最下面
配置yml
在yml中指名扫描的mapper.xml 和实体类的扫描路径
#后台打印出sql语句
logging:
level:
cn.hstc.recommend.dao: debug
#mybatis-plus配置
mybatis-plus:
#mapper扫描,放在resources下
mapper-locations: classpath*:/mapper/*.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: 你的实体类包路径.*.entity
#添加必要的配置
global-config:
db-config:
id-type: auto
field-strategy: not_empty
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
db-type: mysql
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
注意事项
mapper的映射文件的位置和mapper-locations配置的路径需要一致,不然会报绑定异常的错误
Dao层
在dao/mapper层中的接口继承BaseMapper
@Mapper
public interface UserDao extends BaseMapper<UserEntity> {
}
注意事项
一定要加上@Mapper,或者可以在启动类下使用@MapperScan注明Mapper扫描路径,不然会报找不到baseMapper的异常
Server层
在逻辑接口层继承IService,逻辑实现层继承ServiceImpl,注意导的包是 com.baomidou.mybatisplus下的包
public interface UserService extends IService<UserEntity> {
PageUtils queryPage(Map<String, Object> params);
}
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<UserEntity> page = this.page(
new Query<UserEntity>().getPage(params),
new QueryWrapper<UserEntity>()
);
return new PageUtils(page);
}
}
注意事项:加上@Service
config文件
分页配置文件
@EnableTransactionManagement
@Configuration
@MapperScan("")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}