# 问题解决方案 ##### 1、Cannot compare left expression of type 'java.io.Serializable' with right expression of type 'java.lang.Long' ``` 使用@Query查询,因为无法解决序列化问题 ``` ` 分层结构从上到下: ` ``` Controller (表现层) ↓ Service (业务层) ↓ Repository (数据访问层) ``` ` 对于外部集成: ` ``` Controller ↓ Service ←→ Integration Service (集成层) ↓ Repository ``` ``` GET /api/users - 查询所有用户 GET /api/users/list?enabled=true - 查询所有启用的用户 GET /api/users/page?pageNum=1&pageSize=10 - 分页查询用户 ``` `查询日期示例` ``` UserQuery query = new UserQuery(); query.setCreateTimeRange( LocalDateTime.now().minusDays(7), // 一周内 LocalDateTime.now() ); query.setEnabled(true); query.setCreateBy("admin"); @QueryField(type = QueryType.IN) private String status; // 可以传入 "ACTIVE,PENDING,CLOSED" ``` ``` QueryDSL使用方法 @Service @Transactional public class UserServiceImpl extends BaseServiceImpl implements UserService { private final UserRoleRepository userRoleRepository; private final QUserRole qUserRole = QUserRole.userRole; public UserServiceImpl(UserRepository userRepository, UserRoleRepository userRoleRepository) { super(userRepository); this.userRoleRepository = userRoleRepository; } public Set getUserRoles(Long userId) { // 使用QueryDSL构建查询条件 Predicate predicate = qUserRole.user.id.eq(userId); return StreamSupport.stream( userRoleRepository.findAll(predicate).spliterator(), false ).collect(Collectors.toSet()); } public void assignRole(Long userId, Long roleId) { User user = findById(userId); Role role = roleRepository.findById(roleId) .orElseThrow(() -> new RuntimeException("Role not found")); // 检查是否已存在 Predicate predicate = qUserRole.user.id.eq(userId) .and(qUserRole.role.id.eq(roleId)); if (!userRoleRepository.exists(predicate)) { UserRole userRole = new UserRole(user, role); userRoleRepository.save(userRole); } } } ``` ``` -- 插入正常用户数据 INSERT INTO sys_user ( username, password, nickname, email, phone, enabled, deleted, dept_id, dept_name, create_by, create_time, update_by, update_time, version ) VALUES -- 管理员用户 ('admin', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '系统管理员', 'admin@example.com', '13800138000', true, false, 1, '技术部', 'system', '2024-01-01 09:00:00', null, null, 0), -- 普通用户 ('user01', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '张三', 'zhangsan@example.com', '13800138001', true, false, 1, '技术部', 'admin', '2024-01-02 10:00:00', null, null, 0), ('user02', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '李四', 'lisi@example.com', '13800138002', true, false, 2, '市场部', 'admin', '2024-01-03 11:00:00', 'admin', '2024-01-04 15:00:00', 1), -- 已禁用的用户 ('disabled_user', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '王五', 'wangwu@example.com', '13800138003', false, false, 2, '市场部', 'admin', '2024-01-05 14:00:00', 'admin', '2024-01-06 16:00:00', 1), -- 已删除的用户 ('deleted_user', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '赵六', 'zhaoliu@example.com', '13800138004', true, true, 3, '财务部', 'admin', '2024-01-07 10:00:00', 'admin', '2024-01-08 09:00:00', 2), -- 最近创建的用户 ('new_user01', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '小明', 'xiaoming@example.com', '13800138005', true, false, 1, '技术部', 'admin', '2024-03-01 09:00:00', null, null, 0), ('new_user02', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '小红', 'xiaohong@example.com', '13800138006', true, false, 2, '市场部', 'admin', '2024-03-02 10:00:00', null, null, 0), -- 多次更新的用户 ('updated_user', '$2a$10$mW/yJPHjyueQ1g26YxiZNOtr6bKVF4P/w/VHLVHHhxslY.YlXhbcm', '小张', 'xiaozhang@example.com', '13800138007', true, false, 3, '财务部', 'admin', '2024-02-01 09:00:00', 'admin', '2024-03-01 15:00:00', 5); ```