【问题标题】:Mybatis mapper returns null value. in spring -rest-mybatis-mysql-gradleMybatis mapper 返回空值。在春天-rest-mybatis-mysql-gradle
【发布时间】:2020-08-08 05:22:01
【问题描述】:

{ “代码”:1000, “消息”:“成功”, “时间戳”:1596860735157, “数据”: [ { “身份证”:1, “内容名称”:空, “创建时间”:0, “内容路径”:空 }, { “身份证”:2, “内容名称”:空, “创建时间”:0, “内容路径”:空 }, { “身份证”:3, “内容名称”:空, “创建时间”:0, “内容路径”:空 } ] }

在 t_content_library=> id:bigint,content_name:varchar(450),content_path:varchar(500),created_time:bigint。

mysql> select * from t_content_library;
+----+----------------------------------------------------+----------------------------------------------------------------------------------+---------------+
| id | content_name                                       | content_path                                                                     | created_time  |
+----+----------------------------------------------------+----------------------------------------------------------------------------------+---------------+
|  1 | pexels-jack-redgate-3014019.jpg                    | C:\Users\DELL\Desktop\content\pexels-jack-redgate-3014019.jpg                    | 1596783953545 |
|  2 | pexels-thiago-matos-2335275.jpg                    | C:\Users\DELL\Desktop\content\pexels-thiago-matos-2335275.jpg                    | 1596784207089 |
|  3 | louis-hansel-shotsoflouis-2gwghEzGp4g-unsplash.jpg | C:\Users\DELL\Desktop\content\louis-hansel-shotsoflouis-2gwghEzGp4g-unsplash.jpg | 1596784491699 |
|  4 | jessica-delp-p1P_e86R2DI-unsplash.jpg              | C:\Users\DELL\Desktop\content\jessica-delp-p1P_e86R2DI-unsplash.jpg              | 1596784579313 |
|  5 | patrick-untersee-OrT5-yC95j0-unsplash.jpg          | C:\Users\DELL\Desktop\content\patrick-untersee-OrT5-yC95j0-unsplash.jpg          | 1596784602268 |
|  6 | jun-zhao-XWQ15ixxRjE-unsplash.jpg                  | C:\Users\DELL\Desktop\content\jun-zhao-XWQ15ixxRjE-unsplash.jpg                  | 1596784616456 |
|  7 | jo-jo-mPM-x0zPhok-unsplash.jpg                     | C:\Users\DELL\Desktop\content\jo-jo-mPM-x0zPhok-unsplash.jpg                     | 1596784632238 |
|  8 | melnychuk-nataliya-8J6uuvsdj-4-unsplash.jpg        | C:\Users\DELL\Desktop\content\melnychuk-nataliya-8J6uuvsdj-4-unsplash.jpg        | 1596784644961 |
|  9 | nathan-anderson-UhagOo7IOyc-unsplash.jpg           | C:\Users\DELL\Desktop\content\nathan-anderson-UhagOo7IOyc-unsplash.jpg           | 1596784877626 |
| 10 | gayathri-sri-ptbKY_b1ROc-unsplash.jpg              | C:\Users\DELL\Desktop\content\gayathri-sri-ptbKY_b1ROc-unsplash.jpg              | 1596784887985 |
| 11 | Depositphotos_95439918_xl-2015_1920x1920.jpg       | C:\Users\DELL\Desktop\content\Depositphotos_95439918_xl-2015_1920x1920.jpg       | 1596907309367 |
+----+----------------------------------------------------+----------------------------------------------------------------------------------+---------------+
  1. 型号

     @Data
     @AllArgsConstructor
     @NoArgsConstructor
     @Builder
     @ToString
     public class ContentLibrary {
       int id;
       String contentName;
       long createdTime;
       String contentPath;
    
     }
    
  2. 控制器

     @RequestMapping(path = "content/library/")
     @RestController
     public class ContentLibraryController {
     @Autowired
     ContentLibraryService contentLibraryService;
    
     @PostMapping("")
     public Response readLibrary() {
     List<ContentLibrary> contentLibraries = 
     contentLibraryService.readContentLibrary();
     return ResponseBuilder.buildSuccessResponse(contentLibraries);
     }
     }
    
  3. 服务

     @Service
     public class ContentLibraryService {
     @Autowired
     ContentLibraryMapper contentLibraryMapper;
    
     ContentLibrary content = new ContentLibrary();
    
     public List<ContentLibrary> readContentLibrary() {
      return contentLibraryMapper.readAllLibraryContent();
     }
     }
    
  4. 映射器

      @Repository
      @Mapper
      public interface ContentLibraryMapper {
       @Select("SELECT 
       * FROM 
       t_content_library")
       List<ContentLibrary> readAllLibraryContent();
       }
    

【问题讨论】:

    标签: spring rest gradle mybatis pojo


    【解决方案1】:

    我得到的解决方案不是实现一长列列的确切解决方案。只不过是通过添加一个结果集来映射结果。

    
    @Results({
            @Result(property ="id",column = "id"),
            @Result(property ="contentName",column = "content_name"),
            @Result(property ="contentPath",column = "content_path"),
            @Result(property ="createdTime",column = "created_time"),
    })
        public List<ContentLibrary> readAllLibraryContent();
    

    【讨论】:

      【解决方案2】:

      你需要开启MyBatis函数,将下划线映射到驼峰式。

      如果您使用的是 Spring Boot,请将以下内容添加到 application.properties

      mybatis.configuration.map-underscore-to-camel-case=true
      

      【讨论】:

      • 是的,我已经添加了映射器配置,并且曾经使用它一段时间,对于表 t_user 没有任何问题。但现在它与其他表的响应空值发生冲突。
      猜你喜欢
      • 2017-06-19
      • 2012-12-08
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多