《SpringBoot篇》09.Spring Data JPA简介与SpringBoot整合超详细教学(二)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 《SpringBoot篇》09.Spring Data JPA简介与SpringBoot整合超详细教学(二)

(3)填写配置文件

说明: 在application.properties中填写一下配置,这里数据库,用户名,密码 换成自己的。


#自动生成数据库表(关键)
spring.jpa.hibernate.ddl-auto=update
#mysql数据库连接配置(非常重要)
spring.datasource.url = jdbc:mysql://localhost:3306/springbootjpa?serverTimezone=Asia/Shanghai
#数据库用户名
spring.datasource.username = root
#数据库密码
spring.datasource.password = root
#mysql数据库驱动程序(重要)
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
#jpa配置:在控制台显示Hibernate的sql(可选)
spring.jpa.show-sql = true
#其他配置:关闭Thymeleaf 的缓存
spring.thymeleaf.cache = false

image.png


(4)创建数据库并写实体类

说明:只用创建库和实体类,不用创建表信息,当运行项目有实体类就自动生成表了。这也是方便的一点。


image.png

package com.example.demo.pojo;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
//lombok
@Data
//声明为实体类
@Entity
public class Admin implements Serializable {
    //主键id
    @Id
    //主键id 生成策略 IDENTITY表示数据库自动生成
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    //Long 代表bigint
    private Long id;
    //数据库列的字段,非空唯一,长度20
    @Column(nullable = false,unique = true,length = 20)
    private String username;
    //数据库列的字段,非空,长度20
    @Column(nullable = false,unique = true,length = 20)
    private String password;
    @Column(nullable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date rdate;
}


说明:这时再看数据库已经出现表了,说明成功!!!


image.png


(5)编写repository接口与Controller类

说明:创建repository包下接口AdminrRepository,该接口要 extends PagingAndSortingRepository<Admin,Long>接口,这个接口是最新的,有分页和排序功能。其中Admin表示实体模型,Long代表主键类型。**


package com.example.demo.repository;
import com.example.demo.pojo.Admin;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
//声明为repository
@Repository
public interface AdminRepository extends PagingAndSortingRepository<Admin,Long>  {
}
package com.example.demo.controller;
import com.example.demo.pojo.Admin;
import com.example.demo.repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class AdminController {
    @Autowired
    private AdminRepository adminRepository;
    @GetMapping("findall")
    public List<Admin> finall(){
        List<Admin> all = (List<Admin>) adminRepository.findAll();
        return all;
    }
}


image.png


image.png



(6)测试

说明:在数据库admin表中填写一条/多条数据,重新启动项目。然后在网址中输入localhost:8080/findall 出现数据说明成功!!!

image.png

image.png


注:其他的方法就不写了,只是做一个配置演示,都是很简单的。JPA最厉害的是使用命名规则进行创建接口方法,将会在下一个博客中介绍常见的命名规则。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
Java 测试技术 数据库
SpringBoot:@Profile注解和Spring EL
SpringBoot:@Profile注解和Spring EL
|
2月前
|
存储 安全 Java
SpringBoot搭建Spring Security 入门
SpringBoot搭建Spring Security 入门
113 0
|
1月前
|
存储 安全 Java
Spring Boot整合Spring Security--学习笔记
Spring Boot整合Spring Security--学习笔记
53 0
|
1月前
|
消息中间件 Cloud Native Java
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
|
1月前
|
人工智能 JSON 前端开发
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
|
16天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
1月前
|
消息中间件 JSON Java
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
480 0
|
5天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的教学辅助微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的教学辅助微信小程序的详细设计和实现
22 0
|
16天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
14 0
|
22天前
|
开发框架 负载均衡 Java
Spring boot与Spring cloud之间的关系
总之,Spring Boot和Spring Cloud之间的关系是一种构建和扩展的关系,Spring Boot提供了基础,而Spring Cloud在此基础上提供了分布式系统和微服务架构所需的扩展和工具。
18 4
Spring boot与Spring cloud之间的关系