前言
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(@R