Mybatis-Plus
https://baomidou.com/
# 快速开始
添加依赖:
<!-- MySQL连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- mp依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
配置文件添加数据源:
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/link?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
connection-test-query: select 1
connection-timeout: 20000
idle-timeout: 300000
maximum-pool-size: 5
minimum-idle: 5
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
启动类添加配置扫描:
@SpringBootApplication
@MapperScan("com.Nreal.shortlink.admin.dao.mapper")
public class ShortLinkAdminApplication {
public static void main(String[] args) {
SpringApplication.run(ShortLinkAdminApplication.class,args);
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 逆向工程
https://jully.top/generator/
实体类:
@Data
@TableName("t_user")
public class UserDO extends BaseDO {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
private String password;
private String realName;
private String phone;
private String mail;
private Long deletionTime;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
创建对应Mapper接口:
public interface UserMapper extends BaseMapper<UserDO> {
}
1
2
2
创建Service接口及实现类:
public interface UserService extends IService<UserDO> {
}
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 条件构造器
https://baomidou.com/pages/10c804/#abstractwrapper
案例:
@Override
public UserRespDTO getUserByUsername(String username) {
LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
.eq(UserDO::getUsername, username);
UserDO userDO = baseMapper.selectOne(queryWrapper);
//...
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# CRUD接口
https://baomidou.com/pages/49cc81/#service-crud-%E6%8E%A5%E5%8F%A3
# 分页查询
配置分页查询拦截器
@Configuration public class DataBaseConfiguration { /** * 分页插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
1
2
3
4
5
6
7
8
9
10
11
12分页查询
@Override public IPage<ShortLinkPageRespDTO> pageShortLink(ShortLinkPageReqDTO requestParam) { LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class) .eq(ShortLinkDO::getGid, requestParam.getGid()) .eq(ShortLinkDO::getEnableStatus, 0) .eq(ShortLinkDO::getDelFlag, 0); IPage<ShortLinkDO> resultPage = baseMapper.selectPage(requestParam,queryWrapper); return resultPage.convert(each->BeanUtil.toBean(each,ShortLinkPageRespDTO.class)); }
1
2
3
4
5
6
7
8
9其它写法:
@Override public ResponseResult hotArticleList() { LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL) .orderByDesc(Article::getViewCount); Page<Article> page = new Page(1, 10); page(page,queryWrapper); List<Article> articles = page.getRecords(); List<HotArticleVo> hotArticleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class); return ResponseResult.okResult(hotArticleVos); }
1
2
3
4
5
6
7
8
9
10
11@Test public void testPage(){ IPage<User> page = new Page<>(); //设置每页条数 page.setSize(2); //设置查询第几页 page.setCurrent(1); userMapper.selectPage(page, null); System.out.println(page.getRecords());//获取当前页的数据 System.out.println(page.getTotal());//获取总记录数 System.out.println(page.getCurrent());//当前页码 }
1
2
3
4
5
6
7
8
9
10
11
12