保姆级无坑springboot+mybatis-plus-join入门用例

2023-12-13 18:44:11

sql语句? ?最简单连表查询

select
    user.id, username, parentId,password

from user
left join permission on permission.id = user.id
where
    user.id=8;

SQL执行结果?

实体类

UserT

@TableName("user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserT {
    private Integer id;
    private String username;
    private String password;
    private String name;
}

?Permission类

@Data
@TableName("permission") //在数据库中的表名
public class Permission{
    private Integer id;
    private Integer parentId;
    private String name;
}

?Mapper接口

UserTMapper

@Mapper
@Repository  //代表持久层,所有CURD已经完成
public interface UserTMapper extends BaseMapper<UserT>, MPJBaseMapper<UserT> {

}


PermissionMapper

@Mapper
@Repository
public interface PermissionMapper extends BaseMapper<Permission>, MPJBaseMapper<Permission> {

单元测试类

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)   // 
public class mybatisplusjoin {
    @Resource
    private UserTMapper userTMapper;
    @Autowired
    private PermissionMapper permissionMapper;  // 注解为空 不能通过new的方式

    @Test
    public void testSelect() {
        MPJLambdaWrapper<UserT> wrapper = JoinWrappers.lambda(UserT.class)
                .selectAll(UserT.class)
                .select(UserT::getPassword, UserT::getId, UserT::getUsername)
                .leftJoin(Permission.class, Permission::getId, UserT::getId)
                .eq(UserT::getId, 8);

        List<UserT> list = userTMapper.selectJoinList(UserT.class, wrapper);
        list.forEach(System.out::println);
    }


}

执行结果

?

文章来源:https://blog.csdn.net/dwl764457208/article/details/134899251
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。