当前位置: 首页 > 知识库问答 >
问题:

找不到能够从类型转换为类型#2的转换器

邓子濯
2023-03-14

我知道这是另一个类似的问题,但我自己无法回答,这就是我写信给你寻求帮助的原因。

我尝试创建自己的@Query,并在两种情况下返回一个转换错误。我的猜测是服务有问题,但这是我的知识结束。

下面是我的代码:

>

  • 主实体

     @Data
     @Entity
     @Table(name = "users")
     public class User {
    
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private long id;
     private String name;
     private String city;
     private  Date startDate;
     private boolean stat;
    
     public User() {
     }
    
     public User(String name, String city, Date startDate, boolean stat) {
         this.name = name;
         this.city = city;
         this.startDate = startDate;
         this.stat = stat;
      }
     }
    

    2.第二种模式

    public class UserName {
        private String firstname;
        public UserName() {
        }
        public UserName(String firstname) {
            this.firstname = firstname;
        }
        public String getFirstname() {
            return firstname;
        }
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
    }
    

    3.第三种模式

    public class UserCount {
    
        private String city;
        private int count;
    
        public UserCount() {
        }
        public UserCount(String city, int count) {
            this.city = city;
            this.count = count;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public int getCount() {
            return count;
        }
        public void setCount(int count) {
            this.count = count;
        }
    }
    

    存储库

    @Repository public interface UserRepository extends jparepository {

     @Query("select p from User p")  //1.it's work
     List<User> getAll();
    
     @Query("select u from User u where u.name like %?1")  //2.it's work
     List<User> findByFirstnameEndsWith(String firstname);
    
     @Query("select u.name from User u ")  //3. don't work
     List<UserName> getNameUsers();
    
     // this SQL working in database console H2
     // SELECT city, count(*) FROM USERS  WHERE stat = true GROUP BY  city
     @Query("select u.city, count (u) from User u where u.stat = true group by u.city")  //3. don't work
     List<UserCount> getOwnQuery();
    

    }

    服务

    @Service public class UserService{@AutoWired private UserRepository Repo

     public List<UserName> getN (){
         return repo.getNameUsers();
     }
    
     public List<UserCount> getC(){
         return repo.getOwnQuery();
     }
    

    }

    控制

    @Controller公共类MyController{

     @Autowired
     private UserRepository repo;
     @Autowired
     private UserService repoService;
    
     @GetMapping("/") //1.it's work
     ResponseEntity<List<User>> getAllCity(Pageable page){
         return ResponseEntity.ok(repo.getAll());
     }
     @GetMapping("/s") //2.it's work
     ResponseEntity<List<User>> getAllUsers(Pageable page){
         return ResponseEntity.ok(repo.findByFirstnameEndsWith("Seba"));
     }
     @GetMapping("/f") ///3.don't work
     ResponseEntity<List<UserName> >getUsersName(Pageable page){
         return ResponseEntity.ok(repoService.getN());
     }
     @GetMapping("/c") ///4.don't work
     ResponseEntity<List<UserCount> >getUsersCount(Pageable page){
         return ResponseEntity.ok(repoService.getC());
     }
    

    }

    它还在GitHub上添加了源代码

    对不起,我没有添加错误代码

  • 共有1个答案

    孔寒
    2023-03-14

    在@Query中使用带有new关键字的构造函数获取列表

    @Query("select NEW com.sub.model.UserName(u.name) from User u ")
    List<UserName> getNameUsers();
    

    并对列表 执行相同的操作

    @Query("select NEW com.sub.model.UserCount(u.city, count(u)) from User u where u.stat = true group by u.city")
    List<UserCount> getOwnQuery();
    
     类似资料: