Repository 是这么写的:
public default List<acl_accounts> findBySchool_Code(Integer School_Code) { return null; }
Controller 是这么写的:
@Autowired private acl_accountsRepository repository; @GetMapping("/detail") public Result getDetail(@RequestParam("School_Code") Integer School_Code) { return ResultUtil.success(repository.findBySchool_Code(School_Code)); }
1 airfling 2019-10-21 10:41:25 +08:00 acl_accountsRepository 这个你是怎么写的 |
2 airfling 2019-10-21 10:42:12 +08:00 刚看到,你返回的都 null,还问为啥,你方法都没实现好 |
![]() | 4 Macolor21 2019-10-21 10:48:04 +08:00 ![]() 看起来用的是 JPA,把 findBySchool_Code 这个方法去掉,看你继承了哪个 Repository 接口,一般 JPA 都实现了 findBy 字段的方法,不用自己再写一个。不行的话试试在 acl_accounts 的 School_Code 属性上面加个 @Column( value ="数据库字段名") |
5 airfling 2019-10-21 10:48:05 +08:00 你如果用 spring data jpa 下面这样就可以 public interface ComponentRepository extends JpaRepository<Component, Long> { List<Component> findBySchool_Code(String School_Code); } |
6 airfling 2019-10-21 10:49:36 +08:00 spring data jpa 的接口没必要用 default 关键词去自己实现 |
![]() | 7 msg7086 2019-10-21 10:55:02 +08:00 返回 null 的原因难道不是你代码第二行返回了 null 吗…… |
![]() | 8 telami 2019-10-21 10:59:49 +08:00 。。。。。。 |
9 Leiothrix 2019-10-21 11:03:28 +08:00 我佛了 |
![]() | 10 jrtzxh020 2019-10-21 11:05:17 +08:00 return null 自己写的都看不懂。。。 |
11 haloha 2019-10-21 11:06:17 +08:00 你自己 return null 了啊... |
12 vwym 2019-10-21 11:07:15 +08:00 ![]() JPA 接口不用自己实现,继承 JpaRepository 会自动实现的。 参考 5 楼的写法就行。 |
![]() | 13 From313 OP @msg7086 主要我不写这个的话就报错了... @airfling 我这边不写 default return null 啥的就报错... Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'acl_accountsController': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'al_accountsRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.xxx.trynetwork.repository.acl_accountsRepository.findBySchool_Code(java.lang.Integer)! No property school found for type acl_accounts! |
14 TMDlw 2019-10-21 11:08:14 +08:00 给整笑了 |
![]() | 15 chocotan 2019-10-21 11:10:06 +08:00 ![]() 看报错信息,下划线的问题吧 findBySchoolCode 写 java 的话,命名规则不要特立独行 |
16 xiangbudaomingzi 2019-10-21 11:12:00 +08:00 这哪里来的萌新。。。 |
![]() | 17 sty 2019-10-21 11:13:11 +08:00 School_Code 这什么命名方式 |
19 airfling 2019-10-21 11:19:40 +08:00 ![]() 我还以为你还有 school 这张表,jpa 是按照你 java 中的字段来的,不是数据库中的 |
![]() | 20 jsy123392550 2019-10-21 11:19:54 +08:00 大家都撤了吧 我感觉这人在钓鱼 |
![]() | 22 HolmLoh 2019-10-21 11:42:05 +08:00 我怀疑你再炸鱼 |
24 jadehare 2019-10-21 11:54:36 +08:00 别的不说,你这命名真的,真的会被人打死的 |
![]() | 25 dhairoot 2019-10-21 11:57:34 +08:00 命名规范一下好不好 |
26 FightForFreedom 2019-10-21 13:18:40 +08:00 这钓鱼也钓的太钩直饵咸了吧,不过我还是上钩了 命名太蛋疼了,@Column 搞一下啊 |
![]() | 27 itechify PRO 。。。 |
![]() | 28 aragakiyuii 2019-10-21 13:3831 +08:00 via Android 命名最好以驼峰式,对应数据库中下划线形式 |
29 chanchan 2019-10-21 13:51:57 +08:00 这命名看得我想给你一拳 |
![]() | 30 xaplux 2019-10-21 13:58:19 +08:00 看见大家都在喷你的命名,我就放心了 |
![]() | 31 Yiki888 2019-10-21 13:59:18 +08:00 楼上加一 |
![]() | 32 kasper4649 2019-10-21 14:37:42 +08:00 via iPhone 又是驼峰又是下划线??? |
![]() | 33 codzzb 2019-10-21 14:43:26 +08:00 hhhhhh |
![]() | 34 arthas2234 2019-10-21 14:45:57 +08:00 我觉得你应该去学习下代码规范 |
![]() | 35 luoyikang52066 2019-10-21 14:54:18 +08:00 via Android 老子看你命名,想打死你 |
![]() | 36 mind3x 2019-10-21 15:44:56 +08:00 via Android 大家好,我是鱼 |
37 DebugTy 2019-10-21 17:11:29 +08:00 看到你这命名,我吐了 |
![]() | 38 zpm683 2019-10-21 17:13:25 +08:00 因为还没学到家~ |
39 yc8332 2019-10-21 17:42:15 +08:00 大佬。。。你都写了 return null,不返回才怪呢 |
40 muskill 2019-10-21 18:01:32 +08:00 ![]() |
![]() | 41 LuckyBoyGirl 2019-10-21 18:15:07 +08:00 我的哥 |
![]() | 42 Drinker 2019-10-21 19:20:30 +08:00 JPA 的持久层不需要实现,是 interface,写出方法即可。 |
![]() | 43 magiclz233 2019-10-21 22:17:28 +08:00 你 return 了个 null 当然得到的是 null 还有,命名用驼峰,中间加个_看不懂 而且 jpa 不惜要你写实现 直接 findByCode 就出来了 |
![]() | 44 q4487979711 2019-10-22 10:24:16 +08:00 接口命名小写加下划线,6 啊 |