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();
}