Mysql 原因:org.springframework.dao.IncorrectResultSizeDataAccessException:查询未返回唯一结果:3

Mysql 原因:org.springframework.dao.IncorrectResultSizeDataAccessException:查询未返回唯一结果:3,mysql,spring-boot,spring-security,spring-data-jpa,spring-data,Mysql,Spring Boot,Spring Security,Spring Data Jpa,Spring Data,我是SpringSecurity的新手,我正在使用SpringSecurity和Msql构建一个应用程序系统 我在JpaRepository中使用extera查询方法,该方法不返回结果,并显示IncorrectResultSizeDataAccessException 这是我的密码 用户存储库 客户服务详情 @吸气剂 @塞特 公共类CustomeUserDetail实现UserDetails{ /** * */ 私有静态最终长serialVersionUID=-835444753664979

我是SpringSecurity的新手,我正在使用SpringSecurity和Msql构建一个应用程序系统 我在JpaRepository中使用extera查询方法,该方法不返回结果,并显示IncorrectResultSizeDataAccessException

这是我的密码

用户存储库 客户服务详情

@吸气剂
@塞特
公共类CustomeUserDetail实现UserDetails{
/**
* 
*/
私有静态最终长serialVersionUID=-8354447536649796292L;
@自动连线
私人用户;
@凌驾

public Collection您的数据库表中似乎有多个用户使用相同的用户名。因此
User findByUsername(字符串用户名);
返回多个结果

您可以执行以下操作之一:

  • 使数据库中的用户名列唯一
  • 将您的存储库方法更改为
    列出findByUsername(字符串用户名);
    以获取所有用户 用户名
  • 将存储库方法更改为
    User findFirstByUsername(字符串用户名);
    以仅获取一个(随机)用户
  • package com.ganesh.repository;
    
    
    import java.util.List;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.CrudRepository;
    
    import com.ganesh.model.User;
    
    
    public interface UserRepository extends JpaRepository<User, Integer> {
    
        User findByUsername(String username);
            
    }
    
    
    @Service
    public class CustomUserDetailsService implements UserDetailsService {
        
        @Autowired
        private UserRepository userRepo;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            
            User user = userRepo.findByUsername(username);
            
            
            CustomeUserDetail userdetails = null;
            
            if(user != null) {
                
                userdetails = new CustomeUserDetail();
                
                userdetails.setUser(user);
            
                
            }else {
                throw new UsernameNotFoundException("User not fond this username"+ username);
            }
            return userdetails;
            
            
        }
    
    }
    
    
    @Getter
    @Setter
    public class CustomeUserDetail implements UserDetails {
        
        /**
         * 
         */
        private static final long serialVersionUID = -8354447536649796292L;
        
        
        @Autowired
        private User user;
    
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            
            return user.getRoles().stream().map(role -> new SimpleGrantedAuthority("ROLE_"+ role)).collect(Collectors.toList());
        }
    
        @Override
        public String getPassword() {
            
            return user.getPassword();
        }
    
        @Override
        public String getUsername() {
            
            return user.getUsername();
        }
    
        @Override
        public boolean isAccountNonExpired() {
            
            return true;
        }
    
        @Override
        public boolean isAccountNonLocked() {
            
            return true;
        }
    
        @Override
        public boolean isCredentialsNonExpired() {
            
            return true;
        }
    
        @Override
        public boolean isEnabled() {
            
            return true;
        }
    
    }
    #properties
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/spring_auth
    spring.datasource.username=root
    spring.datasource.password=root
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.show-sql=true
    spring.jpa.propertirs.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    hibernate.format_sql=true