当前位置:   article > 正文

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘testApplicat_bean instantiation via constructor failed; nested

bean instantiation via constructor failed; nested exception is org.springfra

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testApplication': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.nos.test.TestApplication

EnhancerBySpringCGLIB
c1828724]: Constructor threw exception; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Invalid numeric type, found: STRING; nested exception is org.bson.BsonInvalidOperationException: Invalid numeric type, found: STRING

题目要求:列出student集合中出现过的所有课程名称(distinct)

  1. public List<String> test6_01(){
  2. Query query = new Query();
  3. List<Integer> list_course = mongoTemplate.findDistinct(query,"CID","student_course",Integer.class);
  4. System.out.println(list_course);
  5. List<String> list = new ArrayList<>();
  6. for(Integer i:list_course){
  7. Query query1 = new Query(Criteria.where("CID").is(i));
  8. Course course = mongoTemplate.findOne(query1,Course.class,"course");
  9. list.add(course.getNAME());
  10. }
  11. return list;
  12. }

org.bson.BsonInvalidOperationException: Invalid numeric type, found: STRING提示我找到错误的数据类型,查找到的CID为String类型,才发现自己在数据中导入的数据,是把CID自动设为String类型的。

然后来看MongoDB中findDistinct()的具体用法:可以去重查询,但是无法排序。

该方法接收4个或5个参数,分别是:

1 query查询条件
2 需要去重的字段filed
3 查询的collection数据库表
4 查询的collection对应的pojo实例
5 返回结果类(返回需要去重的filed字段的list)

修改之后:

  1. public List<String> test6_01(){
  2. Query query = new Query();
  3. List<String> list_course = mongoTemplate.findDistinct(query,"CID","student_course",String.class);
  4. System.out.println(list_course);
  5. List<String> list = new ArrayList<>();
  6. for(String i:list_course){
  7. Query query1 = new Query(Criteria.where("CID").is(i));
  8. Course course = mongoTemplate.findOne(query1,Course.class,"course");
  9. list.add(course.getNAME());
  10. }
  11. return list;
  12. }

查询结果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/140826
推荐阅读
相关标签
  

闽ICP备14008679号