100+ 重磅嘉宾出席 GOTC 2024! 499 专业观众票 【限时免费】
官网 :https://mybatis-mp.cn
0.单表+@Fetch 注解 + fetchFilter 方法
@Data @ResultEntity(SysUser.class) public class SysUserVo { private Integer id; private String userName; private String password; private Integer roleId; private LocalDateTime create_time; @Fetch(source = SysUser.class, property = "roleId", target = SysRole.class, targetProperty = "id") private List<SysRoleVo> sysRoles; }
List<SysUserVO> list = QueryChain.of(sysUserMapper) .from(SysUser.class) .fetchFilter(SysUserVO::getRoles,where->where.eq(SysRole::getStatus,1)) .returnType(SysUserVO.class) .list();
fetchFilter方法是对@Fetch注解的增强,没有特殊要求一般,可忽略
1. 单表查询
SysUser sysUser = QueryChain.of(sysUserMapper) .eq(SysUser::getId, 1) .eq(SysUser::getUserName,'admin') .get();
2.VO 映射
@Data @ResultEntity(SysUser.class) public class SysUserVo { private Integer id; private String userName; //字段名字不一样时 @ResultEntityField(property = "password") private String pwd; }
SysUserVO sysUserVO = QueryChain.of(sysUserMapper) .eq(SysUser::getId, 1) .eq(SysUser::getUserName,'admin') .returnType(SysUserVO.class) .list();
3. join 查询
@Data @ResultEntity(SysUser.class) public class SysUserVo { private Integer id; private String userName; //字段名字不一样时 @ResultEntityField(property = "password") private String pwd; //映射一个对象 1对1 @NestedResultEntity(target = SysRole.class) prviate SysRole sysRole; //映射多个对象 1对多 @NestedResultEntity(target = SysRole.class) prviate List<SysRole> sysRoles; }
List<SysUserRoleVO> list = QueryChain.of(sysUserMapper) .from(SysUser.class) .join(SysUser.class, SysRole.class) .returnType(SysUserRoleVO.class) .list();
还有很多很多超级方便有趣的写法,欢迎大家来使用 https://mybatis-mp.cn
例如:
1 . 多表 join A 内嵌 B B 内嵌 C 都可以
2 . 不使用 join 使用 @Fetch 注解 + fetchFilter 方法实现 将 A JOIN B 变成 query A + query B
3 . 使用 @Paging 注解 实现你的 xml 自动分页
4 . 使用 SQL 模板,让你 ORM 更简单更容易扩展,再也不怕被框架限制了
还没有评论,来说两句吧...