Mybatis 返回对象中包含 List集合

遇到这种情况的实体类,需要在mapper.xml文件里使用

resultMap标签以及其他子标签.

先贴代码:

实体类:

UserEntity类:
private Long id;// 用户id
private String username;// 用户名
private String password;// 密码
private List userRoles;// 用户角色集合
Role类:

public class Role {
    private Integer id;    //角色id
    private String roleName;   //角色名称
}

sql文件:

t_user:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user`  (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT '用户id',
  `username` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES (1, '123', 'admin');
INSERT INTO `t_user` VALUES (2, '456', 'admin');

SET FOREIGN_KEY_CHECKS = 1;

t_role:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_role
-- ----------------------------
DROP TABLE IF EXISTS `t_role`;
CREATE TABLE `t_role`  (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT '角色id',
  `role_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '角色名称',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_role
-- ----------------------------
INSERT INTO `t_role` VALUES (1, 'ROLE_USER');
INSERT INTO `t_role` VALUES (2, 'ROLE_ADMIN');

SET FOREIGN_KEY_CHECKS = 1;

t_user_roles:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_user_roles
-- ----------------------------
DROP TABLE IF EXISTS `t_user_roles`;
CREATE TABLE `t_user_roles`  (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT '用户角色对照ID',
  `user_id` int(8) NULL DEFAULT NULL COMMENT '用户ID',
  `role_id` int(8) NULL DEFAULT NULL COMMENT '角色ID',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_user_roles
-- ----------------------------
INSERT INTO `t_user_roles` VALUES (1, 1, 1);
INSERT INTO `t_user_roles` VALUES (2, 2, 2);

SET FOREIGN_KEY_CHECKS = 1;

application.yml :

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db03?characterEncoding=UTF-8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
logging:
  level:
    com.example.bdatabaserole.mapper: debug  # 打印sql语句

mybatis:
  mapper-locations: classpath:mapper/*.xml

Mapper 接口

@Mapper
public interface UserMapper {
    List getUserByUsername(@Param("username") String username);
}

UserMapper .xml





    
        
        
        
        
            
            
        
    

    

UserService 接口

public interface UserService {
    List getUserByUsername(String username);
}

UserServiceImpl 实现类

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }
}
UserController 类

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/getUserByUsername")
    List getUserByUsername(String username){
        return userService.getUserByUsername(username);
    }
}
结果:
[
    {
        "uid": 1,
        "username": "123",
        "password": "admin",
        "userRoles": [
            {
                "id": 1,
                "roleName": "ROLE_USER"
            }
        ]
    }
]

telnet

你可能感兴趣的:(Spring,spring,intellij-idea,maven,tomcat,java)