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

sql语句   最简单连表查询

select
    user.id, username, parentId,password

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

SQL执行结果 

保姆级无坑springboot+mybatis-plus-join入门用例_第1张图片

实体类

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, MPJBaseMapper {

}


PermissionMapper

@Mapper
@Repository
public interface PermissionMapper extends BaseMapper, MPJBaseMapper {

单元测试类

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

    @Test
    public void testSelect() {
        MPJLambdaWrapper 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 list = userTMapper.selectJoinList(UserT.class, wrapper);
        list.forEach(System.out::println);
    }


}

执行结果

保姆级无坑springboot+mybatis-plus-join入门用例_第2张图片 

你可能感兴趣的:(spring,boot,mybatis,java)