Mybatis-Plus使用笔记(SpringBoot版)
配置
启动类上配置@MapperScan
扫描mapper接口
application.yml
mybatis-plus: #配置文件配置在mybatis-plus
mapper-locations: classpath:mapper/**/*Mapper.xml # 存放映射mapper文件 扫描Mapper.xml结尾的文件
type-aliases-package: com.ctc.entity # 对象存放位置
configLocation: classpath:mybatis/mybatis-config.xml
type-enums-package: com.ctc.enums.mybatis
global-config:
refresh: true
分页
配置上开启分页
Mybatis-Plus自带分页插件使用方式官方文档上有,
如果单独使用插件,page放在第一个参数
MyPage实现Mybatis-Plus的IPage接口(也可以使用插件自带的Page),查询时可自动分页
使用
List<Demo> listByCondition(MyPage page, @Param(Constants.WRAPPER) Wrapper wrapper);
或者
List<Demo> listByCondition(MyPage page, @Param("condition") Condition condition);
自定义SQL
<select id="listByCondition" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_info i
left join t_dict d on d.info_id= i.info_id
<where>
${ew.sqlSegment}
</where>
</select>
如果使用插件的条件构造器,
构造时注意i
为表的别名
wrapper.eq("i.fk_id", fkId);
重点是sqlSegment
ew 是插件默认
ResultMap配置参见Mybatis官方文档
自定义关联关系
pk为关联对象
@TableField(exist = false)
private PK pk;
ResultMap配置参见Mybatis官方文档
配置
我使用的配置
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
/**
* 性能分析插件 生产环境下不推荐使用
* @return
*/
@Bean
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
/**
* 逻辑删除
* @return
*/
@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
}
后续会把整合的项目传到GitHub上
项目使用的技术有
- SpringBoot
- Mybatis
- Durid
- Fastjson
- Mybatis-Plus
- Swagger
- Hibernate Validate
- 文件上传