在上一文中我根据 Mybatis 中 Mapper 的生命周期手动实现了一个简单的通用 Mapper 功能,但是遗憾的是它缺乏实际生产的检验。因此我选择更加成熟的一个 Mybatis 开发增强包。它就是已经获得 Gitee 获得 6900 star 的 Mybatis Plus 。
Gitee: https://gitee.com/felord/kono day04 分支 GitHub: https://github.com/NotFound403/kono day04 分支
Gitee: https://gitee.com/felord/kono day04 分支
GitHub: https://github.com/NotFound403/kono day04 分支
我认为如果你需要在你的框架中引用第三方包,需要考量的最大因素不是它功能的强大而是它社区的活跃度。文档是否齐全,是否拥有完善的 Q/A ,版本维护周期是否持续。目前来看 Mybatis Plus 经过数年的打磨算是比较成熟的类库。目前我认为它几个突出的优点:
UpdateWrapper
QueryWrapper
以mybatis-plus-boot-starter 3.3.2 版本为例。
我们只需要将 kono 项目 day02 分支pom.xml中的mybatis-spring-boot- starter依赖替换为即可:
pom.xml
mybatis-spring-boot- starter
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency>
如果你还需要代码生成器功能请引入下面的模块:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <scope>compile</scope> <optional>true</optional> </dependency> <!-- 引入freemarker包作为代码生成器引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <scope>compile</scope> <optional>true</optional> </dependency>
关于代码生成器可参阅Mybatis Plus代码生成器和Java代码生成器的原理这两篇文章,本文不再对代码生成器进行详细解释。
Mybatis Plus 重写了 starter ,所以配置上有了变化。使用原生 Mybatis 的 Starter 配置为:
mybatis: configuration: # 下划线转驼峰 map-underscore-to-camel-case: true # 扫描 xml 文件位置 mapper-locations: classpath:mapper/*Mapper.xml
对应的 Mybatis Plus 配置为:
mybatis-plus: configuration: # 下划线转驼峰 map-underscore-to-camel-case: true # 扫描 xml 文件位置 mapper-locations: classpath:mapper/*Mapper.xml
我们还可以增加一些 Mybatis Plus 所集成的特性,诸如新增修改查询填充策略、逻辑删除、主键填充等等个性化能力。这里以最常用的主键填充策略为例,如果我们使用分布式主键策略雪花算法的话,可以增加如下配置:
mybatis-plus: global-config: db-config: id-type: assign_id
分页也是我们非常常用的功能,最后我们加入 Mybatis Plus 分页功能,整体的配置如下:
/** * mybatis configuration. * * @author felord.cn */ @MapperScan("cn.felord.kono.mapper") @Configuration public class MybatisConfiguration { /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
请注意这里的分页只支持一对一分页,一对多分页还不支持。原因请阅读一对多分页SQL应该怎么写。
Mybatis Plus 使用了一种叫 Active Record 的领域模型范式,特点是一个模型类(Model)对应关系型数据库中的一个表(table),而模型类的一个实例对应表中的一行记录。因此我们的 POJO 对象这么编写,以UserInfo为例:
UserInfo
/** * @author felord.cn * @since 15:43 **/ @EqualsAndHashCode(callSuper = false) @Data public class UserInfo extends Model<UserInfo> { private static final long serialVersionUID = -8938650956516110149L; @TableId private Long userId; private String name; private Integer age; @Override protected Serializable pkVal() { return this.userId; } }
不适用lombok的情况下请对POJO增加Getter/Setter方法。
对应的Mapper接口只需要继承BaseMapper就可以了:
BaseMapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {}
这时就已经具有了通用 Mapper 提供的一些接口能力了,当然你可以使用传统的模式去实现自定义的操作方法。
服务层的接口范式是这样的,需要继承IService接口:
IService
public interface UserInfoService extends IService<UserInfo> {}
实现类UserInfoServiceImpl同时继承了ServiceImpl:
UserInfoServiceImpl
ServiceImpl
@Service public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService { }
接下来就是使用了,首先我们来用通用 Mapper 进行查询:
final String userId ="12334343455457"; UserInfo byId = userInfoService.getById(userId);
我们也可以使用更加灵活的方式来实现上面的查询:
final String userId ="12334343455457"; UserInfo one = userInfoService.getOne(new QueryWrapper<UserInfo>().lambda() .eq(UserInfo::getUserId, userId));
你可以根据你的业务灵活地增加更多的条件或者指定查询字段进行单表查询,而无需编写 SQL ,其它新增、更新、删除同理。对于上面的范式, Mybatis Plus 提供了代码生成器来实现。
到这里基本的 Mybatis Plus 整合就完成了,更多特性可去官方文档去了解使用。多多关注: 码农小胖哥 继续来和我一起整合脚手架。
原文链接:https://www.cnblogs.com/felordcn/p/13472152.html