简体   繁体   English

Mybatis mapper 返回 null 值。 在 spring -rest-mybatis-mysql-gradle

[英]Mybatis mapper returns null value. in spring -rest-mybatis-mysql-gradle

{ "code": 1000, "message": "Success", "timestamp": 1596860735157, "data": [ { "id": 1, "contentName": null, "createdTime": 0, "contentPath": null }, { "id": 2, "contentName": null, "createdTime": 0, "contentPath": null }, { "id": 3, "contentName": null, "createdTime": 0, "contentPath": null } ] } { "code": 1000, "message": "Success", "timestamp": 1596860735157, "data": [ { "id": 1, "contentName": null, "createdTime": 0, "contentPath": Z37A6259664C1DAE2989D87 }, { "id": 2, "contentName": null, "createdTime": 0, "contentPath": null }, { "id": 3, "contentName": null, "createdTime": 0, "contentPath" : null } ] }

here on t_content_library=> id:bigint,content_name:varchar(450),content_path:varchar(500),created_time:bigint.这里是 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. model model

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

     @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服务

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

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

You need to enable the MyBatis function that maps underscores to camel case.您需要启用将下划线映射到驼峰式大小写的 MyBatis function。

If you are using Spring Boot, add the following to application.properties :如果您使用的是 Spring 引导,请将以下内容添加到application.properties

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

I got a solution which is not the exact solution to implement a long list of columns.我得到了一个解决方案,它不是实现一长列列的确切解决方案。 It is nothing but by adding a result set to map the result which is.只不过是通过将结果集添加到 map 得到的结果。


@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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM