Springboot使用JPA框架对数据库实现增删改查(附详细代码)

本文详细介绍了如何在Springboot应用中利用JPA框架进行数据库的增删改查操作。从环境配置、实体类创建、JPA接口定义到Controller代码实现,每个步骤都有详细的说明和示例代码。通过具体接口调用,展示了获取所有记录、按ID获取记录、按ID列表获取记录、按条件查询以及分页查询等功能。同时,还涵盖了实体的保存(新增与修改)及删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

1、本文将详细阐述如何使用JPA框架对数据库实现增删改查操作,业务中比较常见的应用场景几乎在这里都能看到,并且有详尽的代码可供直观演示,其中遇到的坑也进行了实时标注。
2、JPA的环境配置在前面的章节已有总结,不再赘述,直接上干货。

环境准备
步骤1:创建实体类对象

@Entity	//该注解必须加,表明这是一个与数据库映射的实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "merchant_info") //项目启动后数据库会自动创建merchant_info表
@ApiModel(value = "商户信息表") //该注解以及后续controller中所有以@Api开头的注解表明引入swagger框架,可不加,丝毫不影响,我是为了便于用wagger调用接口演示。
public class MerchantInfo {
   
   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) //必须加该注解,表明id自增,且唯一
    @ApiModelProperty(value = "主键id")
    private Long id;

    @ApiModelProperty(value = "商户名称")
    private String merchantName;

    @ApiModelProperty(value = "城市名称")
    private String cityName;

    @ApiModelProperty(value = "父对象id")
    private Long parentId;

    @ApiModelProperty(value = "商户状态: 1 生效 2 失效")
    private Long status;

    @ApiModelProperty(value = "随机生成码")
    private String invitationCode;

    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "负责人")
    private String principal;

    @ApiModelProperty(value = "负责人联系方式")
    private String principalPhone;

}

步骤2:创建与实体类MerchantInfo相关的JPA接口,并使其继承JpaRepository接口,泛型传递<MerchantInfo, Long>

//此注解必加,表明它是一个bean类,项目启动后spring容器扫描到该注解,会将该类初始化成bean对象,便于其他程序调用
@Component
public interface MerchantInfoJpaRepository extends JpaRepository<MerchantInfo,Long> {
   
   
}

步骤3:SpringBoot主程序入口,如果只用JPA框架,相关的配置只需加@EnableJpaRepositories注解即可

@SpringBootApplication
@EnableJpaRepositories
public class WebapitestApplication {
   
   
    public static void main(String[] args) {
   
   
        SpringApplication.run(WebapitestApplication.class, args);
    }
}

步骤4:创建Controller类
下面是controller类整体格式,后面对数据库表merchant_info进行增删改查的所有代码都会在这个controller类里书写。

@RestController //此注解等同于@Controller(用在类上) + @ResponseBody(用在方法上)注解组合
@Slf4j	//该注解可以在程序中直接使用log.info()打印日志
@Api(tags = "数据库表merchant_info进行增删改查") //此项是swagger的注解,可以不加,丝毫不影响
public class MerchantInfoController {
   
   

    @Autowired
    private MerchantInfoJpaRepository merchantInfoJpaRepository;
    
    方法1: ...{
   
   }
    方法2: ...{
   
   }
    方法3: ...{
   
   }
    }
 }

JPA查询功能

1、findAll()方法:无条件查询merchant_info表中所有数据

步骤1:Controller中代码如下:

public class MerchantInfoController {
   
   
    @Autowired
    private MerchantInfoJpaRepository merchantInfoJpaRepository;
    /**
     *  JPA findAll() 无条件查询表中所有数据
      * @return
     */
    @GetMapping("/getAllMerchantInfo")
    @ApiOperation(value = "获取所有商户信息")
    public List<MerchantInfo> getAllMerchantInfo(){
   
   
        List<MerchantInfo> merchantInfoList = merchantInfoJpaRepository.findAll();
        log.info("打印所有商户列表:{}",merchantInfoList);
        return merchantInfoList;
    }
    }
 }

步骤2:接口调用:http://localhost:8080/getAllMerchantInfo
步骤3:接口返回结果:

[
  {
   
   
    "id": 1,
    "merchantName": "广州泽天君成科技有限公司",
    "cityName": "广州",
    "parentId": null,
    "status": 2,
    "invitationCode": "207717",
    "createTime": "2021-03-23T03:37:00.000+00:00",
    "updateTime": "2021-04-27T09:02:11.000+00:00",
    "remark": null,
    "principal": null,
    "principalPhone": null
  },
  {
   
   
    "id": 2,
    "merchantName": "广州市青豪企业管理有限责任公司",
    "cityName": "温州",
    "parentId": null,
    "status": 2,
    "invitationCode": "126155",
    "createTime": "2021-03-23T03:43:43.000+00:00",
    "updateTime": "2021-04-27T06:47:00.000+00:00",
    "remark": null,
    "principal": null,
    "principalPhone": null
  },
  ...
  省略若干
  ...
]

2、findById(Long id)方法:根据id查询merchant_info表中对应的数据

步骤1:Controller中代码如下:

/**
     * JPA findById(Long id) 根据主键id查询表中相应数据
     * @param id
     * @return
     */
    @GetMapping("/getMerchantById")
    @ApiOperation(value = "获取指定id的商户信息")
    @ApiImplicitParam(name = "id",value = "商户id",required = true,defaultValue = "15")
    public MerchantInfo getMerchantById
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值