jpa开发中遇到的坑

  • 给项目添加JPA模块,连接database导入entiry

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EVFZVCan-1582680212297)(C:\Users\hp\Desktop\1582625642391.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PQ4BnfBe-1582680179651)(C:\Users\hp\Desktop\1582628717822.png)]

  • 注意关联的:如果注解在属性上,就都在属性上,如果在get方法上就在get方法上,不然会报错
private List<LandPic> landPics = new ArrayList<>();//这里最好改成set,不然加载多个会报错
    @OneToMany(targetEntity = LandPic.class,fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
    @JoinColumn(name="pic_gid",referencedColumnName = "gid")
    public List<LandPic> getLandPics() {
        return landPics;
    }
  • 主键是一定要有的
@Id
@GeneratedValue
@GeneratedValue注解的strategy属性提供四种值:

-AUTO主键由程序控制, 是默认选项 ,不设置就是这个
-IDENTITY 主键由数据库生成, 采用数据库自增长, Oracle不支持这种方式
-SEQUENCE 通过数据库的序列产生主键, MYSQL  不支持
-Table 提供特定的数据库产生主键, 该方式更有利于数据库的移植
  • 查询
@Query(value = "select * from dfnc_tdxx td  where  1=1 and " +
            " (case when :htbh is not null and :htbh!='' then td.htbh = :htbh else 1=1 end) and"+
            " (case when :czf is not null and :czf!='' then td.czf = :czf else 1=1 end ) and"+
            " (case when :tdlx is not null and :tdlx!='' then td.tdlx = :tdlx else 1=1 end )"
            ,nativeQuery = true)
    Page<Land> getByCondition(@Param("htbh") String htbh,@Param("czf") String czf,@Param("tdlx") String tdlx, Pageable pageable);
  • Controller
@GetMapping("get")
    public List<Land> getByCondition(@RequestParam(defaultValue = "") String htbh, @RequestParam(defaultValue = "") String czf, @RequestParam(defaultValue = "") String tdlx,
                                     @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum,pageSize);//分页
        Page<Land> lands = landRepository.getByCondition(htbh, czf, tdlx, pageable);
        List<Land> content = lands.getContent();
        return content;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值